Using msg.payload: nr value to switch a shelly relay

Hello all

I'm quit new to node red so I can use some help to learn and get my test setup working :wink:
The idea is to switch a shelly1 plus relay by mqtt in function of the state of charger of a battery.
I setup a mosquitto broker on my freenas system en works fine, I'm running node red on a cerbo gx device. I'm already able to controle the shelly relay manualy in node red by "inject" function. Now at this point I want to switch the relay automatic in funtion of the state of charge. The state of charge comes in as "msg.payload: number 99 (value from 0-99)" How can I use this value to switch on the relay above 95 and off below 94. I guess I need the "function" node, I tried already but with no succes.

[
    {
        "id": "a09ba820e62bca12",
        "type": "tab",
        "label": "Relay control by SOC",
        "disabled": false,
        "info": ""
    },
    {
        "id": "84080a165d3ecc41",
        "type": "mqtt in",
        "z": "a09ba820e62bca12",
        "name": "",
        "topic": "#",
        "qos": "2",
        "datatype": "auto",
        "broker": "69fe757f5b3e5904",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 610,
        "y": 540,
        "wires": [
            [
                "485ad33ac9dac72f"
            ]
        ]
    },
    {
        "id": "485ad33ac9dac72f",
        "type": "debug",
        "z": "a09ba820e62bca12",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 890,
        "y": 540,
        "wires": []
    },
    {
        "id": "470d5864d51bf32d",
        "type": "inject",
        "z": "a09ba820e62bca12",
        "name": "on",
        "props": [
            {
                "p": "topic",
                "vt": "str"
            },
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "shelly1-3c610572ccf0/rpc",
        "payload": "{\"id\":123,\"src\":\"/shelly1-3c610572ccf0/rpc\",\"method\":\"Switch.Set\",\"params\":{\"id\":0,\"on\":true}}",
        "payloadType": "json",
        "x": 610,
        "y": 360,
        "wires": [
            [
                "c98088e16dea5d1f"
            ]
        ]
    },
    {
        "id": "c98088e16dea5d1f",
        "type": "mqtt out",
        "z": "a09ba820e62bca12",
        "name": "",
        "topic": "",
        "qos": "2",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "69fe757f5b3e5904",
        "x": 870,
        "y": 360,
        "wires": []
    },
    {
        "id": "a4520e71012ae481",
        "type": "inject",
        "z": "a09ba820e62bca12",
        "name": "off",
        "props": [
            {
                "p": "topic",
                "vt": "str"
            },
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "shelly1-3c610572ccf0/rpc",
        "payload": "{\"id\":123,\"src\":\"/shelly1-3c610572ccf0/rpc\",\"method\":\"Switch.Set\",\"params\":{\"id\":0,\"on\":false}}",
        "payloadType": "json",
        "x": 610,
        "y": 400,
        "wires": [
            [
                "c98088e16dea5d1f"
            ]
        ]
    },
    {
        "id": "693108d92e429651",
        "type": "function",
        "z": "a09ba820e62bca12",
        "name": "start soc>95",
        "func": "",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 590,
        "y": 300,
        "wires": [
            [
                "c98088e16dea5d1f"
            ]
        ]
    },
    {
        "id": "3e868bb0b398c142",
        "type": "inject",
        "z": "a09ba820e62bca12",
        "name": "Toggle",
        "props": [
            {
                "p": "topic",
                "vt": "str"
            },
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "shelly1-3c610572ccf0/rpc",
        "payload": "{\"id\":123,\"src\":\"/shelly1-3c610572ccf0/rpc\",\"method\":\"Switch.Toggle\",\"params\":{\"id\":0,\"on\":true}}",
        "payloadType": "json",
        "x": 610,
        "y": 440,
        "wires": [
            [
                "c98088e16dea5d1f"
            ]
        ]
    },
    {
        "id": "e84afd6f351b0c23",
        "type": "inject",
        "z": "a09ba820e62bca12",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "95",
        "payloadType": "num",
        "x": 190,
        "y": 300,
        "wires": [
            [
                "693108d92e429651"
            ]
        ]
    },
    {
        "id": "bd6fe2ac1bd15299",
        "type": "inject",
        "z": "a09ba820e62bca12",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "94",
        "payloadType": "num",
        "x": 190,
        "y": 360,
        "wires": [
            [
                "693108d92e429651"
            ]
        ]
    },
    {
        "id": "69fe757f5b3e5904",
        "type": "mqtt-broker",
        "name": "",
        "broker": "192.168.1.118",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "sessionExpiry": ""
    }
]

Here is an example, bear in mind this could be coded in other ways to, and you could do it with a switch node and change nodes with no coding.

[{"id":"2e93b9a0.f510de","type":"inject","z":"a52e6e4913338c46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"95","payloadType":"num","x":300,"y":260,"wires":[["377009c8c7458265"]]},{"id":"377009c8c7458265","type":"function","z":"a52e6e4913338c46","name":"start soc>95","func":"let switch_value = msg.payload;\nmsg.topic = \"shelly1-3c610572ccf0/rpc\";\nmsg.payload = {\n    \"id\":123,\n    \"src\":\"/shelly1-3c610572ccf0/rpc\",\n    \"method\":\"Switch.Set\",\n    \"params\":{\n        \"id\":0,\n        \"on\":(switch_value > 94)\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":590,"y":300,"wires":[["2041a1f75e72bf1d","db89ade20a8fcd24"]]},{"id":"dfdd8366.9753a8","type":"inject","z":"a52e6e4913338c46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"94","payloadType":"num","x":302.3333435058594,"y":295.3333435058594,"wires":[["377009c8c7458265"]]},{"id":"612a1c42aea71435","type":"victron-input-system","z":"a52e6e4913338c46","service":"com.victronenergy.system","path":"/Dc/Battery/Soc","serviceObj":{"service":"com.victronenergy.system","name":"com.victronenergy.system","paths":[{"path":"/Ac/ActiveIn/Source","type":"enum","name":"AC-Input","enum":{"0":"Not available","1":"Grid","2":"Generator","3":"Shore","240":"Inverting"}},{"path":"/Ac/Consumption/L1/Power","type":"float","name":"AC Consumption L1 (W)"},{"path":"/Ac/Consumption/L2/Power","type":"float","name":"AC Consumption L2 (W)"},{"path":"/Ac/Consumption/L3/Power","type":"float","name":"AC Consumption L3 (W)"},{"path":"/Ac/ConsumptionOnInput/NumberOfPhases","type":"integer","name":"AC Consumption on Input Number Of Phases"},{"path":"/Ac/ConsumptionOnOutput/L1/Power","type":"float","name":"AC Consumption on Output L1"},{"path":"/Ac/ConsumptionOnOutput/L2/Power","type":"float","name":"AC Consumption on Output L2"},{"path":"/Ac/ConsumptionOnOutput/L3/Power","type":"float","name":"AC Consumption on Output L3"},{"path":"/Ac/ConsumptionOnOutput/NumberOfPhases","type":"integer","name":"AC Consumption on Output Number Of Phases"},{"path":"/Ac/Genset/DeviceType","type":"float","name":"Genset Device Type","enum":{}},{"path":"/Ac/Genset/L1/Power","type":"float","name":"Genset L1 (W)"},{"path":"/Ac/Genset/L2/Power","type":"float","name":"Genset L2 (W)"},{"path":"/Ac/Genset/L3/Power","type":"float","name":"Genset L3 (W)"},{"path":"/Ac/Genset/NumberOfPhases","type":"integer","name":"Genset Number Of Phases"},{"path":"/Ac/Grid/DeviceType","type":"float","name":"Grid Device Type"},{"path":"/Ac/Grid/L1/Power","type":"float","name":"Grid L1 (W)"},{"path":"/Ac/Grid/L2/Power","type":"float","name":"Grid L2 (W)"},{"path":"/Ac/Grid/L3/Power","type":"float","name":"Grid L3 (W)"},{"path":"/Ac/Grid/NumberOfPhases","type":"integer","name":"Grid Number Of Phases"},{"path":"/Ac/PvOnGenset/L1/Power","type":"float","name":"PV Power AC-tied on Generator L1"},{"path":"/Ac/PvOnGenset/L2/Power","type":"float","name":"PV Power AC-tied on Generator L2"},{"path":"/Ac/PvOnGenset/L3/Power","type":"float","name":"PV Power AC-tied on Generator L3"},{"path":"/Ac/PvOnGenset/NumberOfPhases","type":"integer","name":"PV Power AC-tied on Generator Number Of Phases"},{"path":"/Ac/PvOnGrid/L1/Power","type":"float","name":"PV - AC-coupled on input L1 (W)"},{"path":"/Ac/PvOnGrid/L2/Power","type":"float","name":"PV - AC-coupled on input L2 (W)"},{"path":"/Ac/PvOnGrid/L3/Power","type":"float","name":"PV - AC-coupled on input L3 (W)"},{"path":"/Ac/PvOnGrid/NumberOfPhases","type":"integer","name":"PV - AC-coupled on input Number Of Phases"},{"path":"/Ac/PvOnOutput/L1/Power","type":"float","name":"PV - AC-coupled on output L1 (W)"},{"path":"/Ac/PvOnOutput/L2/Power","type":"float","name":"PV - AC-coupled on output L2 (W)"},{"path":"/Ac/PvOnOutput/L3/Power","type":"float","name":"PV - AC-coupled on output L3 (W)"},{"path":"/Ac/PvOnOutput/NumberOfPhases","type":"integer","name":"PV - AC-coupled on output Number Of Phases"},{"path":"/Buzzer/State","type":"float","name":"Buzzer State","enum":{"0":"Off","1":"On"}},{"path":"/Dc/Battery/ConsumedAmphours","type":"float","name":"Battery Consumed Amphours (Ah)"},{"path":"/Dc/Battery/Current","type":"float","name":"Battery current (A)"},{"path":"/Dc/Battery/Power","type":"float","name":"Battery Power (W)"},{"path":"/Dc/Battery/Soc","type":"float","name":"Battery State of Charge (%)"},{"path":"/Dc/Battery/State","type":"enum","name":"Battery state","enum":{"0":"idle","1":"charging","2":"discharging"}},{"path":"/Dc/Battery/Temperature","type":"float","name":"Battery temperature (C)"},{"path":"/Dc/Battery/TimeToGo","type":"float","name":"Battery Time to Go (h)"},{"path":"/Dc/Battery/Voltage","type":"float","name":"Battery voltage (V)"},{"path":"/Dc/Charger/Power","type":"integer","name":"AC-Chargers - power (W)"},{"path":"/Dc/Pv/Current","type":"float","name":"MPPTs - current (A)"},{"path":"/Dc/Pv/Power","type":"float","name":"MPPTs - power (W)"},{"path":"/Dc/System/Power","type":"float","name":"DC System (W)"},{"path":"/Dc/Vebus/Current","type":"float","name":"VE.Bus charge current (A)"},{"path":"/Dc/Vebus/Power","type":"float","name":"VE.Bus charge power (W)"},{"path":"/Serial","type":"string","name":"Serial (System)"},{"path":"/SystemType","type":"string","name":"System type"},{"path":"/Timers/TimeOff","type":"float","name":"Time off (s)"},{"path":"/Timers/TimeOnGenerator","type":"float","name":"Time generator (s)"},{"path":"/Timers/TimeOnGrid","type":"float","name":"Time grid (s)"},{"path":"/Timers/TimeOnInverter","type":"float","name":"Time inverting (s)"}]},"pathObj":{"path":"/Dc/Battery/Soc","type":"float","name":"Battery State of Charge (%)"},"initial":"","name":"","x":240,"y":340,"wires":[["377009c8c7458265","2041a1f75e72bf1d"]]},{"id":"2041a1f75e72bf1d","type":"debug","z":"a52e6e4913338c46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":400,"wires":[]},{"id":"db89ade20a8fcd24","type":"mqtt out","z":"a52e6e4913338c46","name":"","topic":"","qos":"2","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"69fe757f5b3e5904","x":870,"y":360,"wires":[]},{"id":"69fe757f5b3e5904","type":"mqtt-broker","name":"","broker":"192.168.1.118","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

[EDIT] You may wish to filter the the responses as every time value changes it will publish a response to the topic. You could also convert the temp to a boolean and then filter using the filter node. This way a response is only published if true or false change.
e.g.

[{"id":"2e93b9a0.f510de","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"95","payloadType":"num","x":210,"y":3200,"wires":[["cd45210d.8278e"]]},{"id":"cd45210d.8278e","type":"function","z":"bf9e1e33.030598","name":"start soc>95","func":"if(msg.payload > 94){ \n    msg.payload = true\n}else{ \n    msg.payload = false\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":3220,"wires":[["2ece678.b097398"]]},{"id":"dfdd8366.9753a8","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"94","payloadType":"num","x":212.33334350585938,"y":3235.3333435058594,"wires":[["cd45210d.8278e"]]},{"id":"2ece678.b097398","type":"rbe","z":"bf9e1e33.030598","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","x":520,"y":3220,"wires":[["377009c8c7458265"]]},{"id":"377009c8c7458265","type":"function","z":"bf9e1e33.030598","name":"start soc>95","func":"msg.topic = \"shelly1-3c610572ccf0/rpc\";\nmsg.payload = {\n    \"id\":123,\n    \"src\":\"/shelly1-3c610572ccf0/rpc\",\n    \"method\":\"Switch.Set\",\n    \"params\":{\n        \"id\":0,\n        \"on\": msg.payload\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":3220,"wires":[["2041a1f75e72bf1d"]]},{"id":"2041a1f75e72bf1d","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":420,"y":3340,"wires":[]}]

Hi Tim, welcome to the forum.
A lot of users will be reluctant to load your flow above to test it, because you have included a non-standard node in your flow, which means that it would need to be also installed and configured by the user.
The usual way around this is to replace the offending node with an inject node, to replicate the input msg.
You may wish to edit your post above accordingly.

In answer to your question (without trying your flow), couldn't you use a switch node which would create 2 outputs. One, if input => 95, and the other =<94.
Then use change nodes on each of the switch outputs to format the messages which would switch on, or off the shelly.

1 Like

yes this is what I'm looking for, thank you verry much for the help!

Thank you Paul, I editted the flow I posted, I didn't realise but yes, the victron nodes are not standard in node red, something to think about in the future, thx for the helpfull tips! I can get further with these.

grtz

1 Like
[{"id":"377009c8c7458265","type":"function","z":"a52e6e4913338c46","name":"start soc>95","func":"\nlet msg1 = {}\nlet invar = msg.payload\n\nif(invar > 95) {\n    //on\n    msg1.payload = {\n        \"id\": 123,\n        \"src\": \"/shelly1-3c610572ccf0/rpc\",\n        \"method\": \"Switch.Set\",\n        \"params\": {\n            \"id\": 0,\n            \"on\": true\n        }\n    }   \n} else if(invar < 94) {\n    //off\n    msg1.payload = {\n        \"id\": 123,\n        \"src\": \"/shelly1-3c610572ccf0/rpc\",\n        \"method\": \"Switch.Set\",\n        \"params\": {\n            \"id\": 0,\n            \"on\": false\n        }\n    }\n} else {\n    msg1 = null\n}\n\nreturn msg1\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":220,"wires":[["b2cc7e90e4d37154"]]},{"id":"70dfd12a256b68f3","type":"inject","z":"a52e6e4913338c46","name":"> 95","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"96","payloadType":"num","x":90,"y":160,"wires":[["377009c8c7458265"]]},{"id":"4d85ac9b90b6d590","type":"inject","z":"a52e6e4913338c46","name":"< 94","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"93","payloadType":"num","x":90,"y":280,"wires":[["377009c8c7458265"]]},{"id":"02eab988c6037895","type":"inject","z":"a52e6e4913338c46","name":"= 50","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"50","payloadType":"num","x":90,"y":320,"wires":[["377009c8c7458265"]]},{"id":"2193f3c75603b29e","type":"inject","z":"a52e6e4913338c46","name":"= 100","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":90,"y":120,"wires":[["377009c8c7458265"]]},{"id":"dff6dd3a0210aa73","type":"inject","z":"a52e6e4913338c46","name":"= 95","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"95","payloadType":"num","x":90,"y":200,"wires":[["377009c8c7458265"]]},{"id":"32f753f62bd52603","type":"inject","z":"a52e6e4913338c46","name":"= 94","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"94","payloadType":"num","x":90,"y":240,"wires":[["377009c8c7458265"]]},{"id":"b2cc7e90e4d37154","type":"junction","z":"a52e6e4913338c46","x":460,"y":280,"wires":[["db89ade20a8fcd24","2041a1f75e72bf1d"]]},{"id":"f6ee0c7b.18c73","type":"inject","z":"a52e6e4913338c46","name":"on","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"shelly1-3c610572ccf0/rpc","payload":"{\"id\":123,\"src\":\"/shelly1-3c610572ccf0/rpc\",\"method\":\"Switch.Set\",\"params\":{\"id\":0,\"on\":true}}","payloadType":"json","x":310,"y":260,"wires":[["b2cc7e90e4d37154"]]},{"id":"74b9c69098b56992","type":"inject","z":"a52e6e4913338c46","name":"off","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"shelly1-3c610572ccf0/rpc","payload":"{\"id\":123,\"src\":\"/shelly1-3c610572ccf0/rpc\",\"method\":\"Switch.Set\",\"params\":{\"id\":0,\"on\":false}}","payloadType":"json","x":310,"y":300,"wires":[["b2cc7e90e4d37154"]]},{"id":"083becd95f18bf97","type":"inject","z":"a52e6e4913338c46","name":"Toggle","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"shelly1-3c610572ccf0/rpc","payload":"{\"id\":123,\"src\":\"/shelly1-3c610572ccf0/rpc\",\"method\":\"Switch.Toggle\",\"params\":{\"id\":0,\"on\":true}}","payloadType":"json","x":310,"y":340,"wires":[["b2cc7e90e4d37154"]]},{"id":"db89ade20a8fcd24","type":"mqtt out","z":"a52e6e4913338c46","name":"","topic":"Test","qos":"2","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"112b4e49339ca04a","x":570,"y":300,"wires":[]},{"id":"2041a1f75e72bf1d","type":"debug","z":"a52e6e4913338c46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":260,"wires":[]},{"id":"3c78c12872e79341","type":"mqtt in","z":"a52e6e4913338c46","name":"","topic":"Test","qos":"2","datatype":"auto","broker":"112b4e49339ca04a","nl":false,"rap":true,"rh":0,"inputs":0,"x":190,"y":400,"wires":[["1f70204999ac3e9d"]]},{"id":"1f70204999ac3e9d","type":"debug","z":"a52e6e4913338c46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":470,"y":400,"wires":[]},{"id":"112b4e49339ca04a","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

Change mqtt to your own, I use localhost.

1 Like