Node-RED as a rule/script engine for openHAB

Well, there might be, but for me this seems like a easy solution, I think you can find others.
Oh btw, this is the rules im using for sending notifications:

rule "Send Notification"
when
	Item stNotification received update
then
	sendBroadcastNotification(stNotification.state.toString)
end

We check after 10s please look the trigger node properties, if we recieve ON, we reset the trigger. We send OFF only 10 seconds after Fabien_Wifi is OFF. It’s like a check because if we recieve ON, the trigger is reset. Please test the flow I send in my previous message.
With 2 phone, I think it’s better to use a Group inside openhab with AND from the 2 phones. I’m using it in combination with motion sensors.

Thanks man, I misunderstanding the trigger node, I thought the trigger node when receive back to a ON msg means reset the counting time and then send OFF again… very stupid I know… :frowning:
Yeah your flow is way much better than me and it’s all built in node, thanks again man! You really helped a lot!

1 Like

Have been following the various threads of using Node-RED as the rule engine and have added onto my OpenHab Hub (Raspberry PI 3 using standard manual install). Now slowing moving my routines over to it as definitely find it easy to build and debug with Node-RED.

I was a programmer and still work in VB, Javascript, Python and abit of Java so the transition to using Javascript within Node-RED was relatively easy. Some observations to share so far.

What I like:
The building and debug is massively easier that using OpenHab as you can add injections and debug messages very quickly. Deploy, test and see results very quickly.

There are a huge number of Node-Red integrations which you can expand the base install with. From what I’ve read I think the OpenHabian team have been adding alot of these as default if you use OpenHabian.

