Node-RED as a rule/script engine for openHAB

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

Thanks for working on this. Two requests for additional functionality.

Is there a ability to do the same thing with a trigger node? I.e. Peter added a raw output to the triggers we, I then have to add a switch to process the oldValue and or value from the object payload in order to do a changed from/to. Just looking to save a step.

The other would be if you can trigger a sendBroadcastRequest from the REST API? I’ve setup a virtual item and a rule to send notifications from Node-RED flows but am looking to skip the extra step if I could just output straight to a notification node.

Ah, I was planning to make a pull request to add a setting to the input
node to only send a message when ItemStateChangedEvent occurs. Is this the
same as you are trying to accomplish? Only do something when the value of
an item changes, so not when it recieves an update.

Groeten,

Geert

Actually I’d like both ( hadn’t gotten to my “on update” rules yet so forgot that condition as well :slight_smile: though I definitely need access to the old and new values since I use “changed from 0” for triggering when dimmers turn on and I’ve had to use a “changed to” because I’m getting some weird false changed on some of my dimmers and have to filter them out until I can figure out if it’s a problem with OH and the REST api or something else going on.