I have one of these and ran into problems integrating it.
The main issue is that in order to read the relay state you need to use a TCP sensor, however that sensor type does not support JSON in Home Assistant (might be an oversight as I can’t see any technical reason we shouldn’t be able to parse JSON from a TCP sensor given you can from command line, REST etc…) and this device returns JSON over TCP. I tried to do it with the command line sensor which does allow you to store the status of the relays in JSON attributes, however if I send the ‘getstatus’ command to it using commandline/netcat, the response is never returned to HA. I did read it maybe possible to do it if you upgrade the netcat binary however I wanted my solution to just work on a standard install.
So if you just want to control the relays via HA only, all you need to do is setup the ip address and port on the RLY8 using the instructions provided (and just test it does in fact turn relays on and off using the provided netassist.exe program) and once thats working paste the following into your home assistant configuration.yaml file in the ‘switch:’ section:
- platform: command_line
switches:
rly8_01_01_relay:
command_on: echo -n '{"relay1":"on"}' | nc 192.168.1.1 5000
command_off: echo -n '{"relay1":"off"}' | nc 192.168.1.1 5000
friendly_name: 'RLY8 Switch 1'
rly8_01_02_relay:
command_on: echo -n '{"relay2":"on"}' | nc 192.168.1.1 5000
command_off: echo -n '{"relay2":"off"}' | nc 192.168.1.1 5000
friendly_name: 'RLY8 Switch 2'
rly8_01_03_relay:
command_on: echo -n '{"relay3":"on"}' | nc 192.168.1.1 5000
command_off: echo -n '{"relay3":"off"}' | nc 192.168.1.1 5000
friendly_name: 'RLY8 Switch 3'
rly8_01_04_relay:
command_on: echo -n '{"relay4":"on"}' | nc 192.168.1.1 5000
command_off: echo -n '{"relay4":"off"}' | nc 192.168.1.1 5000
friendly_name: 'RLY8 Switch 4'
rly8_01_05_relay:
command_on: echo -n '{"relay5":"on"}' | nc 192.168.1.1 5000
command_off: echo -n '{"relay5":"off"}' | nc 192.168.1.1 5000
friendly_name: 'RLY8 Switch 5'
rly8_01_06_relay:
command_on: echo -n '{"relay6":"on"}' | nc 192.168.1.1 5000
command_off: echo -n '{"relay6":"off"}' | nc 192.168.1.1 5000
friendly_name: 'RLY8 Switch 6'
rly8_01_07_relay:
command_on: echo -n '{"relay7":"on"}' | nc 192.168.1.1 5000
command_off: echo -n '{"relay7":"off"}' | nc 192.168.1.1 5000
friendly_name: 'RLY8 Switch 7'
rly8_01_08_relay:
command_on: echo -n '{"relay8":"on"}' | nc 192.168.1.1 5000
command_off: echo -n '{"relay8":"off"}' | nc 192.168.1.1 5000
friendly_name: 'RLY8 Switch 8'
You need to change the 192.168.1.1 address to the IP address you set on your RLY-8 and also potentially change the 5000 port number if you changed that as well. In addition you can upgrade the “friendly_name:” to match what you have connected to it…
That should then get the switch working so you can switch the relays on and off:
Now if you do want to control the relay from another device as well (ie another program outside of HA) then you can still update the switch status in HA using an automation. I did it as follows, however there maybe a better way of doing this.
under the “sensor:” section paste this:
- platform: tcp
name: rly8_01_status
host: 192.168.1.1
port: 5000
timeout: 2
scan_interval: 10
payload: '{"get":"relayStatus"}'
- platform: template
sensors:
rly8_01_01_sensor:
friendly_name: "Relay 1 Status"
value_template: "{{ states.sensor.rly8_01_status.state.split(',')[0].split(':')[1].split('\"')[1].split('\"')[0] }}"
rly8_01_02_sensor:
friendly_name: "Relay 2 Status"
value_template: "{{ states.sensor.rly8_01_status.state.split(',')[1].split(':')[1].split('\"')[1].split('\"')[0] }}"
rly8_01_03_sensor:
friendly_name: "Relay 3 Status"
value_template: "{{ states.sensor.rly8_01_status.state.split(',')[2].split(':')[1].split('\"')[1].split('\"')[0] }}"
rly8_01_04_sensor:
friendly_name: "Relay 4 Status"
value_template: "{{ states.sensor.rly8_01_status.state.split(',')[3].split(':')[1].split('\"')[1].split('\"')[0] }}"
rly8_01_05_sensor:
friendly_name: "Relay 5 Status"
value_template: "{{ states.sensor.rly8_01_status.state.split(',')[4].split(':')[1].split('\"')[1].split('\"')[0] }}"
rly8_01_06_sensor:
friendly_name: "Relay 6 Status"
value_template: "{{ states.sensor.rly8_01_status.state.split(',')[5].split(':')[1].split('\"')[1].split('\"')[0] }}"
rly8_01_07_sensor:
friendly_name: "Relay 7 Status"
value_template: "{{ states.sensor.rly8_01_status.state.split(',')[6].split(':')[1].split('\"')[1].split('\"')[0] }}"
rly8_01_08_sensor:
friendly_name: "Relay 8 Status"
value_template: "{{ states.sensor.rly8_01_status.state.split(',')[7].split(':')[1].split('\"')[1].split('\"')[0] }}"
then under “automations:” section paste this:
- alias: 'Automatic Update of Relay 1 State - ON'
trigger:
- platform: state
entity_id: sensor.rly8_01_01_sensor
from: 'off'
to: 'on'
action:
- service: homeassistant.turn_on
entity_id: switch.rly8_01_01_relay
- alias: 'Automatic Update of Relay 1 State - OFF'
trigger:
- platform: state
entity_id: sensor.rly8_01_01_sensor
from: 'on'
to: 'off'
action:
- service: homeassistant.turn_off
entity_id: switch.rly8_01_01_relay
- alias: 'Automatic Update of Relay 2 State - ON'
trigger:
- platform: state
entity_id: sensor.rly8_01_02_sensor
from: 'off'
to: 'on'
action:
- service: homeassistant.turn_on
entity_id: switch.rly8_01_02_relay
- alias: 'Automatic Update of Relay 2 State - OFF'
trigger:
- platform: state
entity_id: sensor.rly8_01_02_sensor
from: 'on'
to: 'off'
action:
- service: homeassistant.turn_off
entity_id: switch.rly8_01_02_relay
- alias: 'Automatic Update of Relay 3 State - ON'
trigger:
- platform: state
entity_id: sensor.rly8_01_03_sensor
from: 'off'
to: 'on'
action:
- service: homeassistant.turn_on
entity_id: switch.rly8_01_03_relay
- alias: 'Automatic Update of Relay 3 State - OFF'
trigger:
- platform: state
entity_id: sensor.rly8_01_03_sensor
from: 'on'
to: 'off'
action:
- service: homeassistant.turn_off
entity_id: switch.rly8_01_03_relay
- alias: 'Automatic Update of Relay 4 State - ON'
trigger:
- platform: state
entity_id: sensor.rly8_01_04_sensor
from: 'off'
to: 'on'
action:
- service: homeassistant.turn_on
entity_id: switch.rly8_01_04_relay
- alias: 'Automatic Update of Relay 4 State - OFF'
trigger:
- platform: state
entity_id: sensor.rly8_01_04_sensor
from: 'on'
to: 'off'
action:
- service: homeassistant.turn_off
entity_id: switch.rly8_01_04_relay
- alias: 'Automatic Update of Relay 5 State - ON'
trigger:
- platform: state
entity_id: sensor.rly8_01_05_sensor
from: 'off'
to: 'on'
action:
- service: homeassistant.turn_on
entity_id: switch.rly8_01_05_relay
- alias: 'Automatic Update of Relay 5 State - OFF'
trigger:
- platform: state
entity_id: sensor.rly8_01_05_sensor
from: 'on'
to: 'off'
action:
- service: homeassistant.turn_off
entity_id: switch.rly8_01_05_relay
- alias: 'Automatic Update of Relay 6 State - ON'
trigger:
- platform: state
entity_id: sensor.rly8_01_06_sensor
from: 'off'
to: 'on'
action:
- service: homeassistant.turn_on
entity_id: switch.rly8_01_06_relay
- alias: 'Automatic Update of Relay 6 State - OFF'
trigger:
- platform: state
entity_id: sensor.rly8_01_06_sensor
from: 'on'
to: 'off'
action:
- service: homeassistant.turn_off
entity_id: switch.rly8_01_06_relay
- alias: 'Automatic Update of Relay 7 State - ON'
trigger:
- platform: state
entity_id: sensor.rly8_01_07_sensor
from: 'off'
to: 'on'
action:
- service: homeassistant.turn_on
entity_id: switch.rly8_01_07_relay
- alias: 'Automatic Update of Relay 7 State - OFF'
trigger:
- platform: state
entity_id: sensor.rly8_01_07_sensor
from: 'on'
to: 'off'
action:
- service: homeassistant.turn_off
entity_id: switch.rly8_01_07_relay
- alias: 'Automatic Update of Relay 8 State - ON'
trigger:
- platform: state
entity_id: sensor.rly8_01_08_sensor
from: 'off'
to: 'on'
action:
- service: homeassistant.turn_on
entity_id: switch.rly8_01_08_relay
- alias: 'Automatic Update of Relay 8 State - OFF'
trigger:
- platform: state
entity_id: sensor.rly8_01_08_sensor
from: 'on'
to: 'off'
action:
- service: homeassistant.turn_off
entity_id: switch.rly8_01_08_relay
Then if something does change the state of the relay outside of HA, the switch state in HA will update to reflect that within 10 seconds. You also need to update the IP/Port in the sensor section above and you can change the “scan_interval” value if you want it to update the switch state more or less frequently.
If anyone has been able to integrate this relay more neatly I’d be interested as well.
HTH
Cheers, Nick.