Automatic lighting in the entrance area with Shelly Mini and Hue motion sensor

In my summer house, there wasn’t originally a standard motion sensor installed to turn on the spotlights in the entrance area, the way you usually see it in most places. The light had to be switched on from inside with an ordinary switch. I therefore decided to set it up as a smart solution instead.

This was done by placing a Shelly 1 Mini Gen4 WiFi & Zigbee relay in the box behind the outlet and the switch. It was set to run on Zigbee, and paired with a Philips Hue outdoor sensor via Home Assistant’s ZHA integration. Both could be paired into the same Zigbee network I used for the rest of the house, without any extra app or bridge.

Two devices, two different paths into ZHA

The Hue sensor was the easy one of the two. It showed up in ZHA the same way as other Zigbee devices – Settings → Devices & services → ZHA → Add device, with the device put into pairing mode – and became two entities I needed separately: a binary motion sensor and an illuminance sensor (lux) that continuously reports how light or dark it is outside.

The Shelly 1 Mini Gen4, on the other hand, is dual-band and ships set up for WiFi by default, not Zigbee. To switch protocol, you have to go into the Shelly portal and actively flash new Zigbee firmware onto it. After that, it can be added in ZHA and switched on and off in Home Assistant.

The Hue device contains both a motion sensor and an illuminance sensor, just like standard motion sensors intended for turning lights on and off. Above a certain lux threshold, the light shouldn’t turn on regardless of whether motion has been detected, and that’s something the automation has to ensure.

Space in the box was a challenge of its own

A particular issue with Danish installations is the space itself behind the physical switch. Fuga switches, the most common system in Denmark, are often set in boxes that in older installations are only about 3 cm deep – nowhere near enough to hold a relay alongside the switch mechanism itself and various wires. The Shelly 1 Mini Gen4 is probably the smallest relay (17 x 29 x 34 mm) available at the moment, but even that can need more space than an older 3 cm box can offer.

In my case, the electrician managed to find room for the relay in a Fuga 2½ module between the outlet and the switches. That solved the space problem without having to replace the whole box or redo the rest of the installation.

The hard part was getting the automation to behave correctly

The easy part was getting the light to turn on with motion. The hard part was getting it to do the *right* thing in all the cases where someone also touches the light themselves – either via the physical switch, the app, or simply because they want the light to stay on while they’re fumbling with keys or unpacking the car.

The fundamental problem is that a switch only knows one thing: whether it’s on or off. It doesn’t know *why* it’s on. If the automation just turns off the light every time the motion sensor settles down again, it will also turn off the light in cases where someone turned it on manually and wants it to stay on, regardless of whether there happens to be motion in front of the sensor at that moment or not.

The solution was a small “auto-on” flag – a helper switch in Home Assistant that’s only set when the automation itself is the one that turned the light on. It’s used like this:

When the motion sensor goes to “on”, and the illuminance is simultaneously below the threshold, the light is turned on and the flag is set at the same time. When the motion sensor later drops back to “off”, the automation checks the flag before turning the light off: only if the light was actually turned on by the automation does the automation also turn it off. If the flag isn’t set – because someone turned the light on manually – the automation doesn’t touch it at all.

And to keep this working reliably, the flag is reset every time the light is turned off at all, no matter how: automatically, manually via the switch, or from the app. Without that step, the flag could be left “set” from an earlier automatic activation, and a subsequent manual activation would be mistakenly interpreted as the automation’s own doing – risking being turned off by it, even though someone had deliberately turned the light on themselves.

The actual delay before the light turns off after motion, incidentally, isn’t in the automation at all, but in the sensor itself – Hue motion sensors have a built-in setting for how long they keep reporting “motion” after the last detected movement, before falling to “off” themselves. That gave a cleaner solution than adding an equivalent delay in the automation, because the sensor’s state then always matches what the dashboard shows, without a hidden extra wait on top.

About the coding

This article describes the principles behind the setup – not the specific Home Assistant automations. The exact code will, in any case, depend on the individual installation: the devices’ entity names, the desired lux threshold, and how long the motion sensor itself keeps reporting motion after the last detection. The automations for turn-on-with-motion, the conditional turn-off, and resetting the auto flag can be generated with AI, if the AI is given the technical requirements described above.

Equipment

  • Shelly 1 Mini Gen4 – WiFi & Zigbee relay, set to Zigbee mode and paired via ZHA. Sits behind the existing switch for the entrance light and controls the lamp as an ordinary switch entity.
  • Philips Hue outdoor sensor, paired via ZHA. Provides both motion (binary sensor) and illuminance in lux, which the automation uses to determine whether it’s dark enough for the light to turn on.
  • Zigbee dongle/coordinator for Home Assistant in order to use ZHA.