Setting MQTT Payloads via Button for Deep Sleep OTA on ESP32

Hi,

I have been reading this excellent topic

and also the documentation on the espHome pages which mention:

deep_sleep:
  # ...
  id: deep_sleep_1
mqtt:
  # ...
  on_message:
    - topic: livingroom/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.prevent: deep_sleep_1
    - topic: livingroom/sleep_mode
      payload: 'ON'
      then:
        - deep_sleep.enter: deep_sleep_1

I have the unit working well, it sleeps, wakes up, sends info, goes back to sleep. No problem. However, I cannot force it to remain awake for an OTA update

The only bit I really cannot figure out is how to make the MQTT broker set the payload to send. I have been trying to create a toggle switch to then set the payload to ‘ON’ or ‘OFF’ but I just cannot seem to figure out the YAML to do this. Does anyone have an example of using a button to interact with the MQTT broker please?

I have looked at this topic, which is also very good, but I am trying to get this working with MQTT which is not covered here.

This is my first post so I hope this fits the rules.

Thanks

Ok, I have figured this out. Below is what I did in the end. It’s a very basic guide aimed at people like me who are just learning how things work with HA.

Premise:
You want to use ESP32 deep sleep on a battery powered device that can be remotely updated from HA.

Kit:
ESP32 Devkit using ESP Home plus whichever sensors you like. I am not covering the sensor set up here just the Deep Sleep side of things.
Home Assistant running on RPi4
Mosquito MQTT Broker installed as an integration in HA.

ESP Config:
ESPHome configuration.yaml needs to have these lines documented here in the mqtt set up:


mqtt:
  # ...
  on_message:
    - topic: mybatteryesp/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.prevent: deep_sleep_1
    - topic: mybatteryesp/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.enter: deep_sleep_1

This will make the unit check the MQTT topic mybatteryesp/ota_mode on your MQTT broker when it comes online.

Setting up MQTT Topics to prevent Deep Sleep

You need a way of setting a topic payload to ‘ON’ or ‘OFF’. If the payload is ‘ON’ you can stop deep sleep and do some OTA work. You can then resume deep sleep by setting the payload to ‘OFF’.

A way of doing this is by creating a toggle switch entity then tying that to an automation. The key here is you can use web interface to achieve all this you DONT need to mess about with YAML files to get something basic going.

  1. Create a toggle helper by going to
    HA SIDEBAR → SETTINGS → DEVICES & SERVICES → HELPERS (at the top) → CREATE HELPER. In the dialog scroll down a bit and find ‘Toggle’.

Give it a relevent name and save it. You should now see something like this:

  1. Create two automations, one for switching the OTA mode ON one to switch the OTA mode off. To get to automations got to
    HA SIDEBAR → SETTINGS → AUTOMATIONS & SCENES → AUTOMATIONS (along the top) → Press ‘Create Automation’ button on bottom right. You need to create one that switches OTA Mde on i.e. state of the toggle entity goes from ‘OFF’ to ‘ON’ and one that switcheds OTA Mode off i.e. state of the toggle entitiy goes from ‘ON’ to ‘OFF’

Automation to switch OTA on:

Automation to switch OTA off:

For me this was the missing link and the important bit:
The ‘When’ settings link the Toggle to the MQTT settings through the ‘Then Do’ settings.

Add a OTA Control Toggle to your dashboard
If you then add the toggle to an appropriate place on your Dashboard you can enable and disable OTA at will. To do this go to the three dots far right on the top banner of the dashboard. Select Edit Dashboard then Add Card button on bottom right. Select ‘Button’ then in the Entity pull down choose the toggle entity you created above. Once you are happy with the icon and names etc press save on bottom right and you should now see the button on your dashboard.

It should be noted that

  1. There are many other things you can do to improve this in terms of automations. This is just a very basic guide that taught me how to link entities and automations together. I was having real trouble with the YAML examples in the documatation and forums and this way skippe all that.
  2. You can’t force the ESP to wake up. You just need to switch on OTA mode then wait for the next wake up time. If you have a unit that only wakes up every hour then you may be waiting up to 60 mins for it to pop up online.

I hope this helps someone.