Check state of load node, update button to on or off accordingly

The switch turns the load on and off properly via exec. The issue is when I manually turn on the load on the physical device not in node red, the button doesn't reflect that the load is on, currently it will show load if off.

Is there a way to have a function check the load state and then change the button accordingly? I am very new to this and been looking in the forums for similar flows and this is what I've attempted.

[{"id":"a8e10bfc.d978b8","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"13ea8c04.5f08d4","type":"mysql","z":"a8e10bfc.d978b8","mydb":"b1efcb9b.8e44a8","name":"Solardata","x":440,"y":280,"wires":[["8b08beb1.56953"]]},{"id":"266e386f.9bacd8","type":"inject","z":"a8e10bfc.d978b8","name":"","topic":"SELECT `PV array voltage`, `PV array current`, `PV array power`, `Battery voltage`, `Battery charging current`, `Battery charging power`, `Load voltage`, `Load current`, `Load power`, `Battery temperature`, `Charger temperature`, `Heat sink temperature`, `Battery SOC`, `Remote battery temperature`, `System rated voltage`, `Battery status`, `Equipment status` FROM `stats` ORDER BY `timestamp` DESC LIMIT 1 ","payload":"","payloadType":"str","repeat":"10","crontab":"","once":false,"onceDelay":"1","x":250,"y":280,"wires":[["13ea8c04.5f08d4"]]},{"id":"8b08beb1.56953","type":"function","z":"a8e10bfc.d978b8","name":"parse data","func":"var pvvoltage = msg.payload[0]['PV array voltage'];\nvar pvcurrent = msg.payload[0]['PV array current'];\nvar pvpower = msg.payload[0]['PV array power'];\nvar voltage = msg.payload[0]['Battery voltage'];\nvar battcurrent = msg.payload[0]['Battery charging current'];\nvar battpower = msg.payload[0]['Battery charging power'];\nvar battsoc = msg.payload[0]['Battery SOC'];\nvar loadvoltage = msg.payload[0]['Load voltage'];\nvar loadcurrent = msg.payload[0]['Load current'];\nvar loadpower = msg.payload[0]['Load power'];\nvar systemvoltage = msg.payload[0]['System rated voltage'];\nvar chargertemp = msg.payload[0]['Charger temperature'];\nvar batterytemp = msg.payload[0]['Battery temperature'];\nvar BattStatus = msg.payload[0]['Battery status'];\nvar chargerStatus = msg.payload[0]['Equipment status'];\n\nif ((msg.payload.indexOf('Database') === -1) && msg.topic.indexOf('Database') === -1){\n\n\nvar msg1 = {topic:\"PV array voltage\", payload: pvvoltage};\nvar msg2 = {topic:\"PV array current\", payload: pvcurrent};\nvar msg3 = {topic:\"PV array power\", payload: pvpower};\nvar msg4 = {topic:\"Battery voltage\", payload: voltage};\nvar msg5 = {topic:\"Battery charging current\", payload: battcurrent};\nvar msg6 = {topic:\"Battery charging power\", payload: battpower};\nvar msg7 = {topic:\"Battery SOC\", payload: battsoc};\nvar msg8 = {topic:\"[`Load voltage`]\", payload: loadvoltage};\nvar msg9 = {topic:\"[`Load current`]\", payload: loadcurrent};\nvar msg10 = {topic:\"[`Load power`]\", payload: loadpower};\nvar msg11 = {topic:\"[`System rated voltage`]\", payload: systemvoltage};\nvar msg12 = {topic:\"[`Charger temperature`]\", payload: chargertemp};\nvar msg13 = {topic:\"[`Battery temperature`]\", payload: batterytemp};\nvar msg14 = {topic:\"[`Battery status`]\", payload: BattStatus};\nvar msg15 = {topic:\"[`Equipment status`]\", payload: chargerStatus};\n\n\nreturn [msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9,msg10,msg11,msg12,msg13,msg14,[msg15]];\n\n} else {\nreturn;\n}\n","outputs":15,"noerr":0,"x":650,"y":320,"wires":[[],[],[],[],[],[],[],[],["cd867452.b3b508"],[],[],[],[],[],[]]},{"id":"297fbabb.bebba6","type":"ui_text","z":"a8e10bfc.d978b8","group":"45d13185.8fc46","order":0,"width":0,"height":0,"name":"","label":"text","format":"<font color= {{msg.colour}}> {{msg.label}} </font>&nbsp<i class=\"{{msg.icon}}\"></i>","layout":"row-spread","x":430,"y":540,"wires":[]},{"id":"cd867452.b3b508","type":"function","z":"a8e10bfc.d978b8","name":"Load Status","func":"var loadcurrent = +msg.payload\nif(loadcurrent == 0){\n    msg.label = \"Load Off\";\n    msg.payload = \"0\"\n    msg.topic = \"state\"\n    msg.colour = \"#00\";\n    msg.background = \"green\";\n    msg.icon = \"fa fa-lightbulb-o fa-2x nr-dashboard-ok\"\n    return msg;\n}\n\n\nelse{\n    msg.label = \"Load On\";\n    msg.payload = \"1\"\n    msg.topic = \"state\"\n    msg.colour = \"#f44242\";\n    msg.background = \"red\";\n        msg.icon = \"fa fa-lightbulb-o fa-2x nr-dashboard-warning\"\n    return msg;\n}\nreturn msg;","outputs":1,"noerr":0,"x":250,"y":540,"wires":[["297fbabb.bebba6","774adb23.c4e7d4"]]},{"id":"e8e3006e.9940d","type":"ui_switch","z":"a8e10bfc.d978b8","name":"","label":"Load on/off","group":"b9d11553.55a72","order":8,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"state","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":270,"y":660,"wires":[["300bea42.a866c6","952780b5.3cf0c"]]},{"id":"55650783.e48948","type":"inject","z":"a8e10bfc.d978b8","name":"from temp control on","topic":"state","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":721.2500019073486,"wires":[[]]},{"id":"5a0f53e9.b50a7c","type":"inject","z":"a8e10bfc.d978b8","name":"from temp control off","topic":"state","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":226.25,"y":765.0000019073486,"wires":[[]]},{"id":"774adb23.c4e7d4","type":"switch","z":"a8e10bfc.d978b8","name":"","property":"override","propertyType":"global","rules":[{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":740,"wires":[["300bea42.a866c6"]]},{"id":"6f3fbd6c.b14c34","type":"change","z":"a8e10bfc.d978b8","name":"global override","rules":[{"t":"set","p":"override","pt":"global","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":705.0000076293945,"y":622.5,"wires":[["75c099b1.0484e8"]]},{"id":"952780b5.3cf0c","type":"switch","z":"a8e10bfc.d978b8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":522.5000076293945,"y":643.75,"wires":[["6f3fbd6c.b14c34"],["a46ce4f6.1a46f8"]]},{"id":"a46ce4f6.1a46f8","type":"change","z":"a8e10bfc.d978b8","name":"global override","rules":[{"t":"set","p":"override","pt":"global","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":707.5,"y":662.4999866485596,"wires":[["1a12528f.57d7fd"]]},{"id":"300bea42.a866c6","type":"smartswitch","z":"a8e10bfc.d978b8","name":"switch","topic":"main","timeout":"0","x":590,"y":560,"wires":[[]]},{"id":"75c099b1.0484e8","type":"exec","z":"a8e10bfc.d978b8","command":"sudo ./","addpay":true,"append":"","useSpawn":"false","timer":"5","oldrc":false,"name":"Turn Load On","x":1072.5000076293945,"y":585.0000019073486,"wires":[[],[],[]]},{"id":"1a12528f.57d7fd","type":"exec","z":"a8e10bfc.d978b8","command":"sudo ./","addpay":false,"append":"","useSpawn":"false","timer":"5","oldrc":false,"name":"Turn Load Off","x":1072.5000076293945,"y":665.0000019073486,"wires":[[],[],[]],"outputLabels":["Normal","",""]},{"id":"b1efcb9b.8e44a8","type":"MySQLdatabase","z":"","host":"","port":"3306","db":"solardata","tz":""},{"id":"45d13185.8fc46","type":"ui_group","z":"","name":"Dashboard","tab":"c569e95d.e2ea7","order":1,"disp":true,"width":"20","collapse":true},{"id":"b9d11553.55a72","type":"ui_group","z":"","name":"Status","tab":"c569e95d.e2ea7","order":2,"disp":true,"width":"8","collapse":true},{"id":"c569e95d.e2ea7","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1}]

If the load is switched on manually on the device is the fact that it is on fed back to the computer somehow?

Yes, it updates the database "Load Current" field which I check via the function. Theres isn't a Load On/Off trigger i could see coming from the RS485.

Do you mean you just want the switch to follow the output of the Node Status function node? If so then feed the output of the function into the front of the switch. In addition you should uncheck the Msg Pass Through option in the switch. Also I see the function node is outputting string "0" or "1" whereas you have the switch configured to output number 0 or 1. You must make those both string or number.


Feed the output of "Load Status" function to which switch?

Forgive me as i am very much a newb on this, i'm not sure how to achieve making them string or number.

Thank you for assistance.

Hello admintite,

The switch can be configured to show the status based on the input to the switch, not on the output, this will reflect the real state, for doing so is required more info, how you turn manually on?

If is totally out of nodered and you are not used something to capture a feedback like MQTT, Serial or whatever you will need to get some real feedback somehow, maybe with a GPIO pin as input for instance so you can capture this real state feedback and pull it as input for the switch.

Will require more info about your hardware infrastructure to really know which options you have and what could me more easy o reliable or economic.


Into the dashboard ui switch. When you click that switch it sends the on or off value you have specified, but if you pass in a payload that matches the on or off value that will set the switch to that value. Your Load Status function node does
msg.payload = "0"
msg.payload = "1"
which sets the payload to string (that's what the quotes mean) 0 or 1. Connect that to the input of the ui switch node. In that node you have the outputs set to number (the dropdown to the left of the values for On payload and Off payload), so set that to string (a/z in the dropdown). Also clear the Msg Pass Through option or it will send the message on which is probably not what you want. Then change the switch node that feeds into to test for string instead of number. I don't know what the smart switch node is so I don't know whether it will be happy with string values rather that numeric. You will have to look at the spec of the node.

Hello Davidcgu,

To turn it on/off, I use the hardware button on the charger itself. This is the charger:

The underlaying method to extract data i followed is from this guide:

I am not a fan of having to query the database for information but due to my very limited knowledge i had to start somewhere to get a beginners understanding. I very much would love to do it directly from the /dev/ttyUSB21 but again its very overwhelming for a newb like me. Specially when it comes to MQTT and MODBUS.

Oh wow, my very primitive knowledge didnt allow me me to differenciate from string to number... until i read your message a few times and changed this in the function:

msg.payload = "0"
msg.payload = "1"


msg.payload = 0
msg.payload = 1

And now it works!!

Great to know that it works, however on the link you showed seems this device could be connected to network and even MQTT and you can fully control it from node red, at the end of the webside there is a flow, have you give it a try?

I recommend you to play a bit even if is just by curiosity.



yes the flow on the that page was the genesis of my current flow, it however uses the sql database node. i have attempted connecting directly to /dev/ttyUSB21 and i can see data but have no idea how to make something of that data.
The epsolar charger is connected to the network via a WiFi dongle and i enabled the TCP serial server, mounted it using SOCAT and /dev/ttyUSB21 on a raspberry pi.

Again not a fan of reading from DB, very much would love to speak directly to the charge controller but way above my head currently.

You can start by posting what info comes with a debug node, then you need to see what info is interesting for you to work with and how to handle it, if you are curious you could do great things easy, just playing a bit.


Thanks David.

You are making my curiosity peek so I did this.

[{"id":"f0d9e618.dce758","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"d5752aab.eb3d48","type":"serial in","z":"f0d9e618.dce758","name":"","serial":"c1ea0ef7.47af","x":700,"y":520,"wires":[["63bbd49d.15f09c"]]},{"id":"63bbd49d.15f09c","type":"debug","z":"f0d9e618.dce758","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1140,"y":540,"wires":[]},{"id":"c1ea0ef7.47af","type":"serial-port","z":"","serialport":"/dev/ttyUSB21","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"bin","out":"char","addchar":false,"responsetimeout":"10000"}]

and the debug output is this

If you dig into a bit more there is no more info than hex data?

Only hex data.

I think this class is used to translate the communications.

I wish i could do the same directly. Maybe i'll start a new thread on this topic.