If you’ve read part 1 and part 2, then you’ll already know how to work with MQTT (both receiving and sending commands), how to take sensor information from home-assistant as well as control entities directly in home-assistant, and most importantly, how to work with the switch node for determining what outputs occur according to which specific inputs. Here I want us to look at timers in a bit more detail. A lot of the things that people want to do when working with home automation is to turn various things on and off at certain times of the day or night. Whether that’s simple lighting, computer screens, power plugs etc. There are various approaches to it. We’ll start with the timer that gets suggested the most online, and that is bigtimer.
Bigtimer
At first glance this node can look overwhelming and not for the faint hearted. For our purposes, we’re going to ignore 95% of what it can do. All we want it to do is switch on and off a simple light at certain times. Bigtimer doesn’t come included in Node-red, you will need to install it manually. You can do this by going to the hamburger icon in the top right of the screen and then > Manage Palette. Click on the Install tab, and paste the following in:
node-red-contrib-bigtimer
Once installed, you should be looking at the following. Let’s drag it out.
Here we’re going to set up a simple evening light. I want the lights to come on at sunset and turn off automatically at sunrise. As I am using sunset and not a specific time, I will need to include my latitude and longitude in order for the system to work out when sunset and sunrise occurs according to my location. For the actual switching side of things, I am only using the ON text and OFF text.
Lets create a flow that turns on a single lamp at sunset and turns it off at sunrise.
As we have already set the payload messages in bigtimer to be On/Off, in the switch node am using On and Off as the variables. For the service calls at the end, it’s a simple turn_on / turn_off of the entity switch.lamp.
If you look further into bigtimer you can see there are all sorts of additional extras you can add, including additional timers, random outputs (good for vacation lights to deter burglars), as well as offsets, i.e. maybe you need the lights to come on an hour before sunset or go off an hour after sunrise. Bigtimer is a topic in itself!
Time dependent sensor flows
Another type of timer that people may wish to use is one that determines whether a flow should be used or not. Again, you can use Bigtimer for such a flow, but I find it a little bit overkill in this instance. I prefer to use the much simpler Time range node.
An example where I would use this is in our motion sensor flow that we created in part 2. I.e. if the motion sensor triggers, the light comes on for 5 minutes before turning off. I would add this sensor to it, to amend the flow that the light would only come on between sunset and sunrise. i.e. to turn that flow into the perfect nightlight. I actually go through how to create a motion activated night light in detail here, so I’ll skip over a few of the steps in this post.
Here’s a flow showing two outputs. A flow that occurs between sunset and sunrise (our main goal) and also an optional flow that occurs during the day. Again check out my other post for full configuration options (including the need for a delay and to recheck for motion, so as to not turn the light off too early).
Delay Node
Another timer that we briefly covered was the delay node. I know some people prefer to use the trigger node after a certain period of time, I prefer to send the payload and then delay the outcome. It’s just simpler (in my mind) to configure – and fault find!
Here I’ve created a flow that encompasses pretty much everything that we’ve gone through so far. Imagine you want your child to go to bed, but they keep gaming. Here’s a flow which takes the single click input of the xiaomi aqara push button and issues a voice warning via google home that the monitor will be turned off in 30 minutes. I am controlling a pc display via IOTlink (more on that here). I could just as easily swapped out the MQTT “display off” command for a smart plug connected to an xbox or playstation. One thing I want to introduce though is the change node.
If you remember from part 2, the aqara push button sends out payloads like single, double, triple etc. This is good to capture the options, but in our case, we then need to modify the payload to move along the flow and to trigger the display to turn off. Therefore at some point we need to modify the payload from single to off for the MQTT command. See below:
The delay mode is self explanatory, and you know by now how to send an MQTT command. Check the IOTlink post for specifics on the topic.
I also introduced another function (TTS) to make Google Home announce the warning to the household, so no one is caught by surprise when the display goes blank.
You can see here for the full Data required to make google home announce…
Summary
In this post, I’ve showed you how to create a standard timer for evening lights which runs permanently in the background. We’ve also created a timer that takes it’s input from a sensor automatically, and we’ve also created a manual timer utilising all the nodes that we have learned so far. New nodes in this post included Bigtimer, the change node, and the time range node.
I hope you’ve found these mini tutorials interesting. If any of you have any feedback, or constructive criticism, I’d gladly listen. We’re in the process of starting a facebook group here so that we can show off solutions and cool tips and tricks, so if this is the sort of thing that would be of interest, feel free to sign up. It’s in its early stages, but hopefully it will continue to grow and inspire!