[FIX] MQTT Sensor and switch with unknown states after HA and mqtt addon restart

Hi there!
Since a few versions ago, HA started to forget states for some mqtt based, sensors and switches when restarted together with mqtt-addon.

I’ve tried to set tasmota to sensorretain and switchretain, but for some “unknown” reason :joy:, tasmota would not send switch2 state with retain.

Here it is what i’ve done to bypass this:

#########################
# Tasmotas fix for Unknown state after home assistant and mqtt restart.
# This fix gets mqtt messages from tasmota, and using automation, republishes them
# to another topic, using retain.
# While tasmota does not resend its state, Home assistant keeps reading from the topic
# with retain.
#########################


binary_sensor:

  - platform: mqtt
    name: "Porta Sala"
    state_topic: "retained/stat/tasmota_porta/SWITCH2" #this is the retained topic
                 # being consumed by the sensor, and no more unknown states.
    payload_on: "ON"
    payload_off: "OFF"
    availability:
      - topic: "tele/tasmota_porta/LWT"
        payload_available: "Online"
        payload_not_available: "Offline"
    qos: 0
    device_class: door
    value_template: '{{ value_json["STATE"] }}'


automation:

- id: Tasmota_porta republish with retain
  alias: Tasmota_porta republish with retain
  initial_state: 'on'
  trigger:
    - platform: mqtt
      topic: stat/tasmota_porta/SWITCH2 #original topic, that will be listened. 
      # and when something is sent from tasmota, it will be retained.
  action:
    - service: mqtt.publish
      data:
        topic: retained/stat/tasmota_porta/SWITCH2 #this is the retained topic,
        # which will be used above on the sensor itself.
        retain: true
        payload_template: "{{ trigger.payload }}"

Is there a reason you aren’t using the Tasmota integration?

1 Like

I’m using via mqtt integration setoption19 1. Because it does not discovered all the sensors via tasmota integration.

I assume you realize that SetOption19 1 is MQTT Discovery and the Tasmota project no longer supports it (and will eventually remove it from the release versions).

What kind of sensors do you have that are not being discovered by the Tasmota integration? I’m curious to know because according to Tasmota’s documentation, sensors are supported.

1 Like

Ops. My mistake. It is a switch, not a sensor. Switch2 is the guy not being discovered by Tasmota integration. I’ve realized that some other approaches, such as decouple every switch in tasmota using setoption114 1 and rediscovery with Tasmota integration could be a fix too.

It is just a regular sonoff basic with a button and a switch added.

Not sure why your basic switch isn’t discoverable. I have wall plugs and outlets (although none are from Sonoff) and they have all been discovered via the Tasmota integration as switches. For example:

Screenshot from 2022-03-11 17-31-45

Check your Sonoff’s switch’s programming with this reference:

1 Like

Yep. Thats where i’ve started.

Switch1, which is coupled with relay1, is being discovered.
Button1 is decoupled with everything and all i need is his mqtt messages, such as SINGLE, DOUBLE…
Switch2 is not being discovered by Tasmota integration, but with MQTT discovery, it is. Maybe because it is not coupled with any relay, i dont know.
Actually, what i’m missing, is the states being unknown for switch2 after ha and mqtt addon restart.
And switch2 being my front door, closed or open sensor… You may understand the rest… :blush:

If you use SetOption19 0, your switch will publish a Tasmota discovery payload. Use an MQTT Client to examine it.

  • If the discovery payload is missing information for Switch2 then the problem lies with Tasmota and should be reported as an Issue in Tasmota’s GitHub repository.

  • If the discovery payload contains information for Switch2 then the problem lies with Home Assistant’s Tasmota integration and should be reported as an Issue in Home Assistant’s Core GitHub repository.

It’s best to have the responsible party correct the error (because Tasmota’s inclusion of MQTT Discovery will eventually disappear).

1 Like

Great! :grinning: I’ll do that with mqtt explorer. Will post back later after my investigation. Thank you for your time.