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.
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?
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.
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?
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.