Node-RED as Alternative Rule Engine

Definitely looks like an issue there. I’d still suggest trying the text file route as another option in the meantime.

Can you tell me how to check for 2 states?
Like if time == 10:00 and Movement == True then Do Stuff?

Assuming your two states are coming from the same trigger node, easiest is just two sequential switch nodes. There is an optional Match node you can install that allows you to do it in a single node. I tend to do the former so it’s easily visible to me when I glance at my flow and I can throw a debug node on each one separately if I’m having issues.

Like this, but this does not work.
I want to check IF time is correct AND movment is True THEN switch stuff on

You will need to use a flow or global variable to define your time since you don’t have a method to go back to Bigtimer and ask if it’s that time.

For mine I have a bunch of time triggered events and a number of checks like yours where if something triggers I only want action during a time frame. So I just set up a single flow to define all my time triggers / time states. For states I used global variables:

So in this case dayMode is defined as a global variable. Then I just have a switch case that checks the dayMode variable after I checked if I achieved the trigger state:

In this case my trigger is linked to an outside temperature I get from a WeatherUnderground node. Then if temperature is either above a certain range in the summer or below in the winter I run a day mode check against my global dayMode variable to make sure its during the day before I turn on the switch for the fan/heater.

Might be a little too much info but thought that might help to see a flow example

I found something strange again.
Which is really sad, because there are AND / OR Logic Gate nodes for Node-Red which need a Topic.
Listens to state changes of a selected openHAB Item.


BUT! If you look into the Debug log of the outgoing MSG there is no topic.
The bottom Node with “MyTopic” is a simple injection to compare a msg.payload with a topic.

Thats because your debug node is set to only show msg.payload. Set your debug to msg.topic or choose full object and you’ll see the additional info available to you (sorry, switched to mqtt so don’t remember exactly all the data the openhab-in node passes)

Edit: saw that you grabbed the info screenshot for the in node. You can put in a change node to move msg.item to msg.topic ahead of your logic check.

1 Like

@rgerrans Thank you SO MUCH you reduced my Rule from

TO

2 Likes

Happy to help!! Node-RED is pretty powerful once you get into it and start figuring out some of the tricks

1 Like

Could you please share the code for your “day/ time triggers / time states”?
The ones on the photo, determining if it is day/night/evening, early/evening etc…
I would really need something like that :slight_smile:
Thanks!

No problem. This uses the BigTimer node:

[{"id":"c45af9b0.53c638","type":"comment","z":"864e4169.fd187","name":"Set Time of Day | Brightness","info":"","x":156.66664505004883,"y":184.00002098083496,"wires":[]},{"id":"61bf26de.c159b8","type":"bigtimer","z":"864e4169.fd187","outtopic":"","outpayload1":"OVERNIGHT","outpayload2":"","name":"Overnight","lat":"39.7388","lon":"-104.4083","starttime":"0","endtime":"300","startoff":"0","endoff":0,"offs":0,"outtext1":"","outtext2":"","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"suspend":false,"random":false,"repeat":true,"atstart":true,"x":159.11106872558594,"y":278.16669845581055,"wires":[["3ec73a0b.fbe2b6"],[],[]]},{"id":"fc914835.8a0988","type":"bigtimer","z":"864e4169.fd187","outtopic":"","outpayload1":"PREDAWN","outpayload2":"","name":"Predawn","lat":"39.7388","lon":"-104.4083","starttime":"300","endtime":"5003","startoff":"0","endoff":0,"offs":0,"outtext1":"","outtext2":"","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"suspend":false,"random":false,"repeat":true,"atstart":false,"x":162.55552291870117,"y":350.5555953979492,"wires":[["3ec73a0b.fbe2b6"],[],[]]},{"id":"688d5da0.d98254","type":"bigtimer","z":"864e4169.fd187","outtopic":"","outpayload1":"EARLYMORNING","outpayload2":"","name":"Early Morning","lat":"39.7388","lon":"-104.4083","starttime":"5003","endtime":"10030","startoff":"0","endoff":0,"offs":0,"outtext1":"","outtext2":"","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"suspend":false,"random":false,"repeat":true,"atstart":false,"x":143.55552291870117,"y":414.5555953979492,"wires":[["3ec73a0b.fbe2b6"],[],[]]},{"id":"d2b91ab3.9a2928","type":"bigtimer","z":"864e4169.fd187","outtopic":"","outpayload1":"MORNING","outpayload2":"","name":"Morning","lat":"39.7388","lon":"-104.4083","starttime":"5003","endtime":"480","startoff":"30","endoff":0,"offs":0,"outtext1":"","outtext2":"","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"suspend":false,"random":false,"repeat":true,"atstart":false,"x":157.55552291870117,"y":476.5555953979492,"wires":[["3ec73a0b.fbe2b6"],[],[]]},{"id":"d039dca9.0943e","type":"bigtimer","z":"864e4169.fd187","outtopic":"","outpayload1":"DAY","outpayload2":"","name":"Day","lat":"39.7388","lon":"-104.4083","starttime":"480","endtime":"1020","startoff":"0","endoff":"0","offs":0,"outtext1":"","outtext2":"","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"suspend":false,"random":false,"repeat":true,"atstart":false,"x":164.55552291870117,"y":538.5555953979492,"wires":[["3ec73a0b.fbe2b6"],[],[]]},{"id":"9d32cecc.e26ef","type":"bigtimer","z":"864e4169.fd187","outtopic":"","outpayload1":"EARLYEVENING","outpayload2":"","name":"Early Evening","lat":"39.7388","lon":"-104.4083","starttime":"5004","endtime":"5004","startoff":"-30","endoff":"0","offs":0,"outtext1":"","outtext2":"","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"suspend":false,"random":false,"repeat":true,"atstart":false,"x":134.3872413635254,"y":677.4373073577881,"wires":[["3ec73a0b.fbe2b6"],[],[]]},{"id":"96c18988.8c7378","type":"bigtimer","z":"864e4169.fd187","outtopic":"","outpayload1":"NIGHT","outpayload2":"","name":"Night","lat":"39.7388","lon":"-104.4083","starttime":"1320","endtime":"0","startoff":"0","endoff":"-1","offs":0,"outtext1":"","outtext2":"","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"suspend":false,"random":false,"repeat":true,"atstart":false,"x":167.6313362121582,"y":811.2854614257812,"wires":[["3ec73a0b.fbe2b6"],[],[]]},{"id":"3ec73a0b.fbe2b6","type":"rbe","z":"864e4169.fd187","name":"","func":"rbe","gap":"","start":"","inout":"out","x":506.5555229187012,"y":474.5555953979492,"wires":[["468281e5.68642","1d9ab6c.bfaa549","d151189a.32ff38","81eeb71e.387a48","a3eed122.99881"]]},{"id":"468281e5.68642","type":"change","z":"864e4169.fd187","name":"Define Brightness","rules":[{"t":"change","p":"payload","pt":"msg","from":"OVERNIGHT","fromt":"str","to":"15","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"PREDAWN","fromt":"str","to":"30","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"EARLYMORNING","fromt":"str","to":"45","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"MORNING","fromt":"str","to":"60","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"DAY","fromt":"str","to":"100","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"AFTERNOON","fromt":"str","to":"100","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"EARLYEVENING","fromt":"str","to":"80","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"EVENING","fromt":"str","to":"60","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"NIGHT","fromt":"str","to":"45","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":698.5555419921875,"y":422.55560302734375,"wires":[["73b2b60f.766a28"]]},{"id":"1d9ab6c.bfaa549","type":"switch","z":"864e4169.fd187","name":"Day Mode","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"DAY","vt":"str"},{"t":"eq","v":"NIGHT","vt":"str"}],"checkall":"false","outputs":2,"x":695.5555229187012,"y":508.5555953979492,"wires":[["8bd48998.e8c2a8"],["8bd48998.e8c2a8"]]},{"id":"d151189a.32ff38","type":"change","z":"864e4169.fd187","name":"Sun Mode","rules":[{"t":"change","p":"payload","pt":"msg","from":"EARLYMORNING","fromt":"str","to":"DAY","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"EVENING","fromt":"str","to":"NIGHT","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":696.5555229187012,"y":554.5555953979492,"wires":[["ec198d17.24b15"]]},{"id":"73b2b60f.766a28","type":"change","z":"864e4169.fd187","name":"Set Brightness","rules":[{"t":"set","p":"dbrightness","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1083.5975341796875,"y":424.18359375,"wires":[[]]},{"id":"81eeb71e.387a48","type":"change","z":"864e4169.fd187","name":"Set Time of Day","rules":[{"t":"set","p":"timeOfDay","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1087.8374633789062,"y":471.3825988769531,"wires":[[]]},{"id":"a3eed122.99881","type":"link out","z":"864e4169.fd187","name":"Time of Day Change","links":["9af5cc96.c2cbd","cfa23785.6aaa78","4a5c0149.deb94","973e5ede.1789b","a79a4c47.9e235","4cd5da16.4ecd54"],"x":654.8475151062012,"y":686.4936141967773,"wires":[]},{"id":"bdc8bfc7.1eb36","type":"bigtimer","z":"864e4169.fd187","outtopic":"","outpayload1":"EVENING","outpayload2":"","name":" Evening","lat":"39.7388","lon":"-104.4083","starttime":"5004","endtime":"1320","startoff":"0","endoff":"0","offs":0,"outtext1":"","outtext2":"","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"suspend":false,"random":false,"repeat":true,"atstart":false,"x":148.96030807495117,"y":738.7908229827881,"wires":[["3ec73a0b.fbe2b6"],[],[]]},{"id":"8c6e86f.717ee78","type":"bigtimer","z":"864e4169.fd187","outtopic":"","outpayload1":"AFTERNOON","outpayload2":"","name":"Afternoon","lat":"39.7388","lon":"-104.4083","starttime":"1020","endtime":"5004","startoff":"0","endoff":"-30","offs":0,"outtext1":"","outtext2":"","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":false,"feb":false,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":false,"dec":false,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"suspend":false,"random":false,"repeat":true,"atstart":false,"x":144.66664505004883,"y":611.000020980835,"wires":[["3ec73a0b.fbe2b6"],[],[]]},{"id":"8bd48998.e8c2a8","type":"change","z":"864e4169.fd187","name":"Set Day Mode","rules":[{"t":"set","p":"dayMode","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1090,"y":511,"wires":[[]]},{"id":"ec198d17.24b15","type":"change","z":"864e4169.fd187","name":"Sun Mode","rules":[{"t":"set","p":"sunMode","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1079,"y":552,"wires":[[]]}]

I then use either the link node you’ll see in the flow to trigger events at change, or Switch nodes like below to check status.

Time of Day Check:

[{"id":"c23b1ee9.60e33","type":"switch","z":"864e4169.fd187","name":"TOD Check","property":"timeOfDay","propertyType":"msg","rules":[{"t":"eq","v":"OVERNIGHT","vt":"str"},{"t":"eq","v":"PREDAWN","vt":"str"},{"t":"eq","v":"EARLYMORNING","vt":"str"},{"t":"eq","v":"MORNING","vt":"str"},{"t":"eq","v":"DAY","vt":"str"},{"t":"eq","v":"AFTERNOON","vt":"str"},{"t":"eq","v":"EARLYEVENING","vt":"str"},{"t":"eq","v":"EVENING","vt":"str"},{"t":"eq","v":"NIGHT","vt":"str"}],"checkall":"false","outputs":9,"x":1630,"y":460,"wires":[[],[],[],[],[],[],[],[],[]]}]

Day Mode Check:

[{"id":"25f3877d.807228","type":"switch","z":"864e4169.fd187","name":"Day Mode Check","property":"dayMode","propertyType":"global","rules":[{"t":"eq","v":"DAY","vt":"str"},{"t":"eq","v":"NIGHT","vt":"str"}],"checkall":"true","outputs":2,"x":1419.2500228881836,"y":559.7500076293945,"wires":[[],[]]}]
1 Like

Thank you, I will look into it soon

Updated the tutorial to add a reference to a nodes I just learned about, norelite, which look very promising for making home automation flows easier:

I also just found another set of contributed nodes to help with home automation, norelite - https://flows.nodered.org/node/norelite I will start experimenting with them and likely update this tutorial to incorporate their functionality in place of some of my workarounds (like stored variables).

@rgerrans I’m in the process of adding your tutorial to the list of selected tutorials here: http://docs.openhab.org/tutorials

How do you feel about renaming the thread to state something other than “Replacement”, I personally feel its to harsh and both systems can be used in parallel, nothing needs to be replaced. How about “Alternative”? While you are at it you could remove the redundant “Tutorial”.

No problem at all.

1 Like

Thanks! :wink:

1 Like

What would you guys suggest for notification system, node-red as alternative or fully internally in openhab?
I am thinking about something like “if time>23 and priority<critical, then send info to mobile1 and mobile2, and tablet1 and tv1”, and then complicate it from there, like if people are home then announce on voice, if people are sleeping then just send email, if this when then that…

It could get a bit complex if one wants it to, that is why I am considering node-red to handle all the logic as well, maybe let the openhab do the actual communication based on node-red rules to avoid redundant accounts etc. donno
Tips? :slight_smile:

I’m overdue to add my notification system to the tutorial. I use Node-Red for the logic and email-based notifications, and OpenHAB for other notifications (via broadcast to app and voice to audio sink). For the OH notifications, I do it by defining notification string variable items in OH, populating them in Node-RED and then using OH rules to trigger the notifications when the variables are updated.

Items:

String  vNotification
String vSpeech

OH Rules:

rule "Send Notification"
when
	Item vNotification received command
then
	sendBroadcastNotification(vNotification.state.toString)
end

rule "Send Speech"
when
	Item vSpeech received update
then
    say(vSpeech.state.toString)
end

I’ll try to update my tutorial with all the details this week.

1 Like

Updated Finally carved out some time to add a Notifications section to the main flow and comment on how I integrated with MQTT:

MQTT

I have fully migrated my deployment over to MQTT using the built in MQTT nodes (MQTT Archives - Node RED Programming Guide) You just need to replace any input / ouput nodes in the below example with MQTT nodes. The one item of note is you can’t (or at least I haven’t figured out how to) request a status from the MQTT broker like you can in the OpenHAB nodes. Instead you have to store any state information in a Node-RED flow or global variable like I defined below.

Notifications

While Node-RED has some addon Nodes to help support different types of notifications, I wanted to still be able to leverage some of the built in functionality of OpenHAB as well. This required setting up necessary OpenHAB items and rules to interfact into OpenHAB’s functionality.

Broadcast Notifications
Through the OpenHAB Cloud Connector http://docs.openhab.org/addons/io/openhabcloud/readme.html you are able to use the mobile apps to both access your system and receive broadcast notifications http://www.myopenhab.org/. To integrate this into Node-RED requires an item to carry the notification information and a rule to trigger the notification.

Item Definition:

String  vNotification  "Notification [%s]"`

Notifications.rules:

rule "Send Notification"
when
	Item vNotification received update
then
	sendBroadcastNotification(vNotification.state.toString)
end

And finally a Node-RED flow to send the notification. Here I used a change node to create my text message:
image
image

You can also use the template node to include other payloads or topics into your notification message:
image
image

If you want to add a time stamp to your message I was able to put together the following to create a plain language time stamp (i.e. 3:32 pm)


For cut-and-paste purposes:

msg.payload =  msg.payload + new Date().toLocaleTimeString('en-US',{timeZoneName: 'short'});
return msg;

Audio & Voice
OpenHAB has the ability to play audio files and convert text-to-speech: http://docs.openhab.org/configuration/multimedia.html. To take advantage of this integration we follow a similar approach as we did for the Broadcast Notifications above using a combination of a custom item and a rule combined with a Node-RED flow.

First to trigger a text-to-voice (assuming you have already setup everything in OpenHAB as per above).

Item Definition:

String vSpeech

Speech.rules:

rule “Send Speech”
when
Item vSpeech received update
then
say(vSpeech.state.toString)
end

And finally a NodeRed flow to tie it together.
image

I went a step further with mine to control the volume levels during and after sending speech (turning it up while the speech is broadcast and turning it back down). So I created a sub-flow (https://github.com/node-red/node-red/wiki/Design:-subflows
image
image

Similarly for sending an Audio file saved to your conf/sounds directory:

Item Definition:

String vSound

Sound.rules:

rule "Send Sound"
when
	Item vSound received update
then
    playSound(vSound.state.toString)
end

And Node-RED flow:
image
image

Email
For email you can choose to use the SendMail binding http://docs.openhab.org/addons/actions/mail/readme.html or alternatively you can do it directly from Node-RED using the email node https://flows.nodered.org/node/node-red-node-email. The one trick I did apply here is I didn’t want to configure each node instance with my smtp server information and I wanted the ability to turn on and off the ability to send email (so I didn’t spam my wife when I was testing) so instead created an email subflow and a control flow.

Email Subflow:
image
image
image

Control Flow:
image
image
image

1 Like

Thank you for the notifications flow, I like the usage of sub-flows to make things more reusable. I am thinking on extending it to have different priority levels sent with the message and then in the subflow have the logic that will determine what will be sent, where and when. (some messages I want to see immediately on the phone, some maybe once a day on email, some should be announced etc).

You said that “I have fully migrated my deployment over to MQTT using the built in MQTT nodes” - is this just to be independent of specific binding and have it all via “pure” mqtt, or do you have some other motives as well? :slight_smile: