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.

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.

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!!

RSS Home

Updated: 2020-12-31 Do 15:14 License: CC BY-SA 4.0