Waveshare Modbus RS485 Relay Board via Node-Red and ZLAN Ethernet Modbus Gateway - full Guide

Since i found a viable solution to substitute the Tuya-Energy-Meters with ORNO-Modbus-Meters i still wanted to have a way of switching the electrical circuits i am measuring ON/OFF.

Must Haves:

  • switch things on and off
    ** maybe will become a Emergency Power Transfer Box with prority-based switchable circuits
  • all typical Voltages should be swhitchable
    ** 5V= Power Supply / USB-Power
    ** 12V= Power Supply, DIN-Rail-Relay
    ** 24V= Solar Battery Power, DIN-Rail-Relay
    ** 48V= Solar Battery Power
    ** 230V~
    ** 400V~
  • not use a Wireless connection

Since i already had a ZLAN 5143D Ethernet to Modbus RS485 Gateway lying around
image
i settled for the Waveshare RTU Relay Board
image

And then the Node-Red-Waltz went 1-2-3, 1-2-3 and with a little reading around the internet and documentation …

… i came up with this:

The upper half shows fetching the Relay-State:

1.)
We read the values every 10 seconds
image

2.)
We use a function mode to prep the read-command for the MODBUS Flex Getter
image

msg.payload = {
    'fc': 1,
    'unitid': 1,
    'address': 0x00ff,
    'quantity': 1,
};
return msg;

Then feed it to the Flex-Getter that accesses the Server-Settings
image

We get a message-Object that reads:
image

…and contains an array with the relay-states:
image

3.)
We use a function node to split that array into 8 descreet messages
image

var msg1= { payload: msg.payload[0] };
var msg2= { payload: msg.payload[1] };
var msg3= { payload: msg.payload[2] };
var msg4= { payload: msg.payload[3] };
var msg5= { payload: msg.payload[4] };
var msg6= { payload: msg.payload[5] };
var msg7= { payload: msg.payload[6] };
var msg8= { payload: msg.payload[7] };
return [msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8];

Each “exit” then transports a specific relay state over to home assistant.

4.)
A Home-Assistant Entity is created and fed the relay-state:
image

image

Voilà - we did not need to yammer into the config-file

The lower half shows, how to trigger a specific relay
1.)
A Home Assistant Switch entity is created:
image

image

The JSON-Payload shows the two states that are represented by the two outputs.

2.)
We use a fuction-node to create the payload for switching a single relay according to the switch state the message-payload announces
image

var val;
if (msg.payload=='on') {
    val= true;
}
if (msg.payload=='off') {
    val= false;
}
msg.payload = {
    'value': val,
    'fc': 5,
    'unitid': 1,
    'address': 0x0000,
    'quantity': 1,
};
return msg;

3.)
The MODBUS Flex writer is used to trigger the Relay-Board
image

Now we can create a Lovelace Dashboard:

And now we are ready to flip some switches…
e.g. when the battery state is below 15% we switch off the feed to the Inverter

1 Like

for your delight - The Node-Red-JSON
Have fun, Playing around!

