Control generators

hello all
please need help in setting up 3 switch relay to turn on one of my generators
1- i have 3 generators and electricity source
2- i added 4 optocouplers to see which one is on through led lamps on dashboard .
3- i want to add 3 relay switch with buttons to turn on any generator i need . also, if i turned any one of the 3 generator on, the other relays should switched off directly.
4- if the optocoupler of the electricity is on, thats mean all relays should be turned off .

any help

The first thing I would say is that if the system accidentally turns on relays that should not be turned on then will there be a big bang (actual or metaphorical)? If so then you need external hard wiring to make sure that this never happens.

Otherwise I am not sure at what level you are asking the question. Are you asking for advice on what devices you should use as the external relays or just how to arrange the algorithms in node-red?

Is this question at all related to node-RED?
If so, please explain how.

yes sure i make the wires to prevent this
im just asking about how to arrange the code in node red

it is about how to arrange the code in node ned
relay are connect to my rpi and also the optocouplers
node red is installed on my rpi

How are you driving the relays from node-red? Is it one output for each relay, or have you got a coded output so that from node red you output something like 0 to 4 when 0 means no relays, 1 means relay 1, 2 means relay 2 etc?

[
    {
        "id": "70c7ec66.31e354",
        "type": "tab",
        "label": "Electricity",
        "disabled": false,
        "info": ""
    },
    {
        "id": "190dc64a.a1249a",
        "type": "ui_led",
        "z": "70c7ec66.31e354",
        "order": 1,
        "group": "ca9031ed.6508",
        "width": "0",
        "height": "0",
        "label": "LEC",
        "labelPlacement": "left",
        "labelAlignment": "left",
        "colorForValue": [
            {
                "color": "#ff0000",
                "value": "false",
                "valueType": "bool"
            },
            {
                "color": "#008000",
                "value": "true",
                "valueType": "bool"
            }
        ],
        "allowColorForValueInMessage": false,
        "shape": "circle",
        "showGlow": true,
        "name": "",
        "x": 710,
        "y": 120,
        "wires": []
    },
    {
        "id": "dc628a10.7e8588",
        "type": "change",
        "z": "70c7ec66.31e354",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "1",
                "fromt": "num",
                "to": "false",
                "tot": "bool"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "0",
                "fromt": "num",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 480,
        "y": 120,
        "wires": [
            [
                "190dc64a.a1249a"
            ]
        ]
    },
    {
        "id": "b9ac64f9.cce7d8",
        "type": "rpi-gpio in",
        "z": "70c7ec66.31e354",
        "name": "",
        "pin": "5",
        "intype": "tri",
        "debounce": "25",
        "read": true,
        "bcm": true,
        "x": 270,
        "y": 120,
        "wires": [
            [
                "dc628a10.7e8588"
            ]
        ]
    },
    {
        "id": "9ea94c27.24e7d",
        "type": "ui_led",
        "z": "70c7ec66.31e354",
        "order": 1,
        "group": "ca9031ed.6508",
        "width": 0,
        "height": 0,
        "label": "Generator 30Kva",
        "labelPlacement": "left",
        "labelAlignment": "left",
        "colorForValue": [
            {
                "color": "#ff0000",
                "value": "false",
                "valueType": "str"
            },
            {
                "color": "#008000",
                "value": "true",
                "valueType": "str"
            }
        ],
        "allowColorForValueInMessage": false,
        "shape": "circle",
        "showGlow": true,
        "name": "",
        "x": 710,
        "y": 180,
        "wires": []
    },
    {
        "id": "1de9d56f.2aeb7b",
        "type": "change",
        "z": "70c7ec66.31e354",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "1",
                "fromt": "num",
                "to": "false",
                "tot": "bool"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "0",
                "fromt": "num",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 480,
        "y": 180,
        "wires": [
            [
                "9ea94c27.24e7d"
            ]
        ]
    },
    {
        "id": "4e327b9b.971464",
        "type": "rpi-gpio in",
        "z": "70c7ec66.31e354",
        "name": "",
        "pin": "6",
        "intype": "up",
        "debounce": "25",
        "read": true,
        "bcm": true,
        "x": 280,
        "y": 180,
        "wires": [
            [
                "1de9d56f.2aeb7b"
            ]
        ]
    },
    {
        "id": "d98fe3e107e2879b",
        "type": "ui_led",
        "z": "70c7ec66.31e354",
        "order": 1,
        "group": "ca9031ed.6508",
        "width": 0,
        "height": 0,
        "label": "Generator 100Kva",
        "labelPlacement": "left",
        "labelAlignment": "left",
        "colorForValue": [
            {
                "color": "#ff0000",
                "value": "false",
                "valueType": "str"
            },
            {
                "color": "#008000",
                "value": "true",
                "valueType": "str"
            }
        ],
        "allowColorForValueInMessage": false,
        "shape": "circle",
        "showGlow": true,
        "name": "",
        "x": 710,
        "y": 240,
        "wires": []
    },
    {
        "id": "cb54b3d5f07c254b",
        "type": "change",
        "z": "70c7ec66.31e354",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "1",
                "fromt": "num",
                "to": "false",
                "tot": "bool"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "0",
                "fromt": "num",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 480,
        "y": 240,
        "wires": [
            [
                "d98fe3e107e2879b"
            ]
        ]
    },
    {
        "id": "6659825d16a45def",
        "type": "rpi-gpio in",
        "z": "70c7ec66.31e354",
        "name": "",
        "pin": "13",
        "intype": "up",
        "debounce": "25",
        "read": true,
        "bcm": true,
        "x": 280,
        "y": 240,
        "wires": [
            [
                "cb54b3d5f07c254b"
            ]
        ]
    },
    {
        "id": "3b506a80a3120bc7",
        "type": "ui_led",
        "z": "70c7ec66.31e354",
        "order": 1,
        "group": "ca9031ed.6508",
        "width": 0,
        "height": 0,
        "label": "Generator 150Kva",
        "labelPlacement": "left",
        "labelAlignment": "left",
        "colorForValue": [
            {
                "color": "#ff0000",
                "value": "false",
                "valueType": "str"
            },
            {
                "color": "#008000",
                "value": "true",
                "valueType": "str"
            }
        ],
        "allowColorForValueInMessage": false,
        "shape": "circle",
        "showGlow": true,
        "name": "",
        "x": 710,
        "y": 300,
        "wires": []
    },
    {
        "id": "c3ea82460537377a",
        "type": "change",
        "z": "70c7ec66.31e354",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "1",
                "fromt": "num",
                "to": "false",
                "tot": "bool"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "0",
                "fromt": "num",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 480,
        "y": 300,
        "wires": [
            [
                "3b506a80a3120bc7"
            ]
        ]
    },
    {
        "id": "03d9a3e67fd73930",
        "type": "rpi-gpio in",
        "z": "70c7ec66.31e354",
        "name": "",
        "pin": "19",
        "intype": "up",
        "debounce": "25",
        "read": true,
        "bcm": true,
        "x": 280,
        "y": 300,
        "wires": [
            [
                "c3ea82460537377a"
            ]
        ]
    },
    {
        "id": "41f146119cbcf39f",
        "type": "rpi-gpio out",
        "z": "70c7ec66.31e354",
        "name": "",
        "pin": "18",
        "set": "",
        "level": "0",
        "freq": "",
        "out": "out",
        "bcm": true,
        "x": 680,
        "y": 440,
        "wires": []
    },
    {
        "id": "9eaa6f3e139ba62b",
        "type": "ui_button",
        "z": "70c7ec66.31e354",
        "name": "",
        "group": "ca9031ed.6508",
        "order": 4,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Gen-1",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "",
        "payloadType": "str",
        "topic": "topic",
        "topicType": "msg",
        "x": 230,
        "y": 440,
        "wires": [
            [
                "a0bbc5ad3e9b1f08"
            ]
        ]
    },
    {
        "id": "fd5c83a62e5c1c0f",
        "type": "ui_button",
        "z": "70c7ec66.31e354",
        "name": "",
        "group": "ca9031ed.6508",
        "order": 5,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Gen-2",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "",
        "payloadType": "str",
        "topic": "topic",
        "topicType": "msg",
        "x": 230,
        "y": 500,
        "wires": [
            [
                "a0bbc5ad3e9b1f08"
            ]
        ]
    },
    {
        "id": "a644075eda60c72b",
        "type": "ui_button",
        "z": "70c7ec66.31e354",
        "name": "",
        "group": "ca9031ed.6508",
        "order": 6,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Gen-3",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "",
        "payloadType": "str",
        "topic": "topic",
        "topicType": "msg",
        "x": 230,
        "y": 580,
        "wires": [
            [
                "a0bbc5ad3e9b1f08"
            ]
        ]
    },
    {
        "id": "a0bbc5ad3e9b1f08",
        "type": "function",
        "z": "70c7ec66.31e354",
        "name": "function 1",
        "func": "\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 460,
        "y": 500,
        "wires": [
            [
                "41f146119cbcf39f",
                "dfc5f88f4dd54dcf",
                "37b2b3f5d5d6106b"
            ]
        ]
    },
    {
        "id": "dfc5f88f4dd54dcf",
        "type": "rpi-gpio out",
        "z": "70c7ec66.31e354",
        "name": "",
        "pin": "23",
        "set": "",
        "level": "0",
        "freq": "",
        "out": "out",
        "bcm": true,
        "x": 680,
        "y": 520,
        "wires": []
    },
    {
        "id": "37b2b3f5d5d6106b",
        "type": "rpi-gpio out",
        "z": "70c7ec66.31e354",
        "name": "",
        "pin": "24",
        "set": "",
        "level": "0",
        "freq": "",
        "out": "out",
        "bcm": true,
        "x": 680,
        "y": 600,
        "wires": []
    },
    {
        "id": "ca9031ed.6508",
        "type": "ui_group",
        "name": "Electricity",
        "tab": "91457eaf.c4e2c",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "91457eaf.c4e2c",
        "type": "ui_tab",
        "name": "Electricity",
        "icon": "dashboard",
        "order": 16,
        "disabled": false,
        "hidden": false
    }
]```[quote="Colin, post:6, topic:66729, full:true"]
How are you driving the relays from node-red? Is it one output for each relay, or have you got a coded output so that from node red you output something like 0 to 4 when 0 means no relays, 1 means relay 1, 2 means relay 2 etc?
[/quote]

is it possible like this ?

but thinking how could be the function code . and how to connect the first optocoupler so that whenever it have 220v all the relays should be switched off

I would start by writing a function node that is given a value in range 0 to 3 with three outputs to drive the relays. Then you just have to feed in the appropriate value. 0 of the electricity is on, 1 if relay 1 is required, etc. Disable the buttons that allow switching when the electricity is on.

thanks for your info
let me try setting the function and let you now the tests

Test it initially feeding in values from Inject nodes. Once you are happy that is working then you can add the rest of the logic

if i use inject with value 1 the function worked and gpiopin changed to 1
but if i use 2 it will not work
if (msg.payload == 2) {
msg.payload.gpio23 == 1
}
return msg;

8/22/2022, msg : string[16]

"Invalid input: 2"

8/22/2022, msg.payload : number

2

I think the second line of your code should be...
msg.payload.gpio23 = 1;

1 Like

Also read what I suggested again

I meant a function with three outputs, one connected to each relay. In the function you need to build an array of three messages each with payload 0 or 1 and return that array.

If your payload is == 2 (I.e. it's a number) you cannot then add properties to payload as if it were an object.

You would need to do this...

if (msg.payload == 2) {
   msg.payload = {}
   msg.payload.gpio23 = 1
}

for (var i = 0; i <= 3 ; i++) {
msg.payload = [i]
return msg;
}

if (msg.payload == "i") {
msg.payload.gpio23 = 0
return [i, null, null];
}

else if (msg.payload == "i") {
msg.payload.gpio23 = 1
return [null, i, null];
}
else if (msg.payload == i) {
msg.payload.gpio4 = 1
return [null, null, i];
}
return msg;

it return the value only for the first output

it didnt worked . invalid input 2

it doesn't seems like that.

You will never get past the first ideration of this code. The return msg will exit the function node. You could use node.warn(msg) but even that will only return [0] three times.

You need to send an value to every output every time there is an input message. Otherwise it will not switch off relays that need to be off. Something like this

// given msg.payload 0,1,2 or 3 to indicate which relay should be on
// initialise all to off
let msgs = [{ payload: 0 }, { payload: 0 }, { payload: 0 }]
// only set one to On if payload > 0 and is a valid value
if (msg.payload > 0  &&  msg.payload <=3) {
    // set the appropriate output On
    msgs[msg.payload - 1].payload = 1
}
return msgs;

Test flow

image

[{"id":"21af9875d3152935","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","v":"","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":110,"y":4180,"wires":[["4d12ea96043cc9fe"]]},{"id":"cb322bb7c5bbc493","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","v":"","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":110,"y":4220,"wires":[["4d12ea96043cc9fe"]]},{"id":"876e6c996c25483a","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","v":"","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":110,"y":4260,"wires":[["4d12ea96043cc9fe"]]},{"id":"b6f8ee14b668f808","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","v":"","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3","payloadType":"num","x":110,"y":4300,"wires":[["4d12ea96043cc9fe"]]},{"id":"4d12ea96043cc9fe","type":"function","z":"bdd7be38.d3b55","name":"Determine relay states","func":"// given msg.payload 0,1,2 or 3 to indicate which relay should be on\n// initialise all to off\nlet msgs = [{ payload: 0 }, { payload: 0 }, { payload: 0 }]\n// only set one to On if payload > 0\nif (msg.payload > 0 && msg.payload <= 3) {\n    // set the appropriate output On\n    msgs[msg.payload - 1].payload = 1\n}\nreturn msgs;","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":4240,"wires":[["478d8c2f88917aad"],["5ea8021f2d9b49bc"],["1e8d8b75303c76df"]]},{"id":"478d8c2f88917aad","type":"debug","z":"bdd7be38.d3b55","name":"OP/1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":4160,"wires":[]},{"id":"5ea8021f2d9b49bc","type":"debug","z":"bdd7be38.d3b55","name":"OP/2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":4240,"wires":[]},{"id":"1e8d8b75303c76df","type":"debug","z":"bdd7be38.d3b55","name":"OP/3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":4320,"wires":[]}]