Controlling RLY-8 via JSON

Is it possible to use an existing integration to control an RLY-8 smart relay over ethernet?

I am new to HA and looking for a solution that ideally doesn’t involve coding. The can be telnetted to and uses JSON to communicate. Looking through the integrations, “telnet switch” looks like it might be viable, but I have so far found HA to be overwhelming with options that dont quite do what I am after.

Sonoff is an alternative relay would be workable… but I would rather run the device over ethernet.

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:

rly8

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.

I did not see anyone say thanks for this so I want to! Thanks.

Hi
I used RLY8 switch and use simple configuration like below with “command_state”. This is example for relay number 1. For others you have to change index in value_template.

switch:
  - platform: command_line
    switches:
        rly8_01_01_relay:
            command_on: echo -n '{"relay1":"on"}' | nc 192.168.1.23 2000
            command_off: echo -n '{"relay1":"off"}' | nc 192.168.1.23 2000
            command_state: echo -n '{"get":"relayStatus"}' | nc 192.168.1.23 2000
            value_template: '{{value.split(",")[0].split("\"")[3] == "on" }}'
            friendly_name: 'Gabinet'

Dariusz

Hi
I would add that…
It does not change the fact that RLY8 are not stable in my system, i.e. they do not always respond, which can be seen from errors in the log.

“Command failed: echo -n ‘{“relay1”:“off”}’ | nc 192.168.1.23 2000
0:23:35 – command_line (ERROR) - wiadomość pojawiła się po raz pierwszy 0:22:36 i powtarzała się 2 razy”

I lost a lot of time to find problem. I changed RLY8 for other LAN switch :wink: