{"id":484,"date":"2018-09-26T13:16:58","date_gmt":"2018-09-26T17:16:58","guid":{"rendered":"http:\/\/www.thesmarthomehookup.com\/test_install\/?p=484"},"modified":"2023-04-24T15:46:27","modified_gmt":"2023-04-24T19:46:27","slug":"node-red-pt-3-trigger-and-events-all-nodes-motion-lighting-and-actionable-notifications","status":"publish","type":"post","link":"http:\/\/www.thesmarthomehookup.com\/test_install\/node-red-pt-3-trigger-and-events-all-nodes-motion-lighting-and-actionable-notifications\/","title":{"rendered":"Node-RED Pt.3: Trigger and Events: All nodes. Motion Lighting and Actionable Notifications."},"content":{"rendered":"<p><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/tHGt4GeHWVE\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"\"><\/iframe><\/p>\n<p>Today on the hookup we\u2019re going to dive back into node-red and check out two extremely useful node types that you might not be using yet.<\/p>\n<p>This is the third installment of my mastering node-red series.&nbsp; If you haven\u2019t seen episode 1 where I describe the terminology and basic concepts of node-red, I\u2019d highly recommend you watch that video before this one.&nbsp; Today we\u2019re going to focus on two specific node types that have hundreds of applications:&nbsp; A built in node called the \u201ctrigger node\u201d, and a home assistant specific node called \u201cevents: all\u201d<\/p>\n<p>Node-RED is great for creating automations that respond immediately to changes in state in home assistant, but it isn\u2019t always easy to program it to do things later based on those changes.&nbsp; 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\u2019s not quite as easy to do that in node red.<\/p>\n<p>To accomplish this task in node-red, we\u2019re going to use the trigger node.&nbsp; The trigger node has three basic settings:<\/p>\n<p>the first setting is the message to immediately send.&nbsp; It has the same plethora of options for setting different message types as the change node.&nbsp; Alternatively, it can also just send on the current payload, or it can send nothing.<\/p>\n<p>The second setting is what to do after sending the immediate message.&nbsp; You\u2019ve got three options, one is to wait to be reset, which means it won\u2019t 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.&nbsp; Another option is to resend the same message every x amount of time until the node is reset using the same methods as before.&nbsp; The last option is the one I use the most, which is to send a different message after a specific amount of time.&nbsp; This message can be cancelled by resetting the node, again, using the same method as before.<\/p>\n<p>One common request for node-red flows was motion triggered lights.<\/p>\n<p>Here\u2019s a common sequence to accomplish this in node-red.&nbsp; We\u2019ll start out with a home assistant events state node for the motion detection, no special settings here.&nbsp; Then we\u2019ll bring it into a switch node that will route the message based on motion = true, or motion = false.&nbsp; 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.&nbsp; If motion is true it sends the message to turn the lights on using a home assistant call service node.&nbsp; Once the sensor sends motion = false message it enters this trigger node, which will initially send nothing.&nbsp; After a specific waiting period it will turn the light off using a call services node.&nbsp; The important thing here is that if at any time during the waiting period motion is detected again, it will send a message of \u201ctrue\u201d to the trigger node, which will reset the node, preventing the light from turning off.&nbsp; 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.<\/p>\n<p>Many many trigger nodes control my LED classroom bell timer.&nbsp; 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.&nbsp; 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.<\/p>\n<p>A similar sequence to the motion trigger controls my medicine reminder from last week\u2019s video.&nbsp; The medicine reminder input Boolean is on a schedule using one of my favorite add on nodes, the light scheduler node.&nbsp; 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.&nbsp; 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.<\/p>\n<p>Lets talk about notifications.<\/p>\n<p>If you\u2019re on android there are a few different options for notifications like HTML5 and pushbullet, but there are not any supported actionable notifications options yet.&nbsp; There are a few projects that are in development, but nothing in the official pipeline.<\/p>\n<p>iOS however, has an incredible home assistant app, it\u2019s honestly amazing, and it\u2019s free.&nbsp; I\u2019m 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.<\/p>\n<p>Setting up actionable notifications can be daunting, I covered the step by step instructions for doing this in my first node-red video.&nbsp; But the short version is that your ios component defines different classes for notifications, which controls which button press options you have.&nbsp; 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.<\/p>\n<p>If you\u2019re going to play around with this don\u2019t 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.<\/p>\n<p>From node red, sending an actionable notification is as easy as correctly formatting your json for the data of your call services node.&nbsp; 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\u2019ve also included the specific json for my notifications in the description.<\/p>\n<p>Sending the message is relatively easy, but what about receiving the message?&nbsp; Most people interact with home assistant in node-red by using the events: state node, but an actionable notification doesn\u2019t come through as a state change, so we\u2019ll need to use the big bad scary events: all node.&nbsp; This node basically outputs every single event that occurs in home assistant, and it does so as a json object.&nbsp; Whenever you\u2019re 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.<\/p>\n<p>When you click on the event in the debug window you\u2019ll 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.&nbsp; 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.&nbsp; 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.&nbsp; Instead of just checking msg.payload like we normally would in a switch node you\u2019ll add a period for every subfield in the message.&nbsp; For instance, the actionable notification response can be read by examining msg (dot) payload (dot) event (dot) actionName.<\/p>\n<p>Once you\u2019ve got the returned message you can use it to do virtually anything in home assistant.&nbsp; In my medication reminder automation I can toggle an input Boolean off, send another notification, or hold onto the message until I return home.<\/p>\n<p>The events: all node is extremely powerful because it gives you information about every single event that occurs in home assistant.&nbsp; This video was specifically about using it to capture actionable notification responses, but if you\u2019ve ever wanted to capture an obscure event from home assistant, this is how you do it.<\/p>\n<p>All of the flows and configuration files that I\u2019ve talked about in this video are down in the description.&nbsp; I do share all of my configuration.yaml file and all of my node-red flows with my patreon supporters.&nbsp; If you\u2019re interested in supporting my channel go ahead and check out my patreon page, or the other links in the description.&nbsp; I release a new home automation related video every Wednesday. If you enjoyed this video and you\u2019d like to see more like it, please consider subscribing.&nbsp; And as always, thanks for watching the hookup.<\/p>\n<p>Actionable Notification Flow: <a href=\"https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/actionable_notification.txt\">https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/actionable_notification.txt<\/a><\/p>\n<p>Actionable Notifcation Response Flow: <a href=\"https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/actionable_response.txt\">https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/actionable_response.txt<\/a><\/p>\n<p>Motion Triggered Lighting: <a href=\"https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/motion_trigger_flow.txt\">https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/motion_trigger_flow.txt<\/a><\/p>\n<p>Light Scheduler Node: <a href=\"https:\/\/www.npmjs.com\/package\/node-red-contrib-light-scheduler\">https:\/\/www.npmjs.com\/package\/node-red-contrib-light-scheduler<\/a><\/p>\n<p>Hold Node: <a href=\"https:\/\/flows.nodered.org\/node\/node-red-contrib-hold\">https:\/\/flows.nodered.org\/node\/node-red-contrib-hold<\/a><\/p>\n<p>Support my channel:<\/p>\n<p>Patreon:<\/p>\n<p><a href=\"https:\/\/www.patreon.com\/thehookup\">https:\/\/www.patreon.com\/thehookup<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Visit my website: <a href=\"http:\/\/www.thesmarthomehookup.com\/test_install\">http:\/\/www.thesmarthomehookup.com\/test_install<\/a><\/p>\n<p>Follow me on Twitter: @TheHookUp1<\/p>\n<p>Music by <a href=\"http:\/\/www.BenSound.com\">www.BenSound.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today on the hookup we\u2019re 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.&nbsp; If you haven\u2019t seen episode 1 where I describe the terminology and basic concepts of node-red, I\u2019d highly recommend [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2594,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-484","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"acf":[],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/484","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/comments?post=484"}],"version-history":[{"count":4,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/484\/revisions"}],"predecessor-version":[{"id":2349,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/484\/revisions\/2349"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media\/2594"}],"wp:attachment":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media?parent=484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/categories?post=484"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/tags?post=484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}