Function check payload of 10 diffrent mqtt topics

Hello

I'm trying to check state of 10 relays on different mqtt topics, i need to turn a pump on / off based on if all relays are off or just one on for floor heating with water.

Can anyone help me how to do this function?

[{"id":"77704ee0.fcd7c","type":"tab","label":"Heating","disabled":false,"info":""},{"id":"f2d2e100.e131","type":"function","z":"77704ee0.fcd7c","name":"","func":"var newMsg={};\nif (msg.topic == \"summermode\"){\n  //return [msg, msg, msg, msg, msg, msg, msg, msg, msg, msg, msg];\n}\nelse if (msg.topic == \"waterpumpon\"){\n    msg.payload = \"1\";\n    return [null, null, null, null, null, null, null, null, null, msg, null];\n}\nelse{\n\nif (msg.tt > msg.ct){\n    msg.payload = 1;\n}\nelse if (msg.ct > msg.tt){\n    msg.payload = 0;\n}\nif (msg.topic == \"climate.sovevaerelse\" ){\n    return [msg, null, null, null, null, null, null, null, null, null, null];\n}\nelse if (msg.topic == \"climate.kontor\" ){\n    return [null, msg, null, null, null, null, null, null, null, null, null];\n}\nelse if (msg.topic == \"climate.gang_entre\" ){\n    return [null, null, msg, null, null, null, null, null, null, null, null];\n}\nelse if (msg.topic == \"climate.bornevaerelse_1\" ){\n    return [null, null, null, msg, null, null, null, null, null, null, null];\n}\nelse if (msg.topic == \"climate.bornevaerelse_2\" ){\n    return [null, null, null, null, msg, null, null, null, null, null, null];\n}\nelse if (msg.topic == \"climate.bryggers\" ){\n    return [null, null, null, null, null, msg, null, null, null, null, null];\n}\nelse if (msg.topic == \"climate.kokken\" ){\n    return [null, null, null, null, null, null, msg, null, null, null, null];\n}\nelse if (msg.topic == \"climate.badevaerelse\" ){\n    return [null, null, null, null, null, null, null, msg, null, null, null];\n}\nelse if (msg.topic == \"climate.stue\" ){\n    return [null, null, null, null, null, null, null, null, msg, null, null];\n}\nelse if (msg.topic == \"climate.garage\" ){\n    return [null, null, null, null, null, null, null, null, null, null, msg];\n}\n}\n\n","outputs":11,"noerr":0,"initialize":"","finalize":"","x":760,"y":320,"wires":[["c06ed851.f717b8"],["c7ae4205.47655"],["fa4f764b.f981d8"],["e921243c.8d3258"],["abc49139.dbe5e"],["727b70ac.c7e02"],["d1a3fb53.488598"],["8c557763.a3a618"],["42f37ee9.9b3aa"],["7b101dc2.eeb944"],["d3cf51f0.121a8"]]},{"id":"e921243c.8d3258","type":"mqtt out","z":"77704ee0.fcd7c","name":"Børneværelse 1","topic":"/switch/heat/1/set","qos":"","retain":"","broker":"5c89750.15ce78c","x":1520,"y":40,"wires":[]},{"id":"de267b8b.cd4068","type":"mqtt in","z":"77704ee0.fcd7c","name":"Soveværelse Sensor","topic":"floorheat/sensor/bedroom/value","qos":"2","datatype":"json","broker":"5c89750.15ce78c","x":110,"y":40,"wires":[["ebf83107.04c8"]]},{"id":"abc49139.dbe5e","type":"mqtt out","z":"77704ee0.fcd7c","name":"Børneværelse 2","topic":"/switch/heat/2/set","qos":"","retain":"","broker":"5c89750.15ce78c","x":1520,"y":100,"wires":[]},{"id":"4371fe6d.e2ea","type":"mqtt in","z":"77704ee0.fcd7c","name":"Kontor Sensor","topic":"floorheat/sensor/office/value","qos":"2","datatype":"auto","broker":"5c89750.15ce78c","x":90,"y":100,"wires":[["8cbddf2e.c16ba"]]},{"id":"c06ed851.f717b8","type":"mqtt out","z":"77704ee0.fcd7c","name":"Soveværelse","topic":"/switch/heat/3/set","qos":"","retain":"","broker":"5c89750.15ce78c","x":1510,"y":160,"wires":[]},{"id":"547710d.aae59f","type":"mqtt in","z":"77704ee0.fcd7c","name":"Gang/entre Sensor","topic":"floorheat/sensor/hallway/value","qos":"2","datatype":"auto","broker":"5c89750.15ce78c","x":110,"y":160,"wires":[["285b8cc.7e45074"]]},{"id":"c7ae4205.47655","type":"mqtt out","z":"77704ee0.fcd7c","name":"Kontor","topic":"/switch/heat/4/set","qos":"","retain":"","broker":"5c89750.15ce78c","x":1490,"y":220,"wires":[]},{"id":"fa6cd671.109b98","type":"mqtt in","z":"77704ee0.fcd7c","name":"Børneværelse 1 Sensor","topic":"floorheat/sensor/kidsroom1/value","qos":"2","datatype":"auto","broker":"5c89750.15ce78c","x":120,"y":220,"wires":[["8241d819.71afe8"]]},{"id":"8c557763.a3a618","type":"mqtt out","z":"77704ee0.fcd7c","name":"Badeværelse","topic":"/switch/heat/5/set","qos":"","retain":"","broker":"5c89750.15ce78c","x":1510,"y":280,"wires":[]},{"id":"53e3864c.605538","type":"mqtt in","z":"77704ee0.fcd7c","name":"Børneværelse 2 Sensor","topic":"floorheat/sensor/kidsroom2/value","qos":"2","datatype":"auto","broker":"5c89750.15ce78c","x":120,"y":280,"wires":[["9e29b554.690078"]]},{"id":"fa4f764b.f981d8","type":"mqtt out","z":"77704ee0.fcd7c","name":"Gang","topic":"/switch/heat/6/set","qos":"","retain":"","broker":"5c89750.15ce78c","x":1490,"y":340,"wires":[]},{"id":"ccd304cb.326d38","type":"mqtt in","z":"77704ee0.fcd7c","name":"Bryggers Sensor","topic":"floorheat/sensor/mudroom/value","qos":"2","datatype":"auto","broker":"5c89750.15ce78c","x":100,"y":340,"wires":[["ee56ad8c.3b86b"]]},{"id":"42f37ee9.9b3aa","type":"mqtt out","z":"77704ee0.fcd7c","name":"Stue TV","topic":"/switch/heat/7/set","qos":"","retain":"","broker":"5c89750.15ce78c","x":1500,"y":400,"wires":[]},{"id":"3addad6a.c042e2","type":"mqtt in","z":"77704ee0.fcd7c","name":"Køkken Sensor","topic":"floorheat/sensor/kitchen/value","qos":"2","datatype":"auto","broker":"5c89750.15ce78c","x":100,"y":400,"wires":[["96b3f9ce.02fe28"]]},{"id":"d1a3fb53.488598","type":"mqtt out","z":"77704ee0.fcd7c","name":"Stue / Køkken","topic":"/switch/heat/8/set","qos":"","retain":"","broker":"5c89750.15ce78c","x":1520,"y":460,"wires":[]},{"id":"f5c5b43d.816958","type":"mqtt in","z":"77704ee0.fcd7c","name":"Badeværelse Sensor","topic":"floorheat/sensor/bathroom/value","qos":"2","datatype":"auto","broker":"5c89750.15ce78c","x":110,"y":460,"wires":[["b66be26d.a91a1"]]},{"id":"727b70ac.c7e02","type":"mqtt out","z":"77704ee0.fcd7c","name":"Bryggers","topic":"/switch/heat/9/set","qos":"","retain":"","broker":"5c89750.15ce78c","x":1500,"y":520,"wires":[]},{"id":"5646c7a4.9a3b18","type":"mqtt in","z":"77704ee0.fcd7c","name":"Stue Sensor","topic":"floorheat/sensor/livingroom/value","qos":"2","datatype":"auto","broker":"5c89750.15ce78c","x":90,"y":520,"wires":[["e9610129.50e5"]]},{"id":"d3cf51f0.121a8","type":"mqtt out","z":"77704ee0.fcd7c","name":"Garage","topic":"/switch/heat/10/set","qos":"","retain":"","broker":"5c89750.15ce78c","x":1500,"y":640,"wires":[]},{"id":"49ebe599.7a765c","type":"mqtt in","z":"77704ee0.fcd7c","name":"Garage Sensor","topic":"floorheat/sensor/garage/value","qos":"2","datatype":"auto","broker":"5c89750.15ce78c","x":100,"y":580,"wires":[["eb4fe527.69ca78"]]},{"id":"7b101dc2.eeb944","type":"mqtt out","z":"77704ee0.fcd7c","name":"","topic":"/switch/heat/pump/set","qos":"","retain":"","broker":"5c89750.15ce78c","x":1540,"y":580,"wires":[]},{"id":"89f4b976.94bbd8","type":"comment","z":"77704ee0.fcd7c","name":"Summer Mode Guide","info":"Uncomment //return [msg, msg, msg, msg, msg, msg, msg, msg, msg, msg, msg]; \n\nFor summermode","x":890,"y":80,"wires":[]},{"id":"66470f29.e15e8","type":"inject","z":"77704ee0.fcd7c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"300","crontab":"","once":false,"onceDelay":0.1,"topic":"waterpumpon","payload":"true","payloadType":"bool","x":150,"y":640,"wires":[["f2d2e100.e131"]]},{"id":"ebf83107.04c8","type":"function","z":"77704ee0.fcd7c","name":"","func":"const globalHomeAssistant = global.get('homeassistant');\n\nmsg = { \"tt\": globalHomeAssistant.homeAssistant.states[\"climate.sovevaerelse\"].attributes.temperature, \"ct\": msg.payload}\nmsg.topic = \"climate.sovevaerelse\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":40,"wires":[["f2d2e100.e131"]]},{"id":"8cbddf2e.c16ba","type":"function","z":"77704ee0.fcd7c","name":"","func":"const globalHomeAssistant = global.get('homeassistant');\n\nmsg = { \"tt\": globalHomeAssistant.homeAssistant.states[\"climate.kontor\"].attributes.temperature, \"ct\": msg.payload}\nmsg.topic = \"climate.kontor\";\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":100,"wires":[["f2d2e100.e131"]]},{"id":"285b8cc.7e45074","type":"function","z":"77704ee0.fcd7c","name":"","func":"const globalHomeAssistant = global.get('homeassistant');\n\nmsg = { \"tt\": globalHomeAssistant.homeAssistant.states[\"climate.gang_entre\"].attributes.temperature, \"ct\": msg.payload}\nmsg.topic = \"climate.gang_entre\";\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":160,"wires":[["f2d2e100.e131"]]},{"id":"8241d819.71afe8","type":"function","z":"77704ee0.fcd7c","name":"","func":"const globalHomeAssistant = global.get('homeassistant');\n\nmsg = { \"tt\": globalHomeAssistant.homeAssistant.states[\"climate.bornevaerelse_1\"].attributes.temperature, \"ct\": msg.payload}\nmsg.topic = \"climate.bornevaerelse_1\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":220,"wires":[["f2d2e100.e131"]]},{"id":"b66be26d.a91a1","type":"function","z":"77704ee0.fcd7c","name":"","func":"const globalHomeAssistant = global.get('homeassistant');\n\nmsg = { \"tt\": globalHomeAssistant.homeAssistant.states[\"climate.badevaerelse\"].attributes.temperature, \"ct\": msg.payload}\nmsg.topic = \"climate.badevaerelse\";\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":460,"wires":[["f2d2e100.e131"]]},{"id":"9e29b554.690078","type":"function","z":"77704ee0.fcd7c","name":"","func":"const globalHomeAssistant = global.get('homeassistant');\n\nmsg = { \"tt\": globalHomeAssistant.homeAssistant.states[\"climate.bornevaerelse_2\"].attributes.temperature, \"ct\": msg.payload}\nmsg.topic = \"climate.bornevaerelse_2\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":280,"wires":[["f2d2e100.e131"]]},{"id":"ee56ad8c.3b86b","type":"function","z":"77704ee0.fcd7c","name":"","func":"const globalHomeAssistant = global.get('homeassistant');\n\nmsg = { \"tt\": globalHomeAssistant.homeAssistant.states[\"climate.bryggers\"].attributes.temperature, \"ct\": msg.payload}\nmsg.topic = \"climate.bryggersr\";\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":340,"wires":[["f2d2e100.e131"]]},{"id":"96b3f9ce.02fe28","type":"function","z":"77704ee0.fcd7c","name":"","func":"const globalHomeAssistant = global.get('homeassistant');\n\nmsg = { \"tt\": globalHomeAssistant.homeAssistant.states[\"climate.kokken\"].attributes.temperature, \"ct\": msg.payload}\nmsg.topic = \"climate.kokken\";\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":400,"wires":[["f2d2e100.e131"]]},{"id":"eb4fe527.69ca78","type":"function","z":"77704ee0.fcd7c","name":"","func":"const globalHomeAssistant = global.get('homeassistant');\n\nmsg = { \"tt\": globalHomeAssistant.homeAssistant.states[\"climate.garage\"].attributes.temperature, \"ct\": msg.payload}\nmsg.topic = \"climate.garage\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":580,"wires":[["f2d2e100.e131"]]},{"id":"e9610129.50e5","type":"function","z":"77704ee0.fcd7c","name":"","func":"const globalHomeAssistant = global.get('homeassistant');\n\nmsg = { \"tt\": globalHomeAssistant.homeAssistant.states[\"climate.stue\"].attributes.temperature, \"ct\": msg.payload}\nmsg.topic = \"climate.stue\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":520,"wires":[["f2d2e100.e131"]]},{"id":"76e2cd33.2b3684","type":"timerswitch","z":"77704ee0.fcd7c","name":"","ontopic":"summermode","offtopic":"summermode","onpayload":"1","offpayload":"0","disabled":false,"schedules":[{"on_h":"03","on_m":"00","on_s":"00","off_h":"03","off_m":"00","off_s":"30","valid":true}],"x":210,"y":780,"wires":[["f2d2e100.e131"]]},{"id":"69bad132.d8b0e","type":"function","z":"77704ee0.fcd7c","name":"","func":"flow.set(msg.topic, msg.payload);\n\nif(flow.get(\"/switch/heat/1/state\") || flow.get(\"/switch/heat/2/state\") || flow.get(\"/switch/heat/3/state\") || flow.get(\"/switch/heat/4/state\") || flow.get(\"/switch/heat/5/state\") || flow.get(\"/switch/heat/6/state\") || flow.get(\"/switch/heat/7/state\") || flow.get(\"/switch/heat/8/state\" || flow.get(\"/switch/heat/9/state\") || flow.get(\"/switch/heat/10/state\") == 1)){\nmsg.payload = flow.get(msg.topic);\nreturn msg;\n}\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"// Code added here will be run when the\n// node is being stopped or re-deployed.\nflow.set(\"/switch/heat/1/state\", 0);\nflow.set(\"/switch/heat/2/state\", 0);\nflow.set(\"/switch/heat/3/state\", 0);\nflow.set(\"/switch/heat/4/state\", 0);\nflow.set(\"/switch/heat/5/state\", 0);\nflow.set(\"/switch/heat/6/state\", 0);\nflow.set(\"/switch/heat/7/state\", 0);\nflow.set(\"/switch/heat/8/state\", 0);\nflow.set(\"/switch/heat/9/state\", 0);\nflow.set(\"/switch/heat/10/state\", 0);\n","x":840,"y":660,"wires":[["9719d53c.288fa8"]]},{"id":"af2eb084.712c98","type":"mqtt in","z":"77704ee0.fcd7c","name":"test","topic":"/switch/heat/+/state","qos":"2","datatype":"auto","broker":"5c89750.15ce78c","x":650,"y":660,"wires":[[]]},{"id":"9719d53c.288fa8","type":"debug","z":"77704ee0.fcd7c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":990,"y":720,"wires":[]},{"id":"ee0dd62f.291e18","type":"inject","z":"77704ee0.fcd7c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":430,"y":860,"wires":[[]]},{"id":"15c02c27.138c0c","type":"function","z":"77704ee0.fcd7c","name":"","func":"msg.payload = flow.get(msg.topic);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":800,"y":760,"wires":[[]]},{"id":"5c89750.15ce78c","type":"mqtt-broker","z":"","name":"Mosquitto","broker":"home-assistant","port":"1883","clientid":"Node-Red","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

I'm confused, does each MQTT topic send the status of each relay or is each topic associated with a different relay?

One thing I would do is reduce tthe number of mqtt nodes. You could have one mqtt-in node with a topic of floorheat/sensor/# and then feed that to a switch node to examining the rest of the topic (use a 'contains' in the switch)

On the output side, you also have one mqtt-out node and buld the topic and pass it in.

Each relay respond with the state on /switch/heat/+/state

Yea i know i can do the other with less later.

For your function node, I would change it and use a switch statement to something like this

var heater = 0;
switch (msg.topic) {
    case "climate.sovevaerelse": 
        heater = 3
        break;

    case "climate.kontor":
        heater = 4
        break    
}

msg.topic = "/switch/heat/"+heater+"/set"
return msg

then you send this to the single mqtt-out node.

In the big function node why do you create var newMsg={}; and never use it?

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