Conditional triggering based on AC input 1 source

Hi guys

Having difficulty with multiple conditions in a flow.

I want to use Node Red to determine: if connected to shore power or not, then apply logic.

I have used Venus Settings, input 1 source: using a switch “3’“ = shore, “0” = nothing

Then if “3” close relay 1, that’s pretty straight forward

if “0” I want to then use SoC to control relay 1, 99% close relay - 95% open relay.

I have set this up and am having difficulty with the second leg still being active when it’s conditions are not being meet. On power I see “3 Shore” under the Venus node and the ==3 leg of the switch is active and closes the relay, however the second leg ==0 which should be inactive is also active, or appears to be active. I suspect my issue may be where and how I connect the Battery monitor node.

After the 1st switch on the “==0” leg, i then have another switch where I have my SoC parameters, I have been feeding in the Battery monitor at this point.

Is there a better way to conditionally insert SoC.

Are you writing this in a function node, if so copy your code here.

You could do something like below, but you need to also make sure that when the SOC is between 95 and 99 that the value is not changed.. The && is and so you are checking both conditions.

if(InSwitch == 3){
RelayOut = 1;
}

if(InSwitch == 0 && SOC <= 95){
RelayOut = 1;
}

if(InSwitch == 0 && SOC >= 99){
RelayOut = 0;
}

@pwfarnell thanks for your help. I have tried using switches, not that familiar writing function yet. Have not been able to get && to work in switched, so tried function, have had a read, copied and adapted an example and this is the code in Function “on start”

if (msg.payload.VAC > 200 && msg.payload.SOC < 70) {
return msg; // Pass the message if both conditions are true
} else {
return null; // Block the message if conditions are not met
}

After the function node I have a trigger “send nothing - wait 1 sec - then send 3”

3 is the switch position of the Multi, I’m sure I’m getting close, I have the Lynx node (SOC) and the Multi (VAC) wired directly to two function nodes both are the same save the diff SoC parameter.

I have not changed anything in the Setup, On Message or On stop tabs of the function node

Cheers

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”: [

]
}
]