Node-RED as a rule/script engine for openHAB

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?

This could happen if you send the values 0 and 1.
I updated the node (version 1.0.3) to not convert 0/1 to ‘OFF’/‘ON’.

@geertbongers and @rgerrans : I added an openhab2-get node in version 1.0.4.
It gets the configured Item object from openhab, puts in the payload of the incoming message and outputs the result on its output channel.

Ok, here is v1 of a compatability tutorial for Node-RED to replace the Openhab rules engine. I still have pieces to add but it’s a start. Let me know if it looks like I messed anything up or missed key pieces (like the new get nodes that Peter just deployed) - Node-RED as Alternative Rule Engine

I’m still getting the same issues with 1.0.3 installed where I’m getting the OFF/ON errors in my OH logs. I put a debug on the messages going to the out node and they are definitely coming across as numbers (0-3). I’ve tried them as both strings and number message types with the same outcome.

6/5/2017, 10:58:56 AMnode: Security Control
msg.payload : string[1]
"1"
6/5/2017, 10:58:56 AMnode: Home OpenHAB
msg : string[740]
"response error '{"statusCode":400,"body":"{\n \"error\": {\n \"message\": \"State could not be parsed: ON\",\n \"http-code\": 400\n }\n}","headers":{"date":"Mon, 05 Jun 2017 16:58:58 GMT","content-type":"application/json","content-length":"89","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/state","path":"/rest/items/Security_Control/state","href":"http://localhost:8080/rest/items/Security_Control/state"},"method":"PUT","headers":{"content-length":2}}}' on 'http://localhost:8080/rest/items/Security_Control/state'"

2017-06-05 10:58:58.662 [WARN ] [thome.io.rest.core.item.ItemResource] - Received HTTP PUT request at 'items/Security_Control/state' with an invalid status value 'ON'.

Great, thanks!!. I’ll check it out later today/tonight.

Edit. That works, thanks. For anyone else giving it a spin, you do have to specify to use msg.payload.state in your next node to evaluate / use the value.

@Peter_De_Mangelaere Did you get a chance to look at geertbongers code?

I liked his integration of the fetch with a standard switch since it would save me the extra step of tying the fetch node to a switch node.

Thanks again for all the hard work. I’ve gotten almost all my rules over and this was the one piece I needed.

I can’t believe that I missed this post…

Just a clarification: This (node-red-contrib-openhab2) is using the REST API (http protocol), not the MQTT-Event Bus method (MQTT protocol) to link Node-RED to OH2… correct?

I am asking because the thread starts by describing an MQTT based “link” to Node-RED and somewhere in the middle, the node-red-contrib-openhab2 pops up…
(silently installing Node-RED in the background…)

Indeed, node-red-contrib-openhab2 is using the http REST API and Server-Sent Events (also http) to directly communicate with openhab.

1 Like

Could you create a minimal flow which causes the same problem and export it, so I can import and test ?