How to Restart a Tasmotized sonoff after HA Restarts using an automation?

Hi All,

Having trouble with binary sensors that do not report the correct state after a HA Restart. I know there are quite of few posts around that ask this question but the solutions given do not seem to work for me at the moment.

I looked at this post Sonoff binary sensor change lost during HA restart and found this automation from @francisp

- id: '1565194745362'
  alias: Status bij opstarten HA
  trigger:
  - event: start
    platform: homeassistant
  condition: []
  action:
  - data:
      payload: ''
      topic: cmnd/Qia/restart
    service: mqtt.publish

Now the automation (once I modify it for my use) triggers and does publish the topic but the sonoff reports:

20:31:15 CMD: restart
20:31:15 MQT: stat/sonoff20/RESULT = {"Restart":"1 to restart"}

So I researched that and yes Restart 1 should be sent.

I modified the automation and again it triggers but the sonoff reports:

20:34:55 MQT: stat/sonoff20/RESULT = {"Command":"Unknown"}

So to help diagnose the problem I used dev tools/mqtt to publish the topic.

This still produces MQT: stat/sonoff20/RESULT = {“Command”:“Unknown”}.

So after quite a number of visits to various resources I tried this:

Which had the desired effect of rebooting the sonoff.

See @ 19:35 the sonoff is attempting connection…after the successful restart!

Then I reworked the automation thinking that this was going to work:

- id: 'UpdateBinarySensorStatus'
  alias: Update Binary Sensor Status
  trigger:
  - event: start
    platform: homeassistant
  condition: []
  action:
  - data:
      payload: ''
      topic: cmnd/sonoff20/Restart 1
    service: mqtt.publish

However if you read the line @19:38 in the sonoff console above you can see it has again failed…I am at a loss now at what to try next really so hoping someone can put me on the right track?

Cheers!

- id: 'UpdateBinarySensorStatus'
  alias: Update Binary Sensor Status
  trigger:
  - event: start
    platform: homeassistant
  condition: []
  action:
  - data:
      payload: '1'
      topic: cmnd/sonoff20/Restart 
    service: mqtt.publish
1 Like

How the hell did I not see that…lol

Cheers Francis @francisp…I’m off to try it now and will report back again.

EDIT: As expected that did the trick…Thanks very much!

You don’t need to restart you device to make it report the state. You can send an empty payload and it will report back. This is a script that runs when HA starts:

update_sonoff:
  alias: Update Sonoff at Start
  sequence:
    - service: mqtt.publish
      data:
        topic: "cmnd/family/power"
        payload: ""
    - service: mqtt.publish
      data:
        topic: "cmnd/sonoff7/power2"
        payload: ""
1 Like

@oriolism cheers for posting!
I like the idea of a sequence:
I feel certain I could use that to target a number of sonoffs that need to update HA after a restart.

I am puzzled about what triggers your automation though?
Or is this just a snippet to be filled in?

EDIT:

I tried your script? automation but it failed to trigger.

I looked at the initial automation which was restarting the sonoff and modified it as you suggested to just send the empty payload to Power1 and Power2.

#**********************************************************
# Restart sonoff on HA restart
# sonoff20 Power1 Power2
# Other?
#**********************************************************                              
- id: 'UpdateBinarySensorStatus'
  alias: Update Binary Sensor Status
  trigger:
  - event: start
    platform: homeassistant
  condition: []
  action:
  - data:
      payload: ''
      topic: cmnd/sonoff20/Power1
    service: mqtt.publish
  - data:
      payload: ''
      topic: cmnd/sonoff20/Power2
    service: mqtt.publish

This works perfectly for my purposes so thanks to you both ( @francisp and @oriolism ) for this solution.

MQTTPublishRestart03%20(2)

This is great, I have a few Shelly 1’s that I’ve Tasmota’d (before Shelly had native MQTT support) and whenever I’m messing with my WiFi config and the Unify AP the Shelly is connected to needs to reboot, the Shelly doesn’t reconnect on it’s own forcing me to power cycle it. This will be a lot easier and less intrusive to those consuming electricity in the house. :wink:

Thanks,

-joni-

1 Like

If your shelly is not connected to wifi, it will probably not recieve the mqtt command. You might check out this solution : https://groups.google.com/forum/#!category-topic/sonoffusers/O27xAaANMkw

1 Like

This the automation that I use to trigger the previous script.

- id: Update_Sonoff_Start
  alias: 'Update Sonoff at Start'
  initial_state: true
  trigger:
    - platform: homeassistant
      event: start
  action:
  - delay: 00:00:15
  - service: script.turn_on
    entity_id: script.update_sonoff
1 Like

@oriolism Cheers…I see how it all comes together now. I see you have a delay of 15 seconds… should I be doing that as well do you think? What was your thinking with adding that?

I have tested how mine works probably 10 times and it was 100% result for having the sonoff update.