The javascript object model (once you understand it - see below - makes it really easy to enrich the messages being sent between nodes.

What I don’t like
Basing this more on that I am familiar with Javascript. You need to have or build a good understanding of the Javascript object model. And as above once you get your head around this alot of possibilities open up.

The third party additions to the system require some learning to understand how they work. For example, the LIFX binding in Openhab makes it very easy to work with the lights, where as in Node-RED you need to learn how to make the correct calls to the LIFX API, also found that not all the new additions work. Will do a post next on this next for LIFX.

Lastly you need to get your head around event processing. I’ve seen a few posts in the threads that have added in nodes for boolean logic (AND/OR etc). These do work, but are shims on making Node-RED behave a way it’s not designed to do (nor intends to address). This is also a part of learning on Javascript events etc. Fully admit it took me ages to move from the mindset of Visual Basic sequential execution to the event driven of the paradigm of Javascript event handlers.

Overall I do like Node-RED and working on moving the presence detection routines provided by Rik over to it if I can. At the moment they work fine via OpenHAB and two systems work very well together. Don’t really have any intention of getting rid of OpenHAB as its very solid (and I’m on the edge release version). Will share shortly some of the work I’ve been doing in OpenHAB to help others who want to dabble in.

Final note to those who havent’ use Node-RED before and want to add it to your OpenHAB system is go slowly. Start with a couple of simple items which give you insight into how Node-RED works.

Hope the above helps.

1 Like

Setting a Scene with LIFX and Node-RED via Web API Call

Firstly you need to install LIFX node.

Please follow the instructions on this site to get your install registered with LIFX. The process is relatively straight simple to follow.

You will notice however that there is no ability to activate a predefined LIFX scene. The latest version does have a node to do this but I can’t currently make it work. That aside this post is still helpful in making calls to cloud API’s via Node-RED.

From the setup you need to gather your key. Be sure to record this as it is only given to you once. This is used later under the name of ‘Bearer’.

You will have also needed to have setup a LIFX scene via the various apps. I’ll expand this post if anyone is interested in how you get the UUID of this scene. From memory I think I had to do extra queries to get the list of scenes and UUID.

The flow looks like this

The start scene is just a simple injection.

The set token and headers is a function node and looks like this.

Set%20Token%20and%20Headers

I have cut off the bearer details as they are my own. There is a space there so it is like ‘Bearer xxxx’;

What we are doing here is setting up the msg.payload details to hold the duration of starting the scene and the headers to hold the Authorization key needed to call the scene via the LIFX Cloud API.

Next node is the actual web call.

The scene ID is the one I found for my own setup of ‘Warm House’.

Last node is just a debug which is all worked should return a JSON object showing the number of bulbs which have responded to the scene call.

Now using this technique to set a scene about 10 mins before sunset and feed in duration to gradually bring my lights on when it gets darker. That workflow involves a presence check which I’ll show shortly.

As mentioned the LIFX API node has been updated to include the ability to activate a scene, however when I feed it the correct selector details it did not work so staying with this process. Sure it is a couple of extra steps but also a handy bit of nodeCode to show how to call make a Web API call with headers etc.

Hope this is helpful for others.

Blending OpenHAB and Node-RED - Lights on if home.

Following on from my 2 other posts time to combine them and show an example.

Use Case
I’m at home at the sun is setting, I would like the lights to gradually come on as it becomes darker outside.

Node Flow

To make this work you need to install the big timer node.

I think this is included already within Open Habian, if not the install process is very easy.

Config of this node is:

Yes I live in the Harbour City of Sydney. Co-ordinates are general for Sydney.

Next node is the integration to OpenHAB and pulls the current details from my gPresent group. That bit is all built using this thread and work done by Rik and others.

The driver is a switch node

Present

Trick here is that the information we what from OpenHab is contained with the msg payload object. You need to reference its via

msg.payload.state

From there it’s just a matter of deciding what you want to do with it. In my setup if it is On i send out a call to LIFX (from notes about) to turn on a scene that takes 15 mins to build up. I also send some messages to pushover which I have nodes for.

That all it is, a great blend of some Open Hab routines and Node-RED.

Hope this helps others.

@ RGB73 - Cesare Arigliano - Unfortunately the link http://noderedguide.com/2016/05/17/lecture-7-dashboards-and-ui-techniques-for-node-red/ is broken.

Few weeks after my last post of using Node-RED and have more feedback.

Overall I’m more comfortable using Node-RED to build out all my automation using the Open HAB nodes.

Staying with OpenHAB as the core engine for the automation as it has a solid bus engine. What I really don’t like about OpenHAB is the rules and scripting engine. Fully understand that is a derivative of Java but overall I don’t think its a good one. I’ve spent time developing in many languages and confess my comfort is Basic, which lead me to Javascript, Python and also able to work in C++ Java. Still struggle with OpenHab thou.

Node-RED is a breath of fresh air here as its very visual and also provide out of the box the best debug testing tools. You can quickly build a workflow with lots of triggers and debug messages as you build out your logic.

I’m going to go all heritic here and ask the OpenHab team why continue with the scripting language we have and rather focus on integration into a scripting language like RedNode.

I thought tne same a while ago.
BUT node-red is event driven. How do you check for the current state of an item inside a function node. Nodes only accept 1 input…

I have moved everything I could into openhab.
There are nodes for RFLink that I still use to translate the mqtt message for openHAB (Not necessary but it make for easier reading code on the OH side)

I also use the ramp-thermostat nodes and the blind-controller nodes to drive the target temperatures according to schedule (Something that OH is poor at) and to drive the blinds according to the sun position and other factors.

That’s it

I had flows with dozens of nodes for quite simple rules because of the flow limitation. In OH these rules only have ten or so lines of code.

To get around the message-driven mechanism of NodeRED, you can use the context. (No need for function nodes, it also works with the switch and change nodes.)

I know. I thought I’d mention it before but I didn’t on purpose. It’s very cumbersome. It add nodes and function lines.
You have to remember to implement it every time a a value changes…
Rules rule.

Also. If you like python or Javascript you can use the rule engine for these languages in openHAB

Hi Guys

i have using red node to integrate the Broadlink S1 and mqtt

it works but i am sending a mqtt message at each second

could it be possible to send a mqtt message only on change?

Use the rbe node

1 Like

so simple and so powerful

thanks a lot!
you saved me a lot of time (again :slight_smile:
Merci

Great question on getting the current state of an item.

I have and use two approaches to this.

  1. For globally used items (such as Time of Day and Presence) I have a page of flow that stuff these into NodeRed global variables.
  2. For other flows that need to know the status of another openHAB item that is not global I use the query OpenHab node, then the next node is a change node that moves the state returned into another part of the msg. From there I can access that value within a function. If i need the value of the payload before the query to openHAB, again use a change node and move that value into the msg JSON structure.

This way you can collect as many values as you need from OpenHAB before running your function. And example from my system

Details of the setdavidPresent node.
image

Here I have moved the payload into msg.davidPresent, then gone out and gotten the luminance of the kitchen from openHab which will show up in the next node as
image

In my example I don’t feed these into a function, however you can access them all within a function by reference as
msg.payload.state (for my current luminance level) and
msg.davidPresent

There is a gothca with using the node to request information from OpenHab in that it is returned as msg.payload.state, vs msg.payload from a watcher node. Has caught me out a few times.

No argument that NodeRED creates more steps vs XCode! Will cover in new post shortly.

Hello all.

Been spending alot more time working with NodeRED and openHAB and have some feedback that might be helpful. Overall I’ve found it alot easier to quickly build new workflows vs XCode where I had to spend alot of time learning an almost new language that is very hard to debug and is very precise in syntax. I’ve been a programmer for years and have a few languages under my belt and XCode was hard.

I had been thinking of going the way of converting everything to NodeRED as some others have done, but found an annoying limitation being that you cannot query MQTT for the state of a device. It is possible by creating numerous NodeRed workflows that store device changes either in Context or Global. Just think this is huge amount of work given that OpenHAB provides a really rich set of connections to nearly everything and is very stable.

The NodeRED openhab nodes integrate really well and with abit of thought (see my post above) it is really easy to query openHAB, grab the status of a specific device, then use it as you need.

Whats not so good in NodeRED?
Alot of extra stuff on the screen! Yes it need alot of extra nodes onscreen to get things done. But its not a procedural written language and is visual and comparisons to formal programming languages is unjust. (think describing the color read to a simple visual indicator - which we all do on dashboards).

Visually complex, sure. But you can break it down into well named tabs, and also there is a comment node so you can document as well.

More I use it I think it is a hand in hand partnership with OpenHAB. OH2 is getting better and better with PaperUI to configure it all visually and then using NodeRED to build the rules opens the platform up to alot of new users really easily.

Final Thought
I’ve seen around the NodeRED world the requirement for a binary AND/OR node with multiple inputs and if you read the NodeRED team blog they are very against this. It is an event driven system and with abit of thought you don’t need these type of nodes. Great news for our community is we have the Nodes already available to allow this.

OpenHAB nodes and changing the URL of NodeRED.

I ran into an issue the other day where I was installing a new Node and it suggested I change the via setttings.js to make the system more secure. Confess I was not entire sure of the reason why but I did. After a full system restart (which also included an update to the latest dev version of OpenHAB) my nodes stopped working.

I logged an issue as had forgotten I had overlooked I had changed NodeRED so rolled back OpenHAB and the nodes still did not connect. I rolled back the change to NodeRED and all was well. Since then have done another refresh of OpenHAB and still working so it appears that the OpenHAB NodeRED Nodes don’t like the url being changed. Will open a GIT case for them.

Just an FYI team.

David

Hi!

I am trying to use your example for my rollershutter flow. I want to make use the temperature to say it is snowing or raining. So i took a switch-node zu say over or unser 0°C and then two change-nodes with yes or no as payload. I using the openhab-input-node with a netamo-sensor for the input. The switch-node ist using numbers payload. But it doesn’t work. I allways get No aus output.
So you have an idea, what i can do? The textual switches are working, only the one with numbers isn’t…

Jürgen

100% of my rules is running on node red. When I designed the system I decided to run every thing via MQTT and when ever I need to add a new “item” to the setup first I look if it can be done via MQTT.
The big advantage to MQTT is that you can subscribe to the MQTT topic from any system and use the data.So you don’t need to go through openhab to manipulate the data.
Here is a sample of one of the many rules I run


What it does:
There is a motion sensor in bedroom 1 ( All the other bedrooms as well):
From Sunset untill 9 PM it will allow the payload to go through , then it checks if the motion sensor is activated or not. If it is then it will switch the light on and will keep it on as long as there is motion in the bedroom other wise it will switch the light off after 2.5 min.
The auto light on can be disabled by turning the light off by the wall (when you go to bed) or if it is after 9 pm , in this case.
The Auto on will be reset every morning at 8 am but will only work after sunset. You can also enable or disable the auto on in Openhab


Once the setup was done for the first room you just need to duplicate for the other rooms and change the values of the room and update the items and sitemap.
You also can place the debug node along the line to check if all is working well.

As far as I could workout it will be quite difficult to setup this up in the rules of openhab.

1 Like