Node-RED Pt.3: Trigger and Events: All nodes. Motion Lighting and Actionable Notifications.
Today on the hookup we’re going to dive back into node-red and check out two extremely useful node types that you might not be using yet.
This is the third installment of my mastering node-red series. If you haven’t seen episode 1 where I describe the terminology and basic concepts of node-red, I’d highly recommend you watch that video before this one. Today we’re going to focus on two specific node types that have hundreds of applications: A built in node called the “trigger node”, and a home assistant specific node called “events: all”
Node-RED is great for creating automations that respond immediately to changes in state in home assistant, but it isn’t always easy to program it to do things later based on those changes. In the home assistant automation system there is an option to say that an entity needs to have been on for a specific amount of time before your automation should be triggered, but It’s not quite as easy to do that in node red.
To accomplish this task in node-red, we’re going to use the trigger node. The trigger node has three basic settings:
the first setting is the message to immediately send. It has the same plethora of options for setting different message types as the change node. Alternatively, it can also just send on the current payload, or it can send nothing.
The second setting is what to do after sending the immediate message. You’ve got three options, one is to wait to be reset, which means it won’t send another message, no matter how many messages enter this node until it gets a specific payload of msg.reset, or a msg.payload that you choose. Another option is to resend the same message every x amount of time until the node is reset using the same methods as before. The last option is the one I use the most, which is to send a different message after a specific amount of time. This message can be cancelled by resetting the node, again, using the same method as before.
One common request for node-red flows was motion triggered lights.
Here’s a common sequence to accomplish this in node-red. We’ll start out with a home assistant events state node for the motion detection, no special settings here. Then we’ll bring it into a switch node that will route the message based on motion = true, or motion = false. My motion detector it set up as a sensor instead of a binary sensor, so you may need to change this to on or off. If motion is true it sends the message to turn the lights on using a home assistant call service node. Once the sensor sends motion = false message it enters this trigger node, which will initially send nothing. After a specific waiting period it will turn the light off using a call services node. The important thing here is that if at any time during the waiting period motion is detected again, it will send a message of “true” to the trigger node, which will reset the node, preventing the light from turning off. This means that the light will turn on immediately when there is motion, but it will only turn off when no motion has been detected for a specific amount of time.
Many many trigger nodes control my LED classroom bell timer. Each one sends a payload for the specific number of minutes for the current period, and then after that many minutes it sends on a payload of next, which feeds into a switch node to activate the next trigger node which does the same thing for a different amount of time. The nice thing about using a trigger node for this is if the administration decides to throw us a curve ball and make a last minute custom schedule I can send a custom timer through home assistant and it will clear the other trigger nodes which clears the previous schedule.
A similar sequence to the motion trigger controls my medicine reminder from last week’s video. The medicine reminder input Boolean is on a schedule using one of my favorite add on nodes, the light scheduler node. Once the medicine reminder input Boolean kicks on it starts this trigger node, which initially sends nothing, then waits 30 minutes before sending an actionable iOS notification. If at any time the input Boolean gets flipped off (usually by pressing the amazon dash button), it will reset that trigger node preventing the notification from being sent.
Lets talk about notifications.
If you’re on android there are a few different options for notifications like HTML5 and pushbullet, but there are not any supported actionable notifications options yet. There are a few projects that are in development, but nothing in the official pipeline.
iOS however, has an incredible home assistant app, it’s honestly amazing, and it’s free. I’m not a huge fan of apple products, and I would love to jump ship especially after they failed to put out a reasonably sized phone with their last announcement, but the home assistant iOS app keeps me from leaving.
Setting up actionable notifications can be daunting, I covered the step by step instructions for doing this in my first node-red video. But the short version is that your ios component defines different classes for notifications, which controls which button press options you have. The main identifier is used to send that specific notification class to your phone, and then subsequent identifiers are the message that will be sent to home assistant when you press those buttons on the notification.
If you’re going to play around with this don’t forget that you need to update the configuration, then reboot home assistant as usual, and then go to the iOS app and update your push notification settings.
From node red, sending an actionable notification is as easy as correctly formatting your json for the data of your call services node. You can easily use a yaml to json converter to do this if you already have actionable notifications set up in home assistant, but I’ve also included the specific json for my notifications in the description.
Sending the message is relatively easy, but what about receiving the message? Most people interact with home assistant in node-red by using the events: state node, but an actionable notification doesn’t come through as a state change, so we’ll need to use the big bad scary events: all node. This node basically outputs every single event that occurs in home assistant, and it does so as a json object. Whenever you’re using this node I highly recommend that you first pipe it into a debug node, then use the trashcan icon to clear the debug window, then perform your action so you can see what event data is generated.
When you click on the event in the debug window you’ll see that there are lots of different data fields, and the good news is that these data fields are really easy to read in node red. You can see that when I send my medicine reminder notification it has a field called payload, then a subfield called event, then another subfield called actionName, and in the actionName field it sends the identifier from the ios push notification. By setting up a switch node we can filter our events: all messages for only the ones that have the specific actionNames that we want. Instead of just checking msg.payload like we normally would in a switch node you’ll add a period for every subfield in the message. For instance, the actionable notification response can be read by examining msg (dot) payload (dot) event (dot) actionName.
Once you’ve got the returned message you can use it to do virtually anything in home assistant. In my medication reminder automation I can toggle an input Boolean off, send another notification, or hold onto the message until I return home.
The events: all node is extremely powerful because it gives you information about every single event that occurs in home assistant. This video was specifically about using it to capture actionable notification responses, but if you’ve ever wanted to capture an obscure event from home assistant, this is how you do it.
All of the flows and configuration files that I’ve talked about in this video are down in the description. I do share all of my configuration.yaml file and all of my node-red flows with my patreon supporters. If you’re interested in supporting my channel go ahead and check out my patreon page, or the other links in the description. I release a new home automation related video every Wednesday. If you enjoyed this video and you’d like to see more like it, please consider subscribing. And as always, thanks for watching the hookup.
Actionable Notification Flow: https://github.com/thehookup/Node-RED-Examples/blob/master/actionable_notification.txt
Actionable Notifcation Response Flow: https://github.com/thehookup/Node-RED-Examples/blob/master/actionable_response.txt
Motion Triggered Lighting: https://github.com/thehookup/Node-RED-Examples/blob/master/motion_trigger_flow.txt
Light Scheduler Node: https://www.npmjs.com/package/node-red-contrib-light-scheduler
Hold Node: https://flows.nodered.org/node/node-red-contrib-hold
Support my channel:
Visit my website: https://www.thesmarthomehookup.com
Follow me on Twitter: @TheHookUp1
Music by www.BenSound.com