[
    {
        "id": "659bdcb5f51541a2",
        "type": "tab",
        "label": "Waveshare Relay",
        "disabled": false,
        "info": ""
    },
    {
        "id": "51d9992770d87d77",
        "type": "inject",
        "z": "659bdcb5f51541a2",
        "name": "10s",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "10",
        "crontab": "",
        "once": true,
        "onceDelay": "10",
        "topic": "",
        "payloadType": "date",
        "x": 110,
        "y": 140,
        "wires": [
            [
                "ce162f6023373f49"
            ]
        ]
    },
    {
        "id": "ce162f6023373f49",
        "type": "function",
        "z": "659bdcb5f51541a2",
        "name": "read 0x00ff",
        "func": "msg.payload = {\n    'fc': 1,\n    'unitid': 1,\n    'address': 0x00ff,\n    'quantity': 1,\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 190,
        "y": 220,
        "wires": [
            [
                "824c5a6f45089bb2",
                "b1413baba168e2b7"
            ]
        ]
    },
    {
        "id": "824c5a6f45089bb2",
        "type": "modbus-flex-getter",
        "z": "659bdcb5f51541a2",
        "name": "Get relay state",
        "showStatusActivities": true,
        "showErrors": true,
        "logIOActivities": false,
        "server": "bd4c735308ae1a40",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "x": 320,
        "y": 140,
        "wires": [
            [
                "8f6b07b964a1df38"
            ],
            [
                "b1413baba168e2b7"
            ]
        ]
    },
    {
        "id": "b1413baba168e2b7",
        "type": "debug",
        "z": "659bdcb5f51541a2",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 690,
        "y": 380,
        "wires": []
    },
    {
        "id": "8f6b07b964a1df38",
        "type": "function",
        "z": "659bdcb5f51541a2",
        "name": "split payload",
        "func": "var msg1= { payload: msg.payload[0] };\nvar msg2= { payload: msg.payload[1] };\nvar msg3= { payload: msg.payload[2] };\nvar msg4= { payload: msg.payload[3] };\nvar msg5= { payload: msg.payload[4] };\nvar msg6= { payload: msg.payload[5] };\nvar msg7= { payload: msg.payload[6] };\nvar msg8= { payload: msg.payload[7] };\nreturn [msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8];",
        "outputs": 8,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 550,
        "y": 80,
        "wires": [
            [
                "41658a0c3e4cf6e7",
                "b1413baba168e2b7"
            ],
            [
                "71f84316ad9247c0",
                "b1413baba168e2b7"
            ],
            [
                "baca705d8a4fe442",
                "b1413baba168e2b7"
            ],
            [
                "b1413baba168e2b7",
                "d847e6b60bc9098b"
            ],
            [
                "b1413baba168e2b7",
                "83e56a37c90c03ed"
            ],
            [
                "b1413baba168e2b7",
                "065d1393b7f6924f"
            ],
            [
                "b1413baba168e2b7",
                "743e916784976ae3"
            ],
            [
                "b1413baba168e2b7",
                "a8517879db8a249f"
            ]
        ]
    },
    {
        "id": "41658a0c3e4cf6e7",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Relay 1",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 1,
        "entityType": "binary_sensor",
        "config": [
            {
                "property": "name",
                "value": "waveshare relay 1 state"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": false,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 1000,
        "y": 20,
        "wires": [
            []
        ]
    },
    {
        "id": "71f84316ad9247c0",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Relay 2",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 1,
        "entityType": "binary_sensor",
        "config": [
            {
                "property": "name",
                "value": "waveshare relay 2 state"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": false,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 1020,
        "y": 80,
        "wires": [
            []
        ]
    },
    {
        "id": "baca705d8a4fe442",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Relay 3",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 1,
        "entityType": "binary_sensor",
        "config": [
            {
                "property": "name",
                "value": "waveshare relay 3 state"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": false,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 1020,
        "y": 140,
        "wires": [
            []
        ]
    },
    {
        "id": "d847e6b60bc9098b",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Relay 4",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 1,
        "entityType": "binary_sensor",
        "config": [
            {
                "property": "name",
                "value": "waveshare relay 4 state"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": false,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 1020,
        "y": 200,
        "wires": [
            []
        ]
    },
    {
        "id": "83e56a37c90c03ed",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Relay 5",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 1,
        "entityType": "binary_sensor",
        "config": [
            {
                "property": "name",
                "value": "waveshare relay 5 state"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": false,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 1000,
        "y": 260,
        "wires": [
            []
        ]
    },
    {
        "id": "065d1393b7f6924f",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Relay 6",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 1,
        "entityType": "binary_sensor",
        "config": [
            {
                "property": "name",
                "value": "waveshare relay 6 state"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": false,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 980,
        "y": 320,
        "wires": [
            []
        ]
    },
    {
        "id": "743e916784976ae3",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Relay 7",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 1,
        "entityType": "binary_sensor",
        "config": [
            {
                "property": "name",
                "value": "waveshare relay 7 state"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": false,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 960,
        "y": 380,
        "wires": [
            []
        ]
    },
    {
        "id": "a8517879db8a249f",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Relay 8",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 1,
        "entityType": "binary_sensor",
        "config": [
            {
                "property": "name",
                "value": "waveshare relay 8 state"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": false,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 920,
        "y": 440,
        "wires": [
            []
        ]
    },
    {
        "id": "056f9c4a8fa467ad",
        "type": "function",
        "z": "659bdcb5f51541a2",
        "name": "write 0x0000",
        "func": "var val;\nif (msg.payload=='on') {\n    val= true;\n}\nif (msg.payload=='off') {\n    val= false;\n}\nmsg.payload = {\n    'value': val,\n    'fc': 5,\n    'unitid': 1,\n    'address': 0x0000,\n    'quantity': 1,\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 250,
        "y": 520,
        "wires": [
            [
                "da2cb8f1688f1716",
                "6a2e6922872c6bce"
            ]
        ]
    },
    {
        "id": "da2cb8f1688f1716",
        "type": "modbus-flex-write",
        "z": "659bdcb5f51541a2",
        "name": "flip switch",
        "showStatusActivities": false,
        "showErrors": true,
        "server": "bd4c735308ae1a40",
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "x": 960,
        "y": 540,
        "wires": [
            [
                "6a2e6922872c6bce"
            ],
            [
                "6a2e6922872c6bce"
            ]
        ]
    },
    {
        "id": "6a2e6922872c6bce",
        "type": "debug",
        "z": "659bdcb5f51541a2",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 690,
        "y": 440,
        "wires": []
    },
    {
        "id": "b8d41cdf21be77c5",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Switch 1",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 2,
        "entityType": "switch",
        "config": [
            {
                "property": "name",
                "value": "Waveshare Relay 1 Switch"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": true,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 80,
        "y": 520,
        "wires": [
            [
                "056f9c4a8fa467ad"
            ],
            [
                "056f9c4a8fa467ad"
            ]
        ]
    },
    {
        "id": "fcad836e9c6a6d96",
        "type": "function",
        "z": "659bdcb5f51541a2",
        "name": "write 0x0001",
        "func": "var val;\nif (msg.payload=='on') {\n    val= true;\n}\nif (msg.payload=='off') {\n    val= false;\n}\nmsg.payload = {\n    'value': val,\n    'fc': 5,\n    'unitid': 1,\n    'address': 0x0001,\n    'quantity': 1,\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 270,
        "y": 560,
        "wires": [
            [
                "da2cb8f1688f1716",
                "6a2e6922872c6bce"
            ]
        ]
    },
    {
        "id": "96a61288d8fca7c9",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Switch 2",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 2,
        "entityType": "switch",
        "config": [
            {
                "property": "name",
                "value": "Waveshare Relay 2 Switch"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": true,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 80,
        "y": 580,
        "wires": [
            [
                "fcad836e9c6a6d96"
            ],
            [
                "fcad836e9c6a6d96"
            ]
        ]
    },
    {
        "id": "0812d9a2c9f9bcc1",
        "type": "function",
        "z": "659bdcb5f51541a2",
        "name": "write 0x0002",
        "func": "var val;\nif (msg.payload=='on') {\n    val= true;\n}\nif (msg.payload=='off') {\n    val= false;\n}\nmsg.payload = {\n    'value': val,\n    'fc': 5,\n    'unitid': 1,\n    'address': 0x0002,\n    'quantity': 1,\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 290,
        "y": 600,
        "wires": [
            [
                "da2cb8f1688f1716",
                "6a2e6922872c6bce"
            ]
        ]
    },
    {
        "id": "dbe556841143b733",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Switch 3",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 2,
        "entityType": "switch",
        "config": [
            {
                "property": "name",
                "value": "Waveshare Relay 3 Switch"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": true,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 80,
        "y": 640,
        "wires": [
            [
                "0812d9a2c9f9bcc1"
            ],
            [
                "0812d9a2c9f9bcc1"
            ]
        ]
    },
    {
        "id": "99e7c2da0e79d023",
        "type": "function",
        "z": "659bdcb5f51541a2",
        "name": "write 0x0003",
        "func": "var val;\nif (msg.payload=='on') {\n    val= true;\n}\nif (msg.payload=='off') {\n    val= false;\n}\nmsg.payload = {\n    'value': val,\n    'fc': 5,\n    'unitid': 1,\n    'address': 0x0003,\n    'quantity': 1,\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 310,
        "y": 640,
        "wires": [
            [
                "da2cb8f1688f1716",
                "6a2e6922872c6bce"
            ]
        ]
    },
    {
        "id": "4e0097f58dfc9d4b",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Switch 4",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 2,
        "entityType": "switch",
        "config": [
            {
                "property": "name",
                "value": "Waveshare Relay 4 Switch"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": true,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 80,
        "y": 700,
        "wires": [
            [
                "99e7c2da0e79d023"
            ],
            [
                "99e7c2da0e79d023"
            ]
        ]
    },
    {
        "id": "419daf65d8f65636",
        "type": "function",
        "z": "659bdcb5f51541a2",
        "name": "write 0x0004",
        "func": "var val;\nif (msg.payload=='on') {\n    val= true;\n}\nif (msg.payload=='off') {\n    val= false;\n}\nmsg.payload = {\n    'value': val,\n    'fc': 5,\n    'unitid': 1,\n    'address': 0x0004,\n    'quantity': 1,\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 330,
        "y": 680,
        "wires": [
            [
                "da2cb8f1688f1716",
                "6a2e6922872c6bce"
            ]
        ]
    },
    {
        "id": "6516cc742509e357",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Switch 5",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 2,
        "entityType": "switch",
        "config": [
            {
                "property": "name",
                "value": "Waveshare Relay 5 Switch"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": true,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 80,
        "y": 760,
        "wires": [
            [
                "419daf65d8f65636"
            ],
            [
                "419daf65d8f65636"
            ]
        ]
    },
    {
        "id": "2fd863635b747c90",
        "type": "function",
        "z": "659bdcb5f51541a2",
        "name": "write 0x0005",
        "func": "var val;\nif (msg.payload=='on') {\n    val= true;\n}\nif (msg.payload=='off') {\n    val= false;\n}\nmsg.payload = {\n    'value': val,\n    'fc': 5,\n    'unitid': 1,\n    'address': 0x0005,\n    'quantity': 1,\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 730,
        "y": 660,
        "wires": [
            [
                "da2cb8f1688f1716",
                "6a2e6922872c6bce"
            ]
        ]
    },
    {
        "id": "4e0e595466442e5e",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Switch 6",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 2,
        "entityType": "switch",
        "config": [
            {
                "property": "name",
                "value": "Waveshare Relay 6 Switch"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": true,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 560,
        "y": 680,
        "wires": [
            [
                "2fd863635b747c90"
            ],
            [
                "2fd863635b747c90"
            ]
        ]
    },
    {
        "id": "61cc935cf845bd04",
        "type": "function",
        "z": "659bdcb5f51541a2",
        "name": "write 0x0006",
        "func": "var val;\nif (msg.payload=='on') {\n    val= true;\n}\nif (msg.payload=='off') {\n    val= false;\n}\nmsg.payload = {\n    'value': val,\n    'fc': 5,\n    'unitid': 1,\n    'address': 0x0006,\n    'quantity': 1,\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 770,
        "y": 700,
        "wires": [
            [
                "da2cb8f1688f1716",
                "6a2e6922872c6bce"
            ]
        ]
    },
    {
        "id": "237f2ebe1642f5cb",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Switch 7",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 2,
        "entityType": "switch",
        "config": [
            {
                "property": "name",
                "value": "Waveshare Relay 7 Switch"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": true,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 560,
        "y": 740,
        "wires": [
            [
                "61cc935cf845bd04"
            ],
            [
                "61cc935cf845bd04"
            ]
        ]
    },
    {
        "id": "d849a2effaf96b3d",
        "type": "function",
        "z": "659bdcb5f51541a2",
        "name": "write 0x0007",
        "func": "var val;\nif (msg.payload=='on') {\n    val= true;\n}\nif (msg.payload=='off') {\n    val= false;\n}\nmsg.payload = {\n    'value': val,\n    'fc': 5,\n    'unitid': 1,\n    'address': 0x0007,\n    'quantity': 1,\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 790,
        "y": 740,
        "wires": [
            [
                "da2cb8f1688f1716",
                "6a2e6922872c6bce"
            ]
        ]
    },
    {
        "id": "5a325959beb30c03",
        "type": "ha-entity",
        "z": "659bdcb5f51541a2",
        "name": "Switch 8",
        "server": "69eb452d.8a610c",
        "version": 1,
        "debugenabled": false,
        "outputs": 2,
        "entityType": "switch",
        "config": [
            {
                "property": "name",
                "value": "Waveshare Relay 8 Switch"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "outputOnStateChange": true,
        "outputPayload": "$entity().state ? \"on\": \"off\"",
        "outputPayloadType": "jsonata",
        "x": 560,
        "y": 800,
        "wires": [
            [
                "d849a2effaf96b3d"
            ],
            [
                "d849a2effaf96b3d"
            ]
        ]
    },
    {
        "id": "bd4c735308ae1a40",
        "type": "modbus-client",
        "name": "Waveshare",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "tcpHost": "10.0.0.69",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "unit_id": "1",
        "commandDelay": "1",
        "clientTimeout": "1000",
        "reconnectOnTimeout": true,
        "reconnectTimeout": "2000",
        "parallelUnitIdsAllowed": true
    },
    {
        "id": "69eb452d.8a610c",
        "type": "server",
        "name": "Home Assistant",
        "version": 1,
        "addon": true,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": true
    }
]
2 Likes

Hi Thomas,

Interesting to read about your experience with this RS485 workflow.
I will definitely look further into it as I’m looking into working with RS485 at the moment.
I would like to implement a lot of sensors relatively close to each other (in an area of 12x8m approx)
For this I wanted to have a lot (about 100) of sensors with a good reliability and no hassle with changing out batteries. Among these sensors there will be motion, door/reed, waterleak, pressure, temperature & humidity, pushbuttons, rotarydails, …
Have a look at the thread I created for it with a bit more information how I would like to set evrything up here: A lot of sensors

For some of them it’s no problem to refresh them on an interval like 5/10/30s. But for the door/reed, and motion sensors for example it would be crucial in HA to get the update as soon as the change happens.
Is that possible with RS485?

For now I’ve always come across an update interval.
How would I manage something like that?
Any help is appreciated, thanks!

Pieter

There is no way to get the state change without polling the sensor.
Some Modbus-devices also act as Servers writing to other modbus devices… maybe your sensor supports that?

Works with the Waveshare 8CH Relay PoE Ethernet too.
Just change node function “read 0x00ff” the paramters adress to 0x0000 and quantity to 8 for states (ON/OFF) of the switches (i recomend to change the node name “read 0x00ff” to “read 0x0000” too):

msg.payload = {
‘fc’: 1,
‘unitid’: 1,
‘address’: 0x0000,
‘quantity’: 8,
};
return msg;

BIG THX 4 THAT !!!

Hello everyone

I’m new to Waveshare CH8 POE relays, I connected the latter to a Mosquitto server and a Node-Red

So I communicate with all these little people in MQTT, however I need your help because I want to retrieve the status of the relays and for the moment I cannot do it

However, with the tutorial above I have the impression that it could really help me :slight_smile:

Can you help me and have you modified the relay json config?

THANKS

Hello JujuBois,

i have the Waveshare 8CH to ETH… but musst be the same like the POE.
If you need further help, let me know. The Relais working fine with NodeRed.

SADMSG

This is my config: with switch verify


Modbus [001].json

Thanks for the great work, I have the POE version up and running within minutes using this guide. I would just want to add the functionality to restore the state after power reset.

Hi, can you please provide the JSON to implement the verify? I assume that this is to sync the state with the relay position?