Node-RED as Alternative Rule Engine

rules
node-red
Tags: #<Tag:0x00007f51ecc119f8> #<Tag:0x00007f51ecc10d78>

(Scantineau) #143

Isn’t it a bit overkill to use a global variable ? I think he should use a flow variable.

@hermann1514 when you say “is pressed” does it means that it is when you press the switch that you want to check if light intensity is > 39 ?

If YES : take a look at my flow : Node-RED as Alternative Rule Engine
You will see how to get another item state in the same flow. Just don’t forget to store previous values

If NO : I think I understand what you mean and I can show you a flow but I’m not at home now. :wink:


(Hermann Mueller) #144

Hello,

for example, I have 2 devices: A motion detector and a Türkontakt.Wenn the motion detector, a brightness of less than 39 and the door contact is open, the light should come on. There should be a possibility in Nodered that if two or more states are true or have a certain value that the light is switched. Excuse me. I’m still pretty new to NodeRed …

And…do you have some Screenshots :wink:

Thx.


(Scantineau) #145

Try this

[{"id":"14824bc.042abb4","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"7d67af5c.419b7","type":"switch","z":"14824bc.042abb4","name":"","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"yourMotionItem","vt":"str"},{"t":"cont","v":"yourContactItem","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":290,"y":200,"wires":[["9b43469b.160dd8"],["fd027f8a.69699"]]},{"id":"9b43469b.160dd8","type":"switch","z":"14824bc.042abb4","name":"","property":"payload.value","propertyType":"msg","rules":[{"t":"neq","v":"yourMotionItem","vt":"flow"}],"checkall":"true","repair":false,"outputs":1,"x":490,"y":160,"wires":[["2a52616f.aa4bae"]]},{"id":"2a52616f.aa4bae","type":"change","z":"14824bc.042abb4","name":"","rules":[{"t":"set","p":"yourMotionItem","pt":"flow","to":"(\t    if (payload.value < 39)\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":160,"wires":[["58ebd238.f8f04c"]]},{"id":"62ccf864.2e4878","type":"change","z":"14824bc.042abb4","name":"","rules":[{"t":"set","p":"yourContactItem","pt":"flow","to":"(\t    if (payload.value != \"ON\")\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":240,"wires":[["58ebd238.f8f04c"]]},{"id":"fd027f8a.69699","type":"switch","z":"14824bc.042abb4","name":"","property":"payload.value","propertyType":"msg","rules":[{"t":"neq","v":"yourContactItem","vt":"flow"}],"checkall":"true","repair":false,"outputs":1,"x":490,"y":240,"wires":[["62ccf864.2e4878"]]},{"id":"58ebd238.f8f04c","type":"switch","z":"14824bc.042abb4","name":"","property":"(\t    if (flow.yourMotionItem and flow.yourContactItem )\t)","propertyType":"jsonata","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":990,"y":200,"wires":[["d2898229.8cb84"],["1944ac1c.950b14"]]},{"id":"d2898229.8cb84","type":"openhab2-out","z":"14824bc.042abb4","name":"","controller":"","itemname":"","topic":"ItemCommand","payload":"ON","x":1200,"y":160,"wires":[]},{"id":"1944ac1c.950b14","type":"openhab2-out","z":"14824bc.042abb4","name":"","controller":"","itemname":"","topic":"ItemCommand","payload":"OFF","x":1200,"y":240,"wires":[]},{"id":"5016f9a0.819e38","type":"comment","z":"14824bc.042abb4","name":"filter your items","info":"","x":280,"y":360,"wires":[]},{"id":"63b3cba.393f634","type":"comment","z":"14824bc.042abb4","name":"has it changed ?","info":"","x":520,"y":360,"wires":[]},{"id":"30252ce8.def2e4","type":"comment","z":"14824bc.042abb4","name":"Save value","info":"","x":720,"y":360,"wires":[]},{"id":"ba94b11c.17e83","type":"comment","z":"14824bc.042abb4","name":"Are the two values set ?","info":"","x":980,"y":360,"wires":[]},{"id":"bc7c0ea6.01904","type":"comment","z":"14824bc.042abb4","name":"Switch the light OFF","info":"","x":1390,"y":240,"wires":[]},{"id":"ae8c4b8b.fbb178","type":"comment","z":"14824bc.042abb4","name":"Switch the light ON","info":"","x":1390,"y":160,"wires":[]},{"id":"42c25e00.f297b","type":"openhab2-events","z":"14824bc.042abb4","name":"","controller":"","itemname":"","x":100,"y":200,"wires":[["7d67af5c.419b7"]]}]

(Hermann Mueller) #146

Hi. thanks. But i receive an error on the Switch “set flow.dummy3”…

Can you help?


(Scantineau) #147

Yep sorry I should have test before…

Juste type in : payload.value != “ON”

Same for the other one and the last switch too…


(Hermann Mueller) #148

Hi.

Sorry, but I think I’m too stupid for that. Can you even send me the complete version - and maybe test it before :wink:
I can not do it.

Thx.


(Branden Smale) #149

You will not get help here by asking others to do it for you. We will offer help, and you should try…if you need more help, we will help, but again, we will not do.


(Scantineau) #150

I could give you a complete answer (TBH I already have it in my flows because I already test it before you asked me to do it).
But I won’t really help you if I give you the complete solution !

Try to understand how it works, what is asked to you for a node to work properly. If you do not try yourself, you will always be dependant of our help.

Another help : import this node as an example

[{"id":"7a4c924.bf1d06c","type":"change","z":"9d27bbb4.fb2c68","name":"","rules":[{"t":"set","p":"test1","pt":"flow","to":"payload.value != \"ON\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":160,"wires":[["18e8880a.37ceb8","a777a8f1.e3a2d8"]]}]

Ps : don’t forget the proverb “give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime”


(Hermann Mueller) #151

Hello.
It was not my intention to let you do my work. I’ve really spent a few hours on the example and could not find the problem - since I thought this example works.
I will continue to play around with it - eventually it will work out well.

Thanks to scantineau for your help and suggestions.

THx.
Regards
Hermann


(Hermann Mueller) #152

Hi, after a long tinkering … I have now created the following flow. This seems to work so far. Is this way correct or can you optimize something here?

Here is my flow:

[{"id":"fcb208d5.874228","type":"change","z":"796b9f56.81c11","name":"","rules":[{"t":"set","p":"dummy","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":742,"y":450,"wires":[["cf82923c.d01488"]]},{"id":"cd658113.728ed8","type":"switch","z":"796b9f56.81c11","name":"brightness_flow","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"49","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":458,"y":480,"wires":[["fcb208d5.874228"],["a87776ec.8c31c8"]]},{"id":"9583570f.064978","type":"change","z":"796b9f56.81c11","name":"","rules":[{"t":"set","p":"dummy2","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":749,"y":611,"wires":[["cf82923c.d01488"]]},{"id":"ef26b4b0.410e08","type":"switch","z":"796b9f56.81c11","name":"switch_flow","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"ON","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":455,"y":627,"wires":[["9583570f.064978"],["b7803c80.a03978"]]},{"id":"cf82923c.d01488","type":"switch","z":"796b9f56.81c11","name":"True or not","property":"$flowContext('dummy') and $flowContext('dummy2')","propertyType":"jsonata","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1026,"y":545,"wires":[["844b802d.d92dc"],["8c962719.dbbb"]]},{"id":"a87776ec.8c31c8","type":"change","z":"796b9f56.81c11","name":"","rules":[{"t":"set","p":"dummy","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":742,"y":495,"wires":[["cf82923c.d01488"]]},{"id":"b7803c80.a03978","type":"change","z":"796b9f56.81c11","name":"","rules":[{"t":"set","p":"dummy2","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":748,"y":652,"wires":[["cf82923c.d01488"]]},{"id":"844b802d.d92dc","type":"openhab2-out","z":"796b9f56.81c11","name":"","controller":"","itemname":"dummy2","topic":"ItemCommand","payload":"ON","x":1276,"y":503,"wires":[]},{"id":"8c962719.dbbb","type":"openhab2-out","z":"796b9f56.81c11","name":"","controller":"","itemname":"dummy2","topic":"ItemCommand","payload":"OFF","x":1281,"y":591,"wires":[]},{"id":"1312a074.33c36","type":"openhab2-in","z":"796b9f56.81c11","name":"","controller":"","itemname":"dummy","x":89,"y":635,"wires":[["ef26b4b0.410e08"],[]]},{"id":"12194930.c2dbc7","type":"openhab2-in","z":"796b9f56.81c11","name":"","controller":"","itemname":"Stube_Bewegungsmelder_Brightness","x":187,"y":486,"wires":[["cd658113.728ed8"],[]]}]

`


(Paul F Prinsloo) #153

Hello Herman

Here is a quick flow of what I think you are looking for. It should also help with global variables and how you could use them.

[{"id":"64fc8843.0090d8","type":"inject","z":"dfe7ca33.da9238","name":"Light Meter 155","topic":"","payload":"155","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":90,"wires":[["13a14a3c.640bb6"]]},{"id":"13a14a3c.640bb6","type":"function","z":"dfe7ca33.da9238","name":"Global Set Light Meter Value","func":"/*You will set the value from your light meter as a global varilabe as you can then\nuse it where ever you want to next*/\nglobal.set('light_sensor',msg.payload);\nreturn msg;\n","outputs":1,"noerr":0,"x":420,"y":130,"wires":[["86cf4ffa.f9ce3"]]},{"id":"212e7134.d9086e","type":"inject","z":"dfe7ca33.da9238","name":"SWITCH","topic":"","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":230,"wires":[["84bac1ec.df4b2"]]},{"id":"84bac1ec.df4b2","type":"function","z":"dfe7ca33.da9238","name":"Check if you need to switch the light or not. ","func":"var dat0 = global.get('light_sensor'); // Get the value from global\nif(dat0 < 90){\nvar msg1 = {payload:\"ON\"}; // or what ever the payload is that you need to switch on the light\n}else { \nvar msg2 = {payload:\"To bright for Auto\"};\n}    \nreturn [msg1,msg2];","outputs":2,"noerr":0,"x":470,"y":230,"wires":[["e9038f74.671e"],["4b0d406.629afc"]],"outputLabels":["payload to the switch","debug info"]},{"id":"e9038f74.671e","type":"debug","z":"dfe7ca33.da9238","name":"Light Switch","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":750,"y":180,"wires":[]},{"id":"4b0d406.629afc","type":"debug","z":"dfe7ca33.da9238","name":"Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":730,"y":250,"wires":[]},{"id":"fe65569c.cfb378","type":"inject","z":"dfe7ca33.da9238","name":"Light Meter 91","topic":"","payload":"91","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":130,"wires":[["13a14a3c.640bb6"]]},{"id":"300e0eb1.8e0b92","type":"inject","z":"dfe7ca33.da9238","name":"Light Meter 80","topic":"","payload":"80","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":170,"wires":[["13a14a3c.640bb6"]]},{"id":"86cf4ffa.f9ce3","type":"debug","z":"dfe7ca33.da9238","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":660,"y":130,"wires":[]}]

Hope it helps


(Scantineau) #154

Hi Paul,

Why a global variable and not a flow variable ?


(Paul F Prinsloo) #155

I prefer global as you can use the value on other functions as well.


(Scantineau) #156

If the function is in the same flow, it should work :thinking:


(Paul F Prinsloo) #157

Definitely it will all depend what and how you would like to use the data. In the case of the light meter value I would also use it in another flow so then I only have to declare it once.


(Scantineau) #158

If you use it in another flow : OK


(Paul F Prinsloo) #159

:+1:


(Evert Hoegee) #160

Good afternoon,

Just got some time to play with node red and openhab.

I have and rgb mi-light, I can turn it of afther 15sec but I also want to change the collor:

From the debug I got the following:

  
8/9/2018, 11:48:40 AMnode: d732bd01.a018b
msg.payload : Object
object
link: "http://localhost:8080/rest/items/milight_rgbLed_ACCF23506D72_1_ledcolor"
state: "47,100,100"
editable: false
type: "Color"
name: "milight_rgbLed_ACCF23506D72_1_ledcolor"
label: "Color"
category: "ColorLight"
tags: array[0]
groupNames: array[0]

is it not possible to change this? (editable: false)
Thank you in advance,

Evert


(Scantineau) #161

Hi !
Why don’t you just send the new colour as new state ?


(Evert Hoegee) #162

HI,

Not Shure how to do that, can you give an example?
For the “On/Off” this is possible, but not for the color.

Thank you for the feedback!!

@can we startup a thread for node Red qwestions?