In a function if you put code in “on start”, it only works once at Node-RED start up, you need to put that code in “on message” so anytime a message comes into the function (whenever the input changes), the code is run so it runs all the time. You also need to add in something for hysteresis, the code as written will just switch on and off at 70%, ideally you want the on and off to have a bit of space between them.
I am not a Node-RED expert, I am more of a coder so the way I have done stuff is to store the inputs as flow values using a change node, use say a 15 second timestamp inject into a function node to get the function to run once per 15 seconds. In the function I read the flow values as variables, do the logic and set the msg.payload of the function as the required switch position. I also store the Switch position to allow for hysteresis. I have the battery SOC and grid voltage in my Node-Red so I just knocked up the example below to show how I do this sort of thing. There are other examples for switching things on SOC, you may be able to find them by searching.
[
{
“id”: “983acf90b1fe81f5”,
“type”: “victron-input-vebus”,
“z”: “1b86f13573794161”,
“service”: “com.victronenergy.vebus/276”,
“path”: “/Ac/ActiveIn/L1/V”,
“serviceObj”: {
“service”: “com.victronenergy.vebus/276”,
“name”: “Inverter”
},
“pathObj”: {
“path”: “/Ac/ActiveIn/L1/V”,
“type”: “float”,
“name”: “Input voltage phase 1 (VAC)”
},
“name”: “”,
“onlyChanges”: false,
“x”: 190,
“y”: 1220,
“wires”: [
[
“a94d7dbe6434c489”
]
]
},
{
“id”: “a94d7dbe6434c489”,
“type”: “change”,
“z”: “1b86f13573794161”,
“name”: “Store Grid_Volt”,
“rules”: [
{
“t”: “set”,
“p”: “Grid_Volt”,
“pt”: “flow”,
“to”: “payload”,
“tot”: “msg”
}
],
“action”: “”,
“property”: “”,
“from”: “”,
“to”: “”,
“reg”: false,
“x”: 520,
“y”: 1220,
“wires”: [
]
},
{
“id”: “6a80f9b7a92e9011”,
“type”: “inject”,
“z”: “1b86f13573794161”,
“name”: “10 second timer”,
“props”: [
{
“p”: “payload”
},
{
“p”: “topic”,
“vt”: “str”
}
],
“repeat”: “10”,
“crontab”: “”,
“once”: false,
“onceDelay”: 0.1,
“topic”: “”,
“payload”: “”,
“payloadType”: “date”,
“x”: 730,
“y”: 1140,
“wires”: [
[
“770be5dcb7db2984”
]
]
},
{
“id”: “770be5dcb7db2984”,
“type”: “function”,
“z”: “1b86f13573794161”,
“name”: “On off”,
“func”: “var GridV = flow.get("Grid_Volt");\nvar BattSOC = flow.get("actualSOC");\nvar OldSw = flow.get("Sw_Posn");\n\nif (GridV < 200) {\n msg.payload = 0\n return msg;\n}\n\nif (BattSOC <= 70) {\n msg.payload = 3\n return msg;\n}\n\nif (BattSOC >= 99) {\n msg.payload = 0\n return msg;\n}\n\nif (OldSw != 0 || OldSw != 3 || OldSw === null || OldSw === undefined || isNaN(OldSw) === true) {\n OldSw = 0\n}\n\nmsg.payload = OldSw;\nreturn msg;\n\n”,
“outputs”: 1,
“timeout”: 0,
“noerr”: 0,
“initialize”: “”,
“finalize”: “”,
“libs”: ,
“x”: 930,
“y”: 1140,
“wires”: [
[
“b276691b8b40f7f5”,
“32b7454c50765003”
]
]
},
{
“id”: “32b7454c50765003”,
“type”: “debug”,
“z”: “1b86f13573794161”,
“name”: “debug 1”,
“active”: true,
“tosidebar”: true,
“console”: false,
“tostatus”: false,
“complete”: “false”,
“statusVal”: “”,
“statusType”: “auto”,
“x”: 1180,
“y”: 1140,
“wires”:
},
{
“id”: “b276691b8b40f7f5”,
“type”: “change”,
“z”: “1b86f13573794161”,
“name”: “Store Sw_Posn”,
“rules”: [
{
“t”: “set”,
“p”: “Sw_Posn”,
“pt”: “flow”,
“to”: “payload”,
“tot”: “msg”
}
],
“action”: “”,
“property”: “”,
“from”: “”,
“to”: “”,
“reg”: false,
“x”: 1120,
“y”: 1220,
“wires”: [
]
},
{
“id”: “b9376ca7aa20506a”,
“type”: “victron-input-battery”,
“z”: “1b86f13573794161”,
“service”: “com.victronenergy.battery/0”,
“path”: “/Soc”,
“serviceObj”: {
“service”: “com.victronenergy.battery/0”,
“name”: “Batteries & BMS”
},
“pathObj”: {
“path”: “/Soc”,
“type”: “float”,
“name”: “State of charge (%)”
},
“name”: “Battery SOC”,
“onlyChanges”: true,
“roundValues”: “1”,
“x”: 110,
“y”: 1680,
“wires”: [
[
“5cdb129bc1763822”
]
]
},
{
“id”: “5cdb129bc1763822”,
“type”: “change”,
“z”: “1b86f13573794161”,
“name”: “Store actualSOC”,
“rules”: [
{
“t”: “set”,
“p”: “actualSOC”,
“pt”: “flow”,
“to”: “payload”,
“tot”: “msg”
}
],
“action”: “”,
“property”: “”,
“from”: “”,
“to”: “”,
“reg”: false,
“x”: 370,
“y”: 1680,
“wires”: [
]
}
]