Help merging 2 Node Red flows in 1

Hi guys,

So I have the following flows to get voltage, power and current sensors added into HA :

[{"id":"6047f37e.79fcdc","type":"tuya-local","z":"390f3759.482b68","devName":"switch_1","devIp":"192.168.0.112","devId":"10470280dc4f22ed6311","devKey":"aa33d4b63d2xxxxx","protocolVer":"3.3","renameSchema":"","filterCB":"","x":320,"y":660,"wires":[["e7a19580.d61c48"]]},{"id":"11300381.5e29bc","type":"tuya-local","z":"390f3759.482b68","devName":"switch_2","devIp":"192.168.0.110","devId":"82141708dc4f22edb4d8","devKey":"2f86e9fc8cf4xxxxx","protocolVer":"3.3","renameSchema":"","filterCB":"","x":340,"y":860,"wires":[["b64ef5fa.405dd8"]]},{"id":"e7a19580.d61c48","type":"function","z":"390f3759.482b68","name":"","func":"if (\"payload\" in msg) {\n    return [msg,msg];\n} else {\n    return [null,msg];\n}","outputs":2,"noerr":0,"x":510,"y":660,"wires":[["16c52854.cde858"],[]]},{"id":"16c52854.cde858","type":"function","z":"390f3759.482b68","name":"","func":"const dps =  msg.payload.dps;\nlet current =  { payload: \"0 mA\"};\nlet power = { payload: \"0 W\"};\nlet voltage =  { payload: \"0 V\"};\n\n\nif (\"18\" in dps) {\n    current.payload = `${dps['18']} mA`;\n}\n\nif (\"19\" in dps) {\n    power.payload = `${dps['19']/10} `;\n}\n\nif (\"20\" in dps) {\n    voltage.payload = `${dps['20'] /10} V`;\n}\n\nreturn [current, power, voltage];","outputs":3,"noerr":0,"x":690,"y":660,"wires":[["bfe2424c.9e347"],["696c4381.2d8dfc","b333122c.19b3a"],["ad7770f8.44bd5"]]},{"id":"bfe2424c.9e347","type":"ha-entity","z":"390f3759.482b68","name":"current","server":"f4fba9ec.51b128","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"mA"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":900,"y":600,"wires":[[]]},{"id":"696c4381.2d8dfc","type":"ha-entity","z":"390f3759.482b68","name":"power","server":"f4fba9ec.51b128","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":890,"y":660,"wires":[[]]},{"id":"ad7770f8.44bd5","type":"ha-entity","z":"390f3759.482b68","name":"voltage","server":"f4fba9ec.51b128","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"V"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":900,"y":720,"wires":[[]]},{"id":"91e6cf.0a51f93","type":"inject","z":"390f3759.482b68","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":130,"y":660,"wires":[["6047f37e.79fcdc"]]},{"id":"b64ef5fa.405dd8","type":"function","z":"390f3759.482b68","name":"","func":"if (\"payload\" in msg) {\n    return [msg,msg];\n} else {\n    return [null,msg];\n}","outputs":2,"noerr":0,"x":530,"y":860,"wires":[["fac2c30e.21554"],[]]},{"id":"fac2c30e.21554","type":"function","z":"390f3759.482b68","name":"","func":"const dps =  msg.payload.dps;\nlet current =  { payload: \"0 mA\"};\nlet power = { payload: \"0 W\"};\nlet voltage =  { payload: \"0 V\"};\n\n\nif (\"18\" in dps) {\n    current.payload = `${dps['18']} mA`;\n}\n\nif (\"19\" in dps) {\n    power.payload = `${dps['19']/10} `;\n}\n\nif (\"20\" in dps) {\n    voltage.payload = `${dps['20'] /10} V`;\n}\n\nreturn [current, power, voltage];","outputs":3,"noerr":0,"x":710,"y":860,"wires":[["e65b7155.c8684"],["2ef1b465.a11edc"],["304e598.59850a6"]]},{"id":"e65b7155.c8684","type":"ha-entity","z":"390f3759.482b68","name":"current","server":"f4fba9ec.51b128","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"mA"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":920,"y":800,"wires":[[]]},{"id":"2ef1b465.a11edc","type":"ha-entity","z":"390f3759.482b68","name":"power","server":"f4fba9ec.51b128","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":910,"y":860,"wires":[[]]},{"id":"304e598.59850a6","type":"ha-entity","z":"390f3759.482b68","name":"volate","server":"f4fba9ec.51b128","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"V"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":910,"y":920,"wires":[[]]},{"id":"94c15572.d36398","type":"inject","z":"390f3759.482b68","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":150,"y":860,"wires":[["11300381.5e29bc"]]},{"id":"b333122c.19b3a","type":"debug","z":"390f3759.482b68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1120,"y":520,"wires":[]},{"id":"f4fba9ec.51b128","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

The way it’s setup will need a different flow for each different power plug but I’m pretty sure that by passwing each plug name as a variable you could optimize this a lot and make it so that all plugs are in the same flow, something like this :

My knowledge with Node Red is very limited so I don’t know how I can make this work the way I descbribed, would really appreciate some help with it !

Thanks

You’re on the right track. In your function node, you can do a switch on msg.topic (or use a Switch node) and then convert the bigger function node and the three outputs at the end to a subflow.

@code-in-progress thanks for the reply !

I tried to do that this way :

Added msg.topic switches for switch_1 and switch_2 , but these are just the devNames of the nodes.

However, doesn’t seem like msg.topic is being passed as a payload anywhere ? How can I set / get it ?

Reconnect switch 2 to your inject node. :slight_smile:

msg.topic won’t show up in payload. It’ll be at the same property level as msg.payload. If you throw a debug node in after switch 1/switch 2 and set it to “complete msg”, you’ll see the msg.topic show up.

1 Like

Is there some reason you are using Tuya (I assume that’s Tuya node) instead of getting the info from HA?

Tried that and here’s what I got :

Which one is msg.topic ? :blush:

Getting info from HA ? There is no direct support for tuya readings such as voltage, wattage, etc as far as I know .

Is that the entire msg object or just the payload? That looks like just the payload. Set your debug node to entire msg object.

Oh. Interesting. I thought for sure they would come through as attributes.

1 Like

Can you post (and blur sensitive info) your Tuya node? It should allow you to set a msg.topic (or other identifying data). If it doesn’t, you could probably switch on msg.data.name as well as msg.topic. Half a dozen of one type of thing. :slight_smile:

1 Like

I did , I think …

image

That’s all the info it got !

Oh. OK. I see the payload as part of the object. It’s weird that it doesn’t have a topic. What node palette are you using?

node-red-contrib-tuya-local

Sadly not, had to do quite a bit of research before coming up with a way to add them without flashing and tuya locale seemed the cleanest solution . The HA tuya integration only supports turning on and off .

Sure thing, here it goes :

[{"id":"390f3759.482b68","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"6047f37e.79fcdc","type":"tuya-local","z":"390f3759.482b68","devName":"switch_1","devIp":"192.168.0.112","devId":"10470280dc4f22ed6311","devKey":"aa33d4b63d2caxxx","protocolVer":"3.3","renameSchema":"","filterCB":"","x":320,"y":660,"wires":[["e7a19580.d61c48"]]},{"id":"11300381.5e29bc","type":"tuya-local","z":"390f3759.482b68","devName":"switch_2","devIp":"192.168.0.110","devId":"82141708dc4f22edb4d8","devKey":"2f86e9fc8cf4fxxx","protocolVer":"3.3","renameSchema":"","filterCB":"","x":340,"y":860,"wires":[["b64ef5fa.405dd8"]]},{"id":"e7a19580.d61c48","type":"function","z":"390f3759.482b68","name":"","func":"if (\"payload\" in msg) {\n    return [msg,msg];\n} else {\n    return [null,msg];\n}","outputs":2,"noerr":0,"x":470,"y":660,"wires":[["133fd40a.2a6bdc"],[]]},{"id":"16c52854.cde858","type":"function","z":"390f3759.482b68","name":"","func":"const dps =  msg.payload.dps;\nlet current =  { payload: \"0 mA\"};\nlet power = { payload: \"0 W\"};\nlet voltage =  { payload: \"0 V\"};\n\n\nif (\"18\" in dps) {\n    current.payload = `${dps['18']} mA`;\n}\n\nif (\"19\" in dps) {\n    power.payload = `${dps['19']/10} `;\n}\n\nif (\"20\" in dps) {\n    voltage.payload = `${dps['20'] /10} V`;\n}\n\nreturn [current, power, voltage];","outputs":3,"noerr":0,"x":750,"y":660,"wires":[["bfe2424c.9e347"],["696c4381.2d8dfc"],["ad7770f8.44bd5"]]},{"id":"bfe2424c.9e347","type":"ha-entity","z":"390f3759.482b68","name":"current","server":"f4fba9ec.51b128","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"mA"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":900,"y":600,"wires":[[]]},{"id":"696c4381.2d8dfc","type":"ha-entity","z":"390f3759.482b68","name":"power","server":"f4fba9ec.51b128","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":890,"y":660,"wires":[[]]},{"id":"ad7770f8.44bd5","type":"ha-entity","z":"390f3759.482b68","name":"voltage","server":"f4fba9ec.51b128","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"V"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":900,"y":720,"wires":[[]]},{"id":"91e6cf.0a51f93","type":"inject","z":"390f3759.482b68","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":130,"y":660,"wires":[["6047f37e.79fcdc","ee57bd6a.64c88"]]},{"id":"b64ef5fa.405dd8","type":"function","z":"390f3759.482b68","name":"","func":"if (\"payload\" in msg) {\n    return [msg,msg];\n} else {\n    return [null,msg];\n}","outputs":2,"noerr":0,"x":530,"y":860,"wires":[["fac2c30e.21554"],[]]},{"id":"fac2c30e.21554","type":"function","z":"390f3759.482b68","name":"","func":"const dps =  msg.payload.dps;\nlet current =  { payload: \"0 mA\"};\nlet power = { payload: \"0 W\"};\nlet voltage =  { payload: \"0 V\"};\n\n\nif (\"18\" in dps) {\n    current.payload = `${dps['18'] /1000} mA`;\n}\n\nif (\"19\" in dps) {\n    power.payload = `${dps['19']/10} `;\n}\n\nif (\"20\" in dps) {\n    voltage.payload = `${dps['20'] /10} V`;\n}\n\nreturn [current, power, voltage];","outputs":3,"noerr":0,"x":710,"y":860,"wires":[["e65b7155.c8684"],["2ef1b465.a11edc"],["304e598.59850a6"]]},{"id":"e65b7155.c8684","type":"ha-entity","z":"390f3759.482b68","name":"current","server":"f4fba9ec.51b128","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"A"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":920,"y":800,"wires":[[]]},{"id":"2ef1b465.a11edc","type":"ha-entity","z":"390f3759.482b68","name":"power","server":"f4fba9ec.51b128","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":910,"y":860,"wires":[[]]},{"id":"304e598.59850a6","type":"ha-entity","z":"390f3759.482b68","name":"volate","server":"f4fba9ec.51b128","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"V"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":910,"y":920,"wires":[[]]},{"id":"94c15572.d36398","type":"inject","z":"390f3759.482b68","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":150,"y":860,"wires":[["11300381.5e29bc"]]},{"id":"133fd40a.2a6bdc","type":"switch","z":"390f3759.482b68","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"switch_1","vt":"str"},{"t":"eq","v":"switch_2","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":610,"y":660,"wires":[["16c52854.cde858"],["16c52854.cde858"]]},{"id":"ee57bd6a.64c88","type":"tuya-local","z":"390f3759.482b68","devName":"switch_2","devIp":"192.168.0.110","devId":"82141708dc4f22edb4d8","devKey":"2f86e9fc8cf4fxxx","protocolVer":"3.3","renameSchema":"","filterCB":"","x":320,"y":600,"wires":[["e7a19580.d61c48","9a3285ed.36e8b8"]]},{"id":"9a3285ed.36e8b8","type":"debug","z":"390f3759.482b68","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","x":530,"y":540,"wires":[]},{"id":"f4fba9ec.51b128","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Thanks :slight_smile:

I can’t seem to find this node palette?

I couldn’t install the Tuya nodes, but I think if you do your switch on msg.data.name (or even ip: or id:), you should be OK so long as you remember that if you change the name of the device, you need to change it in NR as well.

1 Like

Oh I’m sorry should have mentioned, it needs to be installed manually !

1 Like

I found it right before you replied! Thanks. I just added it and will attempt to lend a hand

2 Likes

Just a heads up the way that the sensor nodes work you’ll have to have three separate ones for each switch. There’s really no way around it unless you want to do all the calls manually through the API.