Function node using the value of an array

Hello all, Is someone can help me to find why my flow dont work correctly...

It's an irrigation system on Home assistant.

I use 3 variables (1 bool and 2 number) in a function node. The variables are sent in the node with an array.

If one of the 3 values change, I trigger all "states refresh" to build my array and sent it to the function node.
On the function node:
If the "meteo" switch is OFF, I use directly the watering time from Home assistant
If the "meteo" switch is ON, I want to reduce the watering time in relation with the amount of rain fall of the 24h last hour.
The payload of that function node is alway the duration without correction as if the IF/ELSE didnt work. PS, As i'm not sure the boolean is recognise as it I also try with : if (Meteo == "true") and also ==="true"....

Hope someone can help me to find the bug...


debug

[{"id":"48c59c86.e93d54","type":"trigger-state","z":"4a8600c4.576c4","name":"Z1 ajuster avec météo","server":"d3920f3d.609da","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"input_boolean.zone1_meteo","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":true,"state_type":"habool","x":120,"y":1120,"wires":[["4ec5824d.d70a6c","67a91895.820e18","b828a5a3.e2cb48"],[]]},{"id":"5fff6602.747ec8","type":"trigger-state","z":"4a8600c4.576c4","name":"Pluie 24 heures","server":"d3920f3d.609da","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.debit_pluie_24_heure","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":true,"state_type":"num","x":100,"y":1060,"wires":[["4ec5824d.d70a6c","67a91895.820e18","b828a5a3.e2cb48"],[]]},{"id":"4ec5824d.d70a6c","type":"api-current-state","z":"4a8600c4.576c4","name":"current_state : Météo","server":"d3920f3d.609da","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.zone1_meteo","state_type":"habool","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":400,"y":1000,"wires":[["b5ac77e6.86b888"]]},{"id":"67a91895.820e18","type":"api-current-state","z":"4a8600c4.576c4","name":"current_state : Pluie 24 heure","server":"d3920f3d.609da","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.debit_pluie_24_heure","state_type":"num","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":430,"y":1060,"wires":[["c8505a8f.951258"]]},{"id":"b828a5a3.e2cb48","type":"api-current-state","z":"4a8600c4.576c4","name":"current_state : Durée","server":"d3920f3d.609da","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_number.zone1_duree","state_type":"num","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":400,"y":1120,"wires":[["44c6f273.30fcac"]]},{"id":"b5ac77e6.86b888","type":"change","z":"4a8600c4.576c4","name":"payload.meteo","rules":[{"t":"set","p":"data_tempo","pt":"msg","to":"payload","tot":"msg"},{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.meteo","pt":"msg","to":"data_tempo","tot":"msg"},{"t":"delete","p":"data_tempo","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":1000,"wires":[["dedc415f.8109"]]},{"id":"c8505a8f.951258","type":"change","z":"4a8600c4.576c4","name":"payload.pluie","rules":[{"t":"set","p":"data_tempo","pt":"msg","to":"payload","tot":"msg"},{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.pluie","pt":"msg","to":"data_tempo","tot":"msg"},{"t":"delete","p":"data_tempo","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":1060,"wires":[["dedc415f.8109"]]},{"id":"44c6f273.30fcac","type":"change","z":"4a8600c4.576c4","name":"payload.duree","rules":[{"t":"set","p":"data_tempo","pt":"msg","to":"payload","tot":"msg"},{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.duree","pt":"msg","to":"data_tempo","tot":"msg"},{"t":"delete","p":"data_tempo","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":1120,"wires":[["dedc415f.8109"]]},{"id":"a8eb66d8.70d248","type":"debug","z":"4a8600c4.576c4","name":"calcul","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":1110,"y":1060,"wires":[]},{"id":"18486356.b7e6bd","type":"trigger-state","z":"4a8600c4.576c4","name":"Durée","server":"d3920f3d.609da","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"input_number.zone1_duree","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":true,"state_type":"num","x":70,"y":1000,"wires":[["4ec5824d.d70a6c","67a91895.820e18","b828a5a3.e2cb48"],[]]},{"id":"63ca8c5b.935944","type":"debug","z":"4a8600c4.576c4","name":"join","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":1050,"y":1000,"wires":[]},{"id":"dedc415f.8109","type":"join","z":"4a8600c4.576c4","name":"variables","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":890,"y":1000,"wires":[["63ca8c5b.935944","42c475cb.5b1c0c"]]},{"id":"42c475cb.5b1c0c","type":"function","z":"4a8600c4.576c4","name":"Calculate duration","func":"var METEO = { payload: msg.payload[0].meteo };\nvar PLUIE = { payload: msg.payload[1].pluie };\nvar DUREE = { payload: msg.payload[2].duree };   \n\nif (METEO == true) \n{\n  var CALCUL = (((50-PLUIE)*DUREE)/50);\n  var FINAL = (Math.round(CALCUL));\n  msg = FINAL;\n\n}\nelse \n{\n  msg = DUREE;\n}\nreturn msg;\n","outputs":1,"noerr":0,"x":930,"y":1060,"wires":[["a8eb66d8.70d248"]]},{"id":"d3920f3d.609da","type":"server","z":"","name":"Home Assistant","addon":true}]

Sorry first one was wrong as I was looking on my phone.
There were pretty much a whole lot of syntax errors in you code.
I would really recommend that you look at the documentation on how to write functions in the function node https://nodered.org/docs/user-guide/writing-functions and at the structure of message objects https://nodered.org/docs/user-guide/messages
Have a look at what i changed and compare it to your original function code:

[{"id":"c539e634.4ff85","type":"debug","z":"f1796d14.83af4","name":"calcul","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":1190,"y":480,"wires":[]},{"id":"553a98d5.11dac","type":"function","z":"f1796d14.83af4","name":"Calculate duration","func":"var METEO = msg.payload[0].meteo;\nvar PLUIE = msg.payload[1].pluie;\nvar DUREE = msg.payload[2].duree;   \n\nif (METEO === true) \n{\n  var CALCUL = (((50-PLUIE)*DUREE)/50);\n  var FINAL = (Math.round(CALCUL));\n  msg.payload = FINAL;\n\n}\nelse \n{\n  msg.payload = DUREE;\n}\nreturn msg;\n","outputs":1,"noerr":0,"x":1010,"y":480,"wires":[["c539e634.4ff85"]]},{"id":"6d8412e.a6c9b6c","type":"inject","z":"f1796d14.83af4","name":"","topic":"","payload":"[{\"meteo\":true},{\"pluie\":5},{\"duree\":40}]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":830,"y":480,"wires":[["553a98d5.11dac"]]}]

It should work now.
Johannes

You're right!

I already read the writing function guide and the other one but as english is not my primary language there's something I probably missed.

I'm programming Arduino but it's a little different....

Great thanks Johannes

1 Like

Yes javascript is different to Arduino C code. Maybe look at some basic javascript tutorials that don’t even have to be Nodered specific to get a grasp on concepts like scope, declaration and object types and their properties in javascript.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.