Relay Automation - Switch State

Hi,

I am using the following code to read the status of 16 relays from a script:

  - sensor:
      name: relay_status
      scan_interval: 2

      json_attributes:
        - '1'
        - '2'
        - '3'
	(etc)

      command: python3 /config/script.py
      value_template: '{{ value_json }}'

I have the following switch configuration, with command state blank in an effort to read the status of all relays once, rather than 16 times in quick succession if the status script was called by each switch.

  - switch:
      name: Relay_04
      scan_interval: 1
      command_on: echo -e "04+//" | nc 192.168.10.8 8899
      command_off: echo -e "04-//" | nc 192.168.10.8 8899
      command_state: ":"
      value_template: "{{ not state_attr('sensor.relay_status','4') | int == 0 }}"

In general the above works, but if I switch a relay on from the dashboard the toggle switch will change to on, then off for some time, then back on. Presumably this is because an older relay state call is overriding the toggle until next refresh.

I’ve tried fixing this by mininizing the scan_interval, but it isn’t reliable and I really only want to scan status every minute or so to ensure the switch status is correct in the event of a failed command/power outage, etc.

Any thoughts on how this can be achieved? Is there a way to get the dashboard toggle switch to ignore the status update for some period after a state change?

Thanks – Peter

Instead of using a command line switch put your on/off commands in shell commands and use a template switch. For the turn_on and turn_off sequences call your shell command then call the homeassistant.update_entity service to update your sensor. e.g.

turn_on:
  - service: shell_command.relay_4_on
  - service: homeassistant.update_entity
    target:
      entity_id: sensor.relay_status

I tried changing to a template switch, but am looking to read the actual status of the relays versus assuming the state following an on or off toggle:

- platform: template
    switches:
      relay_01:
        value_template: "{{ not state_attr('sensor.relay_status','1') | int == 0 }}"
        turn_on:
          service: shell_command.relay01_on
        turn_off:
          service: shell_command.relay01_off

This approach has the same issue - if I toggle the relay on from the dashboard, then I often see the toggle switch change back to off (for 1-2 seconds) then back on again. I think this is because an older relay status call overrides the actual state for a few seconds until next refresh.

Any thoughts on how to fix this - other than changing the scan interval to a very small number (1s) which floods the relay board with status requests?

Thanks!

You did not do what I said to though.

- platform: template
    switches:
      relay_01:
        value_template: "{{ not state_attr('sensor.relay_status','1') | int == 0 }}"
        turn_on:
          - service: shell_command.relay01_on
          - delay: 1
          - service: homeassistant.update_entity
            entity_id: sensor.relay_status
        turn_off:
          - service: shell_command.relay01_off
          - delay: 1
          - service: homeassistant.update_entity
            entity_id: sensor.relay_status

This forces an update of your sensor so you don’t have to wait for the next polling period to happen. You can now leave the sensor polling period as something sensible.

You may or may not need the delay.

Ah, got it! Thanks for the additional detail - tested and working.

1 Like