Node-RED as a rule/script engine for openHAB

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 ?

Can you send me a screenshot of the full flow you are building with these components (from input to output) ?

Thanks. Here’s the actual flow since it’s not too big:

[{"id":"581e9f1c.830f7","type":"openhab2-in","z":"a0348b2c.c67e88","name":"Security System Ready","controller":"7892a38d.64783c","itemname":"Security_Partition_Ready","x":133,"y":424,"wires":[["3b74ba14.0300e6"],[]]},{"id":"91376316.95e19","type":"openhab2-in","z":"a0348b2c.c67e88","name":"Security System Armed","controller":"7892a38d.64783c","itemname":"Security_Partition_Armed","x":130,"y":499,"wires":[["66c6cc20.02e624"],[]]},{"id":"c104f539.f58668","type":"openhab2-in","z":"a0348b2c.c67e88","name":"Security System Armed Stay","controller":"7892a38d.64783c","itemname":"Security_Partition_Stay","x":149,"y":567,"wires":[["6a79ffd3.b297d"],[]]},{"id":"9b15ca99.69ae88","type":"change","z":"a0348b2c.c67e88","name":"System Ready Status","rules":[{"t":"set","p":"systemReady","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":604.4963455200195,"y":413.701021194458,"wires":[["ad5c346e.d69748"]]},{"id":"ad5c346e.d69748","type":"switch","z":"a0348b2c.c67e88","name":"System Ready Status","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"OFF","vt":"str"},{"t":"eq","v":"ON","vt":"str"}],"checkall":"true","outputs":2,"x":858.0866546630859,"y":408.74991512298584,"wires":[["dc28e25f.cda31"],["7814192e.38f888"]]},{"id":"13de23d2.535b7c","type":"change","z":"a0348b2c.c67e88","name":"System Stay Status","rules":[{"t":"set","p":"systemStay","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":592.0639724731445,"y":557.085018157959,"wires":[["ae75e292.f6a7e"]]},{"id":"c9564fe7.badcd","type":"change","z":"a0348b2c.c67e88","name":"System Armed Status","rules":[{"t":"set","p":"systemArmed","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":611.5377044677734,"y":490.05193519592285,"wires":[["7814192e.38f888"]]},{"id":"7814192e.38f888","type":"switch","z":"a0348b2c.c67e88","name":"System Armed Status","property":"systemArmed","propertyType":"flow","rules":[{"t":"eq","v":"OFF","vt":"str"},{"t":"eq","v":"ON","vt":"str"}],"checkall":"true","outputs":2,"x":865.8128662109375,"y":483.22599029541016,"wires":[["f57eecef.54704"],["ae75e292.f6a7e"]]},{"id":"f6de054a.006fc8","type":"openhab2-out","z":"a0348b2c.c67e88","name":"Security Control","controller":"7892a38d.64783c","itemname":"Security_Control","topic":"ItemUpdate","payload":"","x":1381.487419128418,"y":497.42331409454346,"wires":[]},{"id":"ae75e292.f6a7e","type":"switch","z":"a0348b2c.c67e88","name":"System Stay Status","property":"systemStay","propertyType":"flow","rules":[{"t":"eq","v":"OFF","vt":"str"},{"t":"eq","v":"ON","vt":"str"}],"checkall":"true","outputs":2,"x":867.7197875976562,"y":555.3284301757812,"wires":[["a78deb.a0de2218"],["ebe26896.0a1cd8"]]},{"id":"3b74ba14.0300e6","type":"switch","z":"a0348b2c.c67e88","name":"Check","property":"payload","propertyType":"msg","rules":[{"t":"neq","v":"systemReady","vt":"flow"}],"checkall":"true","outputs":1,"x":354.06296157836914,"y":418.14478302001953,"wires":[["9b15ca99.69ae88"]]},{"id":"66c6cc20.02e624","type":"switch","z":"a0348b2c.c67e88","name":"Check","property":"payload","propertyType":"msg","rules":[{"t":"neq","v":"systemArmed","vt":"flow"}],"checkall":"true","outputs":1,"x":358.6548843383789,"y":491.74392318725586,"wires":[["c9564fe7.badcd"]]},{"id":"6a79ffd3.b297d","type":"switch","z":"a0348b2c.c67e88","name":"Check","property":"payload","propertyType":"msg","rules":[{"t":"neq","v":"systemStay","vt":"flow"}],"checkall":"true","outputs":1,"x":373.5247917175293,"y":560.9623775482178,"wires":[["13de23d2.535b7c"]]},{"id":"f57eecef.54704","type":"change","z":"a0348b2c.c67e88","name":"Ready","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1147.1484375,"y":469.5540466308594,"wires":[["f6de054a.006fc8"]]},{"id":"dc28e25f.cda31","type":"change","z":"a0348b2c.c67e88","name":"Not Ready","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1167.118637084961,"y":408.4246587753296,"wires":[["f6de054a.006fc8"]]},{"id":"a78deb.a0de2218","type":"change","z":"a0348b2c.c67e88","name":"Armed","rules":[{"t":"set","p":"payload","pt":"msg","to":"2","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1148.8619689941406,"y":521.7760009765625,"wires":[["f6de054a.006fc8"]]},{"id":"ebe26896.0a1cd8","type":"change","z":"a0348b2c.c67e88","name":"Armed (Stay)","rules":[{"t":"set","p":"payload","pt":"msg","to":"3","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1164.3748779296875,"y":566.697998046875,"wires":[["f6de054a.006fc8"]]},{"id":"7892a38d.64783c","type":"openhab2-controller","z":"","name":"Home OpenHAB","host":"localhost","port":"8080","path":"","username":"","password":""}]

Here you go:

[{"id":"71212da1.f34434","type":"openhab2-in","z":"5cea3d68.97d634","name":"Front Door Lock","controller":"7892a38d.64783c","itemname":"Lock_GF_Living","x":243.75000381469727,"y":236.25000381469727,"wires":[["9ef1413.07dbbc"],[]]},{"id":"9ef1413.07dbbc","type":"switch","z":"5cea3d68.97d634","name":"Door Locked","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"ON","vt":"str"}],"checkall":"true","outputs":1,"x":473.75000762939453,"y":231.25000381469727,"wires":[["4524935c.37cecc"]]},{"id":"4524935c.37cecc","type":"openhab2-get","z":"5cea3d68.97d634","name":"Patio Door Sensor","controller":"7892a38d.64783c","itemname":"Sensor_GF_KitchenNook_Door","x":711.25,"y":230,"wires":[["ef5374a4.97a6e8"]]},{"id":"ef5374a4.97a6e8","type":"switch","z":"5cea3d68.97d634","name":"Door Closed","property":"payload.State","propertyType":"msg","rules":[{"t":"eq","v":"CLOSED","vt":"str"}],"checkall":"true","outputs":1,"x":921.2500152587891,"y":228.75000381469727,"wires":[["e577ed32.4871a"]]},{"id":"e577ed32.4871a","type":"change","z":"5cea3d68.97d634","name":"Lock Door","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1103.75,"y":227.5,"wires":[["4eb642dd.3f398c"]]},{"id":"4eb642dd.3f398c","type":"openhab2-out","z":"5cea3d68.97d634","name":"Patio Door Lock","controller":"7892a38d.64783c","itemname":"Lock_GF_KitchenNook","topic":"ItemCommand","payload":"","x":1322.5000190734863,"y":227.50001335144043,"wires":[]},{"id":"7892a38d.64783c","type":"openhab2-controller","z":"","name":"Home OpenHAB","host":"localhost","port":"8080","path":"","username":"","password":""}]

Just curios to the benefits if I decided to migrate to a direct mqtt connection vs. using the node-red-contrib-openhab2 using the REST API? The contrib node makes it pretty easy and straight forward but I didn’t know if going straight to a mqtt bus integration might make it faster / more reliable?

@Peter_De_Mangelaere Just a heads up that it appears that with the recent code changes you now have to explicitly set the topic on the Openhab-out node to either ItemCommand or ItemUpdate otherwise the commands don’t get passed on to the items.

This helped a lot. I issued an update with the (hopefully :slight_smile: )fix.

Thanks Peter. That seems to have fixed the problem

(ignore my bad news response if it got emailed to you, that may have been user error :wink:

Last night I looked at the code of @Peter_De_Mangelaere, everythinks looks really nice. I added two branches, one for the inject button and one for the switch. Tonight I will try to have another look at the changes I made and make two pull requests.

1 Like

OK, I’ve added the two pull requests. Now @Peter_De_Mangelaere can have a look at the changes and hopefully soon merge them.

1 Like