Tasmota POW Switch Status in HASS Front End - PowerRetain

I have a sonoff switch running tasmota (5.12.0). I can see all the stats etc in the HASS (0.65.3) front end perfectly fine. What is bothering me is I have powerretain 1 set on the sonoff POW and it does not appear to be working. I can see it ‘on’ in HASS but once I reboot HASS, it goes back to off. I can see in MQTT.fx the retained Power ON state on the MQTT server (mosquitto running in Docker). But HASS seems to ignore it. FWIW: I’m using MQTT discovery to pick up the sonoff in question.

any ideas?

This log from the sonoff is right after I toggled it:
image

Screenshot from MQTT.fx - shows retained and POWER On

HASS Front End After the Toggle of the sonoff POW - shows on (which is correct):
image

HASS Front End After HASS Restart - shows off (not correct):
image

MQTT config from configuration.yaml

mqtt:
  broker: 10.0.10.194
  discovery: true
  discovery_prefix: homeassistant

I use this automation to set the correct state after reboot of pi.

#-----------------------------------------------------------------------------------------------
# Set Sonoff switch states after restart
#-----------------------------------------------------------------------------------------------
  - alias: 'Set McSonoff01 Switch on at boot'
    initial_state: 'on'
    trigger:
      platform: homeassistant
      event: start
    condition:
      condition: state
      entity_id: sensor.mcsonoff01_power
      state: "ON"
    action:
      service: switch.turn_on
      entity_id:
        - switch.mcsonoff01
#-----------------------------------------------------------------------------------------------
  - alias: 'Set McSonoff01 Switch off at boot'
    initial_state: 'on'
    trigger:
      platform: homeassistant
      event: start
    condition:
      condition: state
      entity_id: sensor.mcsonoff01_power
      state: "OFF"
    action:
      service: switch.turn_off
      entity_id:
        - switch.mcsonoff01
#-----------------------------------------------------------------------------------------------
1 Like

I assume you have the switch configured like in the tasmota wiki?

That’s an interesting solution to the problem - I hate band-aiding something like this that ‘should’ be working from what I can tell - but I might run down this path if needed - thanks for the idea!

@VDRainer - thank you for that link - much appreciated. So if I’m using discovery, I guess that’s out the door if I want retained? I tried setting retain: true under MQTT component the other night - and HASS throws an error when it started as a result. So if I can’t use discovery, so be it - but trying confirm that it can or cannot be used if I want retain.

Thanks for all the help and input!

Hey @vexter0944 how did you solve it? I have the same problem when using tasmota with autodiscovery. The PowerRetain flag is set to On, but HA only discovers the device itself, not its power state - although i can confirm that the topic is set, indeed.
I could use @mcfrojd s approach now, but what’s the point of autodiscovery if i still have to add each new device to the config somewhere? For now, i am resigning from autodiscovery and specify each device manually in the MQTT switch config. This requires me to add each device manually as well, but it’s less lines of code than to add a bunch of automations to the config - but no approach feels like a real solution, just like a workaround.

I ended up just using automation to set the power state of the switch to on when HASS reboots. Not my ideal situation, but it does work.

I just ran into the same problem. My solution was to create an automation that is triggered when Home Assistant starts and performs a mqtt.publish to “cmnd/sonoff/POWER2”. This causes tasmota to update the status of “stat/sonoff/POWER2” (assuming your state_topic: stat/sonoff/POWER2)

- id: '1545179894309'
  alias: Tasmota Status Update
  trigger:
  - event: start
platform: homeassistant
  condition: []
  action:
  - delay: '60'  #This may not be necessary
  - data:
  topic: cmnd/sonoff/POWER2
service: mqtt.publish

I only have 1 Tasmota device right now but if I end up with more than a few I’ll probably throw the mqtt.publish into a script and have the automation call it.

Hope this helps others running into this issue.

Is this still up to date?

I was just tinkering with the auto discovery.
Then I tested the behaviour with “powerretain on” for some sonoff devices and a home assistant restart.
It does not work.

Adding a automation for every device ( like 10) or just configuring them manually seems like almost the same effort.

I think I will switch back and won’t use auto discovery of this is the case.