[Solved] Struggling with msg comparaison

#1

Hello floks,
I discoverd node red a week ago and it's seems perfect for my home control dream with my low programation knowledge...

I'm trying to control a gpio from my raspberry to start and stop at a certain time I select in the dashboard (time picker)

My code is not pretty but it's kinda working...I just cannot succed to compare the actual time with my inputs.
The problem is for me in the "in time?" function node
Two days in, I'm lossing my mind, HELP!

[{"id":"b8b61556.49e7d8","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"b71d6cc.4e7329","type":"schedex","z":"b8b61556.49e7d8","name":"lamp controle","suspended":true,"lat":"","lon":"","ontime":"","ontopic":"","onpayload":"","onoffset":0,"onrandomoffset":0,"offtime":"","offtopic":"","offpayload":"","offoffset":0,"offrandomoffset":0,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"sun":true,"x":900,"y":240,"wires":[["c8a83827.bf0f78","63d075cc.a7b9cc"]]},{"id":"93d7789e.07b5b8","type":"debug","z":"b8b61556.49e7d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":870,"y":200,"wires":[]},{"id":"f29d19ce.b31a48","type":"ui_text_input","z":"b8b61556.49e7d8","name":"","label":"Stop time","tooltip":"","group":"841f7e06.0ded2","order":7,"width":"3","height":"1","passthru":true,"mode":"time","delay":300,"topic":"","x":100,"y":280,"wires":[["55153aa0.682414"]]},{"id":"23c81448.09b38c","type":"ui_text_input","z":"b8b61556.49e7d8","name":"","label":"Go time","tooltip":"","group":"841f7e06.0ded2","order":6,"width":"3","height":"1","passthru":true,"mode":"time","delay":300,"topic":"","x":80,"y":240,"wires":[["510fa072.b6068"]]},{"id":"510fa072.b6068","type":"function","z":"b8b61556.49e7d8","name":"format time","func":"msg.play = parseFloat(msg.payload/1000);\n\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":240,"wires":[["13b18663.1228ba","9ddeee45.7ad7d"]]},{"id":"55153aa0.682414","type":"function","z":"b8b61556.49e7d8","name":"format time","func":"msg.stop = parseFloat(msg.payload/1000);\n\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":280,"wires":[["13b18663.1228ba","9ddeee45.7ad7d"]]},{"id":"13b18663.1228ba","type":"function","z":"b8b61556.49e7d8","name":"in time?","func":"var timeOn = msg.play;\nvar timeOff = msg.stop;\nvar timeNow = msg.time;\n\n    if(timeNow>=timeOn && timeNow<=timeOff)\n\t\t{\n\t\t\t msg.payload = 'on';\n\t\t}\n    else\n        {\n\t\t\tmsg.payload = 'off';\n\t\t}\n    \n\t\nreturn msg;","outputs":1,"noerr":0,"x":700,"y":240,"wires":[["b71d6cc.4e7329","93d7789e.07b5b8"]]},{"id":"14789a0a.dae326","type":"inject","z":"b8b61556.49e7d8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":80,"y":320,"wires":[["814d1cab.34185"]]},{"id":"a44c7ba9.311188","type":"moment","z":"b8b61556.49e7d8","name":"seconds","topic":"","input":"payload","inputType":"msg","inTz":"Europe/Brussels","adjAmount":0,"adjType":"days","adjDir":"add","format":"ss","locale":"fr_BE","output":"second","outputType":"msg","outTz":"Europe/Brussels","x":240,"y":400,"wires":[["7a5c378b.29edc8"]]},{"id":"7177f4a4.9845ac","type":"moment","z":"b8b61556.49e7d8","name":"minutes","topic":"","input":"payload","inputType":"msg","inTz":"Europe/Brussels","adjAmount":0,"adjType":"days","adjDir":"add","format":"mm","locale":"fr_BE","output":"minute","outputType":"msg","outTz":"Europe/Brussels","x":240,"y":360,"wires":[["a44c7ba9.311188"]]},{"id":"814d1cab.34185","type":"moment","z":"b8b61556.49e7d8","name":"hours","topic":"","input":"payload","inputType":"msg","inTz":"Europe/Brussels","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH","locale":"fr_BE","output":"hour","outputType":"msg","outTz":"Europe/Brussels","x":230,"y":320,"wires":[["7177f4a4.9845ac"]]},{"id":"6e2ec02e.4b667","type":"function","z":"b8b61556.49e7d8","name":"m","func":"msg.minute = parseFloat(msg.minute*60);\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":360,"wires":[["3d329590.722a9a"]]},{"id":"7a5c378b.29edc8","type":"function","z":"b8b61556.49e7d8","name":"h","func":"msg.hour = parseFloat(msg.hour*3600);\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":320,"wires":[["6e2ec02e.4b667"]]},{"id":"3d329590.722a9a","type":"function","z":"b8b61556.49e7d8","name":"s","func":"msg.second = parseFloat(msg.second);\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":400,"wires":[["e64c5706.085fa8"]]},{"id":"e64c5706.085fa8","type":"function","z":"b8b61556.49e7d8","name":"sum","func":"msg.time = msg.second+msg.minute+msg.hour;\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":400,"wires":[["13b18663.1228ba","f69f47cc.7dcc58"]]},{"id":"c8a83827.bf0f78","type":"debug","z":"b8b61556.49e7d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1110,"y":280,"wires":[]},{"id":"63d075cc.a7b9cc","type":"rpi-gpio out","z":"b8b61556.49e7d8","name":"","pin":"32","set":"","level":"0","freq":"","out":"out","x":1120,"y":240,"wires":[]},{"id":"9ddeee45.7ad7d","type":"debug","z":"b8b61556.49e7d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":690,"y":200,"wires":[]},{"id":"f69f47cc.7dcc58","type":"debug","z":"b8b61556.49e7d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":690,"y":400,"wires":[]},{"id":"841f7e06.0ded2","type":"ui_group","z":"","name":"Croissance","tab":"2fb138d2.a45978","order":2,"disp":true,"width":"6","collapse":false},{"id":"2fb138d2.a45978","type":"ui_tab","z":"","name":"Photopériode","icon":"dashboard","order":3,"disabled":false,"hidden":false}]

