Tasmota correct switch status after HA restart

Hi all, I’ve recently installed a number of switches and outlets flashed with tasmota. I’m finding when I restart HA (which I do a lot at the moment due to adding new devices daily) the current status of the switches is not picked up correctly. I’ve figure out this is because the preferred method of determining switch state from MQTT appears to be:

state_topic: "inside/garage/lights/stat/RESULT"

which is updated only when the switch state changes, either via MQTT command to the switch, or pressing the switch button manually. This is great for HA normally as it updates the switch status almost instantly. However, if the switch isn’t pressed, that topic doesn’t update. Which means that when HA starts, its state is off, even if the switch is on.

So I tried using the telemetry data instead, which has a configurable update period. That works correctly as the device reports its state continuously and updates HA correctly:

state_topic: "inside/garage/lights/tele/STATE"      

However, the telemetry period needs to be set as low as possible (10 seconds) otherwise turning the switch on either via HA or manually results in a big lag between the switch status and what HA reports (up to the duration of the telemetry period).

I’m looking for anyone else’s experience or suggestions on how to handle this. So far I’ve come up with 3 ideas:

  • Setup some kind of automation so that HA correctly populates the switch status on startup with the /tele/STATE and then uses /stat/RESULT for the normal operation
  • Restore the state from the database prior to HA being restarted. Only problem I see here is that if someone manually turns a switch on/off when HA is restarting this won’t be an accuate representation.
  • Somehow reduce the telemetry period to 1 second?

TIA.

You need to set your mqtt message retain settings correctly.

1 Like

Or simply ask the state when HA starts :

- id: '1565194745362'
  alias: Status starting HA
  trigger:
  - event: start
    platform: homeassistant
  condition: []
  action:
  - data:
      payload: ''
      topic: tasmotas/cmnd/status
    service: mqtt.publish
  - data:
      payload: ''
      topic: cmnd/tasmotas/status
    service: mqtt.publish
  - data:
      payload: ''
      topic: sonoffs/cmnd/status
    service: mqtt.publish
  - data:
      payload: ''
      topic: cmnd/sonoffs/status
    service: mqtt.publish

The sonoffs is for Tasmota firmware pre 7.x, the tasmotas is for more recent firmware

As of version 0.115 there are now many more entity specific restart services, including MQTT.
You might find you don’t need to restart HA as often as you are, instead reload a particular component.

Thanks all for the thoughtful suggestions.

I ended up going with @francisp suggestion and writing a startup automation to check the real status of the switch using the STATUS11 mqtt message which then sets the current HA switch state correctly.

@tom_l thanks for the video, and normally the power retain setting would work, but there is a rare edge case where someone might turn the switch on or off while HA is restarting. This was the simplest solution, so if I would just do that if I didn’t mind the odd state error.

@jivesinger thanks, I didn’t realise could restart individual compoenents (I’ve just upgraded to 0.115 and haven’t gotten around to looking at all the improvements yet). That’s a definite time saver, but unfortunately when restarting mqtt it resets all the previous states so doesn’t solve the problem.

1 Like

If you have the retain settings correctly set HA obtains the current state from the mqtt broker as soon as it reconnects to it. No automation required.

ok, I might be missing something here. I’ve got powerretain on:

stat/RESULT = {"PowerRetain":"ON"}

and the retain flag not set in MQTT, but upon restarting HA the switch still reverts to off. What am I missing?

EDIT: Should mention that I’m manually configuring my MQTT lights and switches, not using discovery

I don’t use any retain settings on my Tasmota’s, just the automations. And everything is always in sync. I only use retain on some republished sensors from openmqttgateway (my sonoff rf bridges) and esprflink.

@francisp the only challenge I’ve found with this approach is that the /cmnd/status returns the current state in a /stat/STATUS topic which is not the same as the state topic (/stat/RESULT) that the normal MQTT switch uses. So then I had to setup another switch (“initial state”) with the state topic as /stat/STATUS and use that via another automation to set the state of the original switch. Have you found a more elegant way?

EDIT: never mind, I figured it out. /cmnd/status gives the status via a /stat/STATUS topic, but /cmnd/state gives the result with the usual /stat/RESULT which the normal MQTT switch picks up.