Hi,
I’m using a Sonoff switch and would like to have some sort of way to display on the Homeassistant webpage if the switch is offline (unavailable) by greying the switch (disabling) so the user can’t change the switch state while it’s unavailable.
On the sonoff switch side, I have implemented a simple way to send the switch availability by setting the last_will message to be ‘offline’ , so the switch will send ‘offline’ to a specific mqtt topic whenever it disconnects from the broker and sends an ‘online’ message if it connects again.
Just need to figure out a way to make the webpage display this status on a particular switch.
I am not sure if you can disable (without hiding) a switch.
You are using a state topic, so if you do click the button and the sonoff does not confirm the state, it should revert back to the prior state.
You could also change the icon for the switch using customize: in an automation if the switch goes offline to set it grey - but I dont think you can disable the ability to use the switch.
I have some ESP8266 MQTT relays that are wired in series with light switches.
The switches (sonoff basic units) are programmed to turn on at power on - so that if someone (the wife) wants the light on and it is off due to the system, she can just turn the switch off and then on. This will update the status of the switch in MQTT once the sonoff is back on the network.
Using an MQTT switch works fine - whilst the unit has power! If the sonoff unit is off, the state if the switch never changes, it is locked at the last state before it lost power as it never gets confirmed back from the sonoff. So you don’t know that the switch is actually off as it has had the power cut.
I have 3 MQTT topics for the device:
state_topic: "ha/sonoff-1be5f1/state" (ON/OFF) as confirmed by the sonoff
command_topic: "ha/sonoff-1be5f1/command" (ON/OFF) the command we pass to the sonoff
status or will topic: "ha/sonoff-1be5f1/status" (online/offline) - offline is the will for the device. when the device comes online, it sets state to online.
I have setup 1 switch, 1 binary sensor for the device connection status and 1 template switch which combines the two other switches as follows:
##Basic switch:
switch:
- platform: mqtt
name: sonoff3_switch
state_topic: "ha/sonoff-1be5f1/state"
command_topic: "ha/sonoff-1be5f1/command"
qos: 0
payload_on: "ON"
payload_off: "OFF"
optimistic: false
##binary sensor - I did not use a switch as we cannot set this in HASS, it is the status as published by the device
binary_sensor:
- platform: mqtt
state_topic: "ha/sonoff-1be5f1/status"
name: "sonoff3_status"
qos: 0
payload_on: "online"
payload_off: "offline"
sensor_class: connectivity
hidden: true
##template switch
##(in the switch: section also)
- platform: template
switches:
sonoff3:
value_template: "{{is_state('switch.sonoff3_switch', 'on') and is_state('binary_sensor.sonoff3_status', 'on')}}"
turn_on:
service: switch.turn_on
entity_id: switch.sonoff3_switch
turn_off:
service: switch.turn_off
entity_id: switch.sonoff3_switch
entity_id:
- switch.sonoff3_switch
- binary_sensor.sonoff3_status
You can then manipulate the switches by using the template switch (switch.sonoff3 in this example) on your HASS screen. If the device is online, it will act like a normal switch. If it is offline, it will show off and not ever change as the state of the device will not be on and the switch falls back to off.
Apologies about formatting - I can’t seem to format more than 1 chunk of YAML in a post.