Thanks

0 Likes

#2

Welcome to the (addictive) world of Node-red!

First off I would combine your four function nodes h, m, s, and sum they can all go into one function to declutter your flow.

Second, you have three nodes sending data to the in time? node. These come at different times so not all the data may be correct. I would suggest that you have the Go time' and the 'Stop time functions send there output to a change node where you store the values in a flow variable (see: https://nodered.org/docs/user-guide/context)

Now when the inject node sends in the timestamp, the in time? function node can grab the flow variables and do the calculations.

0 Likes

#3

Yeah, thanks a lot zenofmud!
Its alive :smiley:
Here is the result :

[{"id":"850c7085.8eb5a","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"35924edb.fec082","type":"ui_text_input","z":"850c7085.8eb5a","name":"","label":"Stop time","tooltip":"","group":"841f7e06.0ded2","order":7,"width":"3","height":"1","passthru":true,"mode":"time","delay":300,"topic":"","x":100,"y":220,"wires":[["5c04adb0.5af3e4"]]},{"id":"d27a6d64.fca9b","type":"ui_text_input","z":"850c7085.8eb5a","name":"","label":"Go time","tooltip":"","group":"841f7e06.0ded2","order":6,"width":"3","height":"1","passthru":true,"mode":"time","delay":300,"topic":"","x":100,"y":180,"wires":[["59a5f97d.41ac08"]]},{"id":"59a5f97d.41ac08","type":"function","z":"850c7085.8eb5a","name":"go time","func":"msg.play = parseFloat(msg.payload/1000);\n\nreturn msg;","outputs":1,"noerr":0,"x":240,"y":180,"wires":[["1bfc7e27.2be4e2"]]},{"id":"5c04adb0.5af3e4","type":"function","z":"850c7085.8eb5a","name":"stop time","func":"msg.stop = parseFloat(msg.payload/1000);\n\nreturn msg;","outputs":1,"noerr":0,"x":240,"y":220,"wires":[["7c09abfa.f50154"]]},{"id":"e33253cd.42d96","type":"function","z":"850c7085.8eb5a","name":"time compare","func":"var timeOn = flow.get('timeOn');\nvar timeOff = flow.get('timeOff');\nvar hour = parseFloat(msg.hour*3600);\nvar minute = parseFloat(msg.minute*60);\nvar timeNow = minute+hour;\n\n    if(timeNow>=timeOn && timeNow<=timeOff)\n\t\t{\n\t\t\t msg.payload = 1;\n\t\t}\n    else\n        {\n\t\t\tmsg.payload = 0;\n        }\n\t\nreturn msg;","outputs":1,"noerr":0,"x":520,"y":260,"wires":[["af5bb1fd.d01d7"]]},{"id":"8913226c.c1f43","type":"inject","z":"850c7085.8eb5a","name":"","topic":"","payload":"","payloadType":"date","repeat":"60","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":260,"wires":[["6bf93d5a.2604a4"]]},{"id":"4e228248.b96b2c","type":"moment","z":"850c7085.8eb5a","name":"mm","topic":"","input":"payload","inputType":"msg","inTz":"Europe/Brussels","adjAmount":0,"adjType":"days","adjDir":"add","format":"mm","locale":"fr_BE","output":"minute","outputType":"msg","outTz":"Europe/Brussels","x":370,"y":260,"wires":[["e33253cd.42d96"]]},{"id":"6bf93d5a.2604a4","type":"moment","z":"850c7085.8eb5a","name":"HH","topic":"","input":"payload","inputType":"msg","inTz":"Europe/Brussels","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH","locale":"fr_BE","output":"hour","outputType":"msg","outTz":"Europe/Brussels","x":250,"y":260,"wires":[["4e228248.b96b2c"]]},{"id":"af5bb1fd.d01d7","type":"rpi-gpio out","z":"850c7085.8eb5a","name":"","pin":"32","set":"","level":"0","freq":"","out":"out","x":680,"y":260,"wires":[]},{"id":"1bfc7e27.2be4e2","type":"change","z":"850c7085.8eb5a","name":"","rules":[{"t":"set","p":"timeOn","pt":"flow","to":"play","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":180,"wires":[[]]},{"id":"7c09abfa.f50154","type":"change","z":"850c7085.8eb5a","name":"","rules":[{"t":"set","p":"timeOff","pt":"flow","to":"stop","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":220,"wires":[[]]},{"id":"841f7e06.0ded2","type":"ui_group","z":"","name":"Croissance","tab":"2fb138d2.a45978","order":2,"disp":true,"width":"6","collapse":false},{"id":"2fb138d2.a45978","type":"ui_tab","z":"","name":"Photopériode","icon":"dashboard","order":3,"disabled":false,"hidden":false}]
0 Likes

#4

Glad to help and that is much more compact!

0 Likes