Node-RED as a rule/script engine for openHAB

Thanks for the updates, I’ll test today-

For the lookup node, my thought would to trigger it on an inbound message and then have it set the outbound message equal to the items state. I’m doing something similar now where I trigger a Change node to look up a global variable and send it to the next step in my flow (I’m looking up time-defined dim levels that I define in a different flow as a global variable and sending them to my dimmers when they are first turned on).

You could even have two outbound messages, one for the original inbound message and one for the lookup results.

Thanks!!

wow, talk about fast development! Will test tonight!

After testing I can confirm both issues are now fixed, it’s working great feeding it numbers, and also displaying all item names (no more undefined). Thanks so much!

I’ve also been working on a set of node red modules I’ve combined the work of Peter_De_Mangelaere and kwave (https://github.com/kwave/nodered-openhab).

  • I’ve used the bootstrap selector of kwave (disabled the multi select),
  • Added a openhab2 switch node. This node is a combination of a fetch node and the standard switch node. In the node you can select the item to retrieve and its value can directly be used for the switch statements.
  • Added a button to the input node, with a single and double click event listener. You can define payloads which you want to send on single or double click. This makes it easier to debug/prototype your rules in node-red, because you don’t have to wait for the event to occur.
  • Added status messages, the payload of the messages is displayed as the status message of the node. First it appears yellow and after two seconds is color is changed to grey. This makes it easier to spot the active node paths.

Under the hood I also made some changes. The eventsource listener is now only used within the server node, and the other nodes subscribe to the server node. If an openhab event occurs, the server node receives the event and then publishes it to the other nodes. I’ve done this because otherwise you would have a lot of nodes creating a connection to the same openhab server (I don’t know if thats a problem but it just didn’t feel natural/logic for me).

I would like to share this but there are already two modules on npm for openhab, should I make a third or is @Peter_De_Mangelaere willing to accept a lot of changes to his code?

Hi @Artyom_Syomushkin,

I would be interested to look at your tutorial. I can always use Google translate. I have a similar scenario in my garage. My rule (in node) effectively does this: If motion is detected and its not daylight and (the garage light is off or the garage light is on but has a time running to turn it off that will expire before this new timer) then turn on the garage light and schedule the garage light to be turned off in ten minutes (but if someone turns off the light then cancel that timer). This was hard enough in Node and I really don’t see how it will fit into the drag and drop flow that Node Red uses.

If I can accomplish that then I may well revisit Node Red.

I would be very interested in the additions you made but agree that a single code base would serve the community better. Hopefully @Peter_De_Mangelaere is open to incorporating other work.

Along those lines, since I’m not a coder anymore I’d be happy to help with some of the documentation with some of the tips & tricks I’ve worked out to replicate my old OH2 rules.

@markrad I don’t think that would be that difficult in Node-Red (though I’ve only been playing with it for about a week).

A suggested approach (didn’t test, just thinking the approach through) would be:

One flow to define a global variable of day/night (I do that using BigTimer).

Then in a separate flow:

  • Start with a trigger node based on your Motion

  • Feeds a Switch node to check that Motion On was received

  • Feeds a Switch node to check that flow variable for light state is OFF

  • Feeds a Switch node that checks the global daylight variable is NIGHT

  • Feeds a Trigger node (with Send Nothing wait for 10 minutes with extend delay if new message arrives then send OFF Reset the trigger if msg.payload equals OFF

  • Feeds a Change node to change msg.payload to OFF

  • Feeds your light output node

  • Add a separate trigger node for your light
    1st Feed:

  • Feeds a switch node to check that the light was turned OFF

  • Feeds your Trigger node above
    2nd Feed

  • Feed a Change node to define a flow variable for light state

Certainly willing to evaluate the changes for inclusion. Please let me know how I can access your changed codebase.
Fyi : the eventsource listener has been moved to the config node already some time ago, amongst bug changes.
And I posted an update (v1.0.2) yesterday which adds https and username/password options to the config node parameters.

Ok, I will fork your repository, look at the updated code, add my changes
and make a pull request.

Groeten,

Geert

Hey guys,
I’m personally not using NodeRED but I understand it’s popularity by many. My question is simple: Do you think it would be a worthy addition to openHABian and if so, which steps and settings (besides the normal installation) would you recommend?

@ThomDietrich I’m becoming a big fan of it over the native rules engine. I would suggest adding a couple of the contrib nodes out the gate. Obviously the OH node but the other one I’m getting a lot of use out of is the BigTimer node. I also think it would be helpful to put together some samples of how to replicate the same core rules functionality from OH’s rules (for example to replicate Change From requires an additional Switch Node programmed with the necessary logic).

Hey thanks for the answer! I’d setup NodeRED to the point where it is running and would probably be able to add some of these nodes :slight_smile: Samples, Tutorials and such should be available here in the forum. If not it’s about time to write a tutorial…
Could you give a few links to nodes and whatever relevant for me? Please don’t expect this to be implemented tomorrow, I’ll create a ticket and look into it soon.

Good suggestion. I’ll try to carve out some time over the next few weeks to start a tutorial thread for Node-Red.

Here you go:

Those are the only 3rd party nodes I’m currently using. Another one, if they want more complete Astro replacement: https://flows.nodered.org/node/node-red-contrib-sunevents

Once key tutorial for more complex rules is around functions: http://nodered.org/docs/writing-functions.html

That would be amazing! You can have a look at the InfluxDB+Grafana tutorial to get some ideas.

I’ve created an issue on NodeRED for openHABian, feel free to add whatever addition you feel noteworthy: Add NodeRED as optional component · Issue #146 · openhab/openhabian · GitHub

I will definitely second rgerrans recommendations for the obvious OH node, and also bigtimer. bigtimer is the go to node for anything involving times, dates, even sunset/sunrise. Another I’ve found incredibly useful is Smooth - lets you do rolling averages over an arbitrary number of samples with one click

I’m getting an error that I’m having trouble understanding and hope someone can point me in the right direction. I’m trying to update the status on a virtual switch.

6/3/2017, 8:46:40 AMnode: Home OpenHAB
msg : string[1011]
"response error '{"statusCode":400,"body":"<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n<title>Error 400 Bad Request</title>\n</head>\n<body><h2>HTTP ERROR 400</h2>\n<p>Problem accessing /rest/items/Security_Control. Reason:\n<pre> Bad Request</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>\n\n</body>\n</html>\n","headers":{"date":"Sat, 03 Jun 2017 14:46:40 GMT","content-type":"text/html; charset=ISO-8859-1","cache-control":"must-revalidate,no-cache,no-store","content-length":"315","connection":"close","server":"Jetty(9.2.19.v20160908)"},"request":{"uri":{"protocol":"http:","slashes":true,"auth":null,"host":"localhost:8080","port":"8080","hostname":"localhost","hash":null,"search":null,"query":null,"pathname":"/rest/items/Security_Control","path":"/rest/items/Security_Control","href":"http://localhost:8080/rest/items/Security_Control"},"method":"POST","headers":{"content-length":2}}}' on 'http://localhost:8080/rest/items/Securi..."
6/3/2017, 8:46:40 AMnode: c5b0ef06.139d7
msg.payload : IncomingMessage
"[object Object]"
6/3/2017, 8:46:59 AMnode: Home OpenHAB
msg : string[1011]
"response error '{"statusCode":400,"body":"<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n<title>Error 400 Bad Request</title>\n</head>\n<body><h2>HTTP ERROR 400</h2>\n<p>Problem accessing /rest/items/Security_Control. Reason:\n<pre> Bad Request</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>\n\n</body>\n</html>\n","headers":{"date":"Sat, 03 Jun 2017 14:46:58 GMT","content-type":"text/html; charset=ISO-8859-1","cache-control":"must-revalidate,no-cache,no-store","content-length":"315","connection":"close","server":"Jetty(9.2.19.v20160908)"},"request":{"uri":{"protocol":"http:","slashes":true,"auth":null,"host":"localhost:8080","port":"8080","hostname":"localhost","hash":null,"search":null,"query":null,"pathname":"/rest/items/Security_Control","path":"/rest/items/Security_Control","href":"http://localhost:8080/rest/items/Security_Control"},"method":"POST","headers":{"content-length":3}}}' on 'http://localhost:8080/rest/items/Securi..."
6/3/2017, 8:46:59 AMnode: c5b0ef06.139d7
msg.payload : IncomingMessage
"[object Object]"

@Peter_De_Mangelaere I think it might be a bug. I’ve verified that my virtual switch is a number Item (might that be the issue since the node is sending a string?); I’ve sent the msg as a string and as a number, I’ve tried using the node payload overide; I’ve also tried all topic overrides for both input and payload override.

Let me know anything else I can do on my side to help troubleshoot.

Thanks for the template.

@rgerrans I created a simple OH Item with type “Number” using PaperUI and have no issues updating its value from an openhab2-out node using ‘ItemCommand’ and ‘ItemUpdate’. Or did I miss something ?

No, that’s the same thing I was doing.

Something weird is definitely going on. I verified that I’m changing the msg.payload to a number and feeding that number to the item, but this is what I’m seeing in my Openhab logs:

2017-06-04 16:54:02.768 [WARN ] [thome.io.rest.core.item.ItemResource] - Received HTTP POST request at 'items/Security_Control' with an invalid status value 'OFF'.
2017-06-04 16:54:05.823 [WARN ] [thome.io.rest.core.item.ItemResource] - Received HTTP POST request at 'items/Security_Control' with an invalid status value 'ON'.
2017-06-04 16:56:14.397 [WARN ] [thome.io.rest.core.item.ItemResource] - Received HTTP POST request at 'items/Security_Control' with an invalid status value 'OFF'.
2017-06-04 16:56:17.462 [WARN ] [thome.io.rest.core.item.ItemResource] - Received HTTP POST request at 'items/Security_Control' with an invalid status value 'ON'.
2017-06-04 16:58:12.921 [WARN ] [thome.io.rest.core.item.ItemResource] - Received HTTP POST request at 'items/Security_Control' with an invalid status value 'OFF'.
2017-06-04 16:58:17.800 [WARN ] [thome.io.rest.core.item.ItemResource] - Received HTTP POST request at 'items/Security_Control' with an invalid status value 'ON'.

No sure why it’s sending an on/off to it from Node-RED. Here is the error debug from the Openhab monitor in Node-RED:

6/4/2017, 4:58:16 PMnode: Home OpenHAB
msg : string[1011]
"response error '{"statusCode":400,"body":"<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n<title>Error 400 Bad Request</title>\n</head>\n<body><h2>HTTP ERROR 400</h2>\n<p>Problem accessing /rest/items/Security_Control. Reason:\n<pre> Bad Request</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>\n\n</body>\n</html>\n","headers":{"date":"Sun, 04 Jun 2017 22:58:17 GMT","content-type":"text/html; charset=ISO-8859-1","cache-control":"must-revalidate,no-cache,no-store","content-length":"315","connection":"close","server":"Jetty(9.2.19.v20160908)"},"request":{"uri":{"protocol":"http:","slashes":true,"auth":null,"host":"localhost:8080","port":"8080","hostname":"localhost","hash":null,"search":null,"query":null,"pathname":"/rest/items/Security_Control","path":"/rest/items/Security_Control","href":"http://localhost:8080/rest/items/Security_Control"},"method":"POST","headers":{"content-length":2}}}' on 'http://localhost:8080/rest/items/Securi..."

I verified that no other flows are inputting to this item. Any ideas on how to troubleshoot further?