Lights on!

What do I want to do?

So everybody knows this, you go into a room and a light goes automaticaly on. In order to get this to work, we need a motion sensor and a light that can be turned on automaticaly. The tricker part is, when do you turn off the light?
One option is to have a constant time delay and then turn off the light. This can be realized using the timer function already present in Domoticz.
The other option is to have time delays, luminosity thresholds and more complicated flows. This is what I offer with my script. So let's dive in.

What devices do I use for this

I am using following devices in my lights setup for the living room:

  1. Multisensor from Aeotec, I think I have the Gen5. It has presence detection, luminescence sensor and your usual temperatur/humidity measurement.
  2. Sonoff Basic Wifi controlled switch. Great switch but not that small or pretty. So be sure to hide it somewhere :)!

So the Multisensor is a Z-Wave device and Sonoff is a China-Cloud controlled Wifi switch? What gives, how to we connect the two together?
Enter Tasmota! Tasmota is an open source firmware for ESP8266 powered devices. I was so happy as I found Tasmota and these cheap devices. Although some tinkering is needed to get the Tasmota on the devices, it is well worth it! Tasmota directly integrates with Domoticz, using the MQTT backend, and it is very versatile.
I have a couple of Sonoff switches and power plugs, some Shelly 1 relays and also a NodeMCU based self-made outside temperature sensor. Although they all come from different hardware manufacturer and are basically bound to different China-Cloud solutions, they can be liberated by Tasmota and interoperate seamlessly.

After you get Tasmota on the Sonoff Basic, you will need Serial-To-USB Adapter to do it, we need a MQTT server installed on our Domoticz setup. Installation of the MQTT server is explained on the wiki, so just dive in. Integration of Tasmota devices in Domoticz, using the MQTT backbone, is explained here.

For the time being you can skip the NodeRED installation, which is also explained on the Domoticz wiki. But we will need NodeRED when I explain my integration of the Rockrobo vacuum cleaner.

The light script explained

My script for light control based on presence detection is here.
I'll just explain the interesting parts, otherwise the blog article will be too long.

In the data section I defined a couple of local variables, e.g. auto_on, in order to have the values stored between the runs of the script.

Basic flow is simple:

  • When the light is low and somebody comes in the room, then turn the light on.
  • If nobody is in the room anymore, then turn the light off.
  • If the motion sensor gets triggered again, then the light is turned on again.
  • The turn on -> turn off procedure is repeated a couple of times, counter is the variable auto_off_count and then it stops. Why? I do not like turning the light on and off all the time.

The motion sensor triggers when somebody enters the room and then it goes off again. In order to have some delay to turning of the light, I save the time when it went off in a variable.

if (domoticz.data.turn_off_time == 0 or (current_state == 'Off' and domoticz.data.old_state == 'On')) then
   domoticz.data.turn_off_time = domoticz.time
end

The light is also turned off, when the light is not low any more. I also do not want the lights to be automatically controlled between 22:00 and 06:00. Why? When I go to pee at night, I do not want to wake up everybody.

if (domoticz.data.light_low == 1) then
    if (current_state == 'On' and domoticz.data.auto_on == 0 and domoticz.time.matchesRule('at nighttime') and not domoticz.time.matchesRule(no_active_time)) then
        domoticz.log('Lichtstärke im Wohnzimmer ist niedrig, schalte das Licht ein.', domoticz.LOG_INFO)
        domoticz.devices('Licht_Wohnzimmer').switchOn()
        domoticz.data.auto_on = 1
    elseif (current_state == 'Off' and domoticz.data.auto_on == 1) then
          if (domoticz.data.auto_off_count < auto_off_max and domoticz.time.compare(domoticz.data.turn_off_time).mins > update_time_max) then
            domoticz.log('Niemand im Wohnzimmer, schalte das Licht aus.', domoticz.LOG_INFO)
            domoticz.devices('Licht_Wohnzimmer').switchOff()
            domoticz.data.auto_on = 0
            domoticz.data.auto_off_count = domoticz.data.auto_off_count + 1
          elseif (domoticz.data.auto_off_count >= auto_off_max) then
              domoticz.log('Automatisches Abschalten nach Bewegungserkennung ist ausgesetzt.', domoticz.LOG_INFO)
        end
    end
else
    if (domoticz.devices('Licht_Wohnzimmer').state == 'On') then
        domoticz.log('Lichtstärke im Wohnzimmer ist hoch, schalte das Licht aus.')
          domoticz.devices('Licht_Wohnzimmer').switchOff()
    end
    domoticz.data.auto_on = 0
    domoticz.data.auto_off_count = 0 -- Reset the counter
end

Final thoughts

So that's it. Not complicated, but it took me some time to get it right.
As always you are free to use the script, modify it or give me tips how to get it better.

Happy Hacking!!

License: CC BY-SA 4.0 Discuss on Mastodon