Switch status (available/unavailable) based on last_will message

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 have it like this:

- platform: mqtt
   name: "Camera [Power]"
   state_topic: "home/living_room/cam_pwr/status"
   command_topic: "home/living_room/cam_pwr"
   qos: 0
   payload_on: "on"
   payload_off: "off"
   retain: true

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.

1 Like

Hi Phileep,
Changing the icon for the switch would do it for me.
Could you elabore a bit more on how could I achieve that ? (Keeping in mind that it would need to listen to the mqtt message that says if it’s online/offline, comming from the switch)
I saw this commit: https://github.com/home-assistant/home-assistant-polymer/pull/34/commits/c95e7dae4a4c29fb98fa7de8267929924e48a0f6 (PR: https://github.com/home-assistant/home-assistant-polymer/pull/34 , but can’t make sense of how to use it.

The icon changing I was thinking of is discussed here

May only apply to sensors on re-reading!

I cant test right now - wrangling some MQTT broker issues before bed!

Just to share my config for a similar situation.

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:

  1. state_topic: "ha/sonoff-1be5f1/state" (ON/OFF) as confirmed by the sonoff
    
  2. command_topic: "ha/sonoff-1be5f1/command"  (ON/OFF) the command we pass to the sonoff
    
  3. 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.

Hope someone finds this useful

Phil

2 Likes

Phileep, many thanks for your time and help :slight_smile:
I hadn’t time for test your config yet… but will post as soon as I can test it!

Thanks!

I’ve made a pull request for this feature, but it has not been merged yet: https://github.com/home-assistant/home-assistant/pull/8593

Currently it uses the same payload data as the command and state messages but it could easily be extended to support a different payload.