Node-RED as a rule/script engine for OpenHAB

rules
node-red
Tags: #<Tag:0x00007f6ce3a639c8> #<Tag:0x00007f6ce3a63838>

(Rgerrans) #41

How do you lookup a value of an OH item? Or would this require another node?


(Rgerrans) #42

To confirm 'openhab2-in' will now send a message whether there is a change in state or just an update (even if the state doesn't change)? Is that the channel2 Raw Event?

Thanks.


(Peter De Mangelaere) #43

Indeed, channel2 forwards all events (state, statechanged,, command, ...) on the selected item.


(Peter De Mangelaere) #44

Which values are you looking for ?


(Rgerrans) #45

Usually dimmer levels or on/off status of virtual switches. I saw on a blog where the Wemo node added the ability to do a lookup so suspected it was an explicit separate item.

Thanks


(Rgerrans) #46

@Peter_De_Mangelaere So I'm slowly coming up the Node-Red learning curve. I'm trying to figure out how to configure a switch to activate on a "Changed From" condition similar to the rules engine in OH. I can see from the Raw channel the following msg:

{ topic: "smarthome/items/Light_GF_Dinin…", payload: object, type: "ItemStateEvent" }
5/23/2017, 7:37:35 AMnode: Get Brightness
msg.payload : Object
object
topic: "smarthome/items/Light_GF_Dining_Main/statechanged"
payload: object
   type: "PercentType"
   value: "75"
   oldType: "PercentType"
   oldValue: "0"
type: "ItemStateChangedEvent"

I'm assuming I need my switch to be based on the oldValue == 0. How do I define the right msg property to do a switch comparison? I tried msg.payload.OldValue which didn't work. I did try putting a split in front of my switch and that seems to work. Is there a simpler way or did I stumble into the right approach?


(Artyom Syomushkin) #47

rgerrans, I don't see the reason why you should do this in Nodered, except analog values from sensors. You receive new message from OH only in the case if the value of item is changed, so there is no need for additional check. Just do, what is required.
Otherwise you just use Switch node with following condition


(Rgerrans) #48

@Artyom_Syomushkin I'm assuming you are referring to the lookup of a value. Sensors is one reason (i.e. I don't want my doors to lock if they are open), the other is in rooms with multiple dimmers, I have them match the dimmer value of the main light if it's already on. Granted I could capture and store those values in flow variables but easier would be just to look it up when I need it.

Thanks.


(Peter De Mangelaere) #49

Since the raw event is in the node-red msg's payload, I think msg.payload.payload.oldValue should be used.


(Artyom Syomushkin) #50

@rgerrans I don't think you can use old value for this. This compares just old value of same event, not with actual value of another switch or sensor. For this you have to store that value as flow variable.


(Rgerrans) #51

Perfect, that worked. Thanks. I'll have to do some research to understand the syntax of the variable.


(Rgerrans) #52

Thanks. I had two different issues I was trying to resolve, one was to trigger an event based on a change from an old value (for dimmers I found I have to pickup when it changes from 0 vs. being able to look for an "ON" event). That's what I needed the oldValue from the device and is resolve based on the new raw channel that Peter turned on with checking the msg.payload.payload.oldValue.

The second issue was polling a device to check it's current status to verify a sensor status (i.e. door open/closed to lock) or a dimmer value to match a newly turned on dimmer to. This is where I could use a flow variable but it means extra flows to capture and won't work if I get caught in a restart between events (I know, shouldn't happen very often once the system is stable and running)


(Mark Radbourne) #53

Just for interest, why is it that Node Red is popular? I wrote all my rules using plain node and MQTT. I've played with Node Red but it seems overly complex to perform actions such as "do this now and later do that". Has anybody managed to write that kind of action with Node Red?


(Artyom Syomushkin) #54

The beauty of Nodered is that you don't write, you draw and drag'n'drop. There is no need to know Javascript if you want to create such rules, as you say "do that when this happens and spmething else afterwards". I have made a tutorial of creating simple rule, like light switching on movement detection and illuminance sensor, which can be made by everyone without any programming. But unfortunatelly it's not yet in english and needs to be translated.


(Rgerrans) #55

@Peter_De_Mangelaere I realized this was still an open question as to if you would be able to add a node to do a lookup from an OH2 item?


(Rgerrans) #56

@Peter_De_Mangelaere If I set a Payload value for an Output Node and save it, the Payload value doesn't save.


(Jon Sands) #57

Started using the OH2 node red package and it's made node red integration so much easier, thanks @Peter_De_Mangelaere

However there's a couple small issues that had me BAMBOOZLED

1 - For item names in the in and out node list, it's using the sitemap name property, not the actual item name. So for the people getting a lot of Undefined in the items dropdown list, it's because your items don't have this. Simply add one to the item definition in the items file and it will show up properly, eg

Number CostAvg "Average Cost"

The in and out nodes will use the text in quotes for the name in the dropdown list. In OpenHab this sitemap name is completely optional, so a lot of us (especially those of us using other dashes like HabPanel) leave it out for a cleaner items file. I think it would be a good idea to have the Node Red package pull and display actual item names and not rely on this optional descriptor.

2 - The Out node completely explodes if you feed it a number and not a string. it actually stops my entire node red instance from responding until I kill the openhab service, then the node red web UI will respond again and I can remove the Out node. It seems when you feed in a number, OH2 replies with an error and the Out node doesn't do so good at handling it.

A lot of useful nodes (Smooth for instance) output the payload in number format. To get around this, I pass that into a function block with the simple code below, that will format it into a string that can then be succesfully passed to the OH2 Out node. I think it might be worth implementing this functionality into the out node directly

msg.payload = msg.payload.toString();
return msg;

Aside from that it's been a huge help!


(Peter De Mangelaere) #58

I posted an update to fix both issues. Please let me know if it works.


(Peter De Mangelaere) #59

I posted an update to fix this issue.


(Peter De Mangelaere) #60

Don't all values of 'Things' translate in different openhab items ... each with their 'state' ?
Anyway, it could be done by implementing a new node.
How would you envision to trigger the 'getting' of an items state/value/properties on demand ?