Automation to set switch status at startup no longer working

I have been using the following automation for some time to get the correct status of Mqtt switches at startup.

- alias: "Power state on HA start-up"
  trigger:
    platform: homeassistant
    event: start
  action:
#    - delay: '00:01:00'
       - service: mqtt.publish
         data:
           topic: "cmnd/sonoffs/power"
           payload: ""

I noticed it stopped working today following upgrade to 0.80 and Mqtt tasmota switches are showing off when they are actually on after startup.

Manually triggering the automation after startup sorts out the correct status.

I tried a delay shown above as I suspect things are starting up slower but then there is a message that the component hassio could not be set up.

Is anyone else getting this?

Does anyone know how to trigger the automation after home assistant has been running for X time?

No. I just ignored the author’s revulshion of the retain flag and used it instead of the startup boge.

Can you tell me how you got it working without the startup script. Tasmota doesnt allow retain for state so that’s why you have to send the command with an empty payload. It then sends the current state.

I have not looked at persistence, can it be set on the built in broker?

I have been doing some testing, the sonoffs running tasmota are sending stat messages with the retain flag (powerretain 1) set but they are not being retained over a restart of home assistant so when the automation fires at startup, there is no stat message in the broker and if the sonoff hasn’t connected in time it doesn’t get the command.

Update: fixed by adding

qos: 1

to the end of the automation action, this ensures that the command is delivered when the sonoff connects.

Yes it does. Go to the sonoff web console and issue the command:

PowerRetain 1

Also set retain: true for your switch config. e.g.

- platform: mqtt
  name: "Rumpus Dehumidifier"
  state_topic: "stat/sonoff_2/POWER"
  command_topic: "cmnd/sonoff_2/POWER"
  availability_topic: "tele/sonoff_2/LWT"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  payload_available: "Online"
  payload_not_available: "Offline"
  retain: true

So now the published sonoff and home assistant messages are retained.

The example yaml I copied from the Tasmota wiki had that set already. It shouldn’t make a difference to weather messages are retained or not though.

If you are using the embedded broker, the retain flag has no effect as it does not support it. To use the retain flag, you need to use an external broker, such as mosquitto.

Retain does have an effect, if I connect to the built in broker with an mqtt client, I can confirm I see retained messages. They are not there after a restart of the broker though, maybe they are with the external broker. I believe this is called persistence though, it’s not to do with retain.

I don’t use retain in home assistant as I just want it to to follow what my switches are doing.

I suspect my original issue occured as my sonoffs are taking longer to connect with the new 0.80 version and are connecting after home assistant has started and the automation to check switch status has already run.

I resolved it by adding QoS to the automation to ensure they get the command even if they connect later. It has been running without it without issue until now. I hadn’t realised it was in the wiki.

If you upgraded Tasmota to V7 the topics changed. Maybe this helps https://github.com/arendst/Tasmota/wiki/Moving-to-v7