Using ESP32 to send battery telemetry data via MQTT which works well, to conserve power place the ESP32 into deep sleep and it wakes up every 5minute and sends temeletry data.
Wanting to add a switch on the EPS32, Can’t see to work out how I can make ESP32 check the state of the switch when it wakes up.
Understand I can subscribe and listen to a state change, but cant seem to work out a way to setup the broker(mosquito) to retain the last state and for the esp32 read a switch state from the broker and set the switch accordingly.
Any help appreciated.
You can simply use restore_mode: on your switch with whatever default you require.
restore_mode (Optional ): Control how the switch attempts to restore state on bootup. NOTE : Not all components consider restore_mode . Check the documentation of the specific component to understand how this feature works for a particular component or device. For restoring on ESP8266s, also see restore_from_flash in the esp8266 section.
If you really need to change the state of something in HA and then have the device set to that in ESPHome on wakeup, your best idea would be a input boolean in HA that you can then read via a platform: home_assistant EDIT: no API - oops - I’ll have to think about that…
You probably need to explain your use case a bit better so people can understand what you want to do.
But not exactly what I’m looking for (I think).
Okay here’s the setup: HA controlling a switch to a pump via a esp32.
Scenario:
ESP32 is off and last time it was powered the state of the switch was off, I then flick the switch in HA to on, ESP32 then powers up and then checks the state of the switch that is now on and subsequently switches the pump on.
Sounds like this where I need to focus:
“To subscribe to a retained message in MQTT, a client simply subscribes to the topic containing the retained message. The MQTT broker will automatically send the last retained message published on that topic to the newly subscribed client”
I think what you will find, even if you define a MQTT switch and then turn it off in HA, the ESP won’t send back a status message and thus HA will mark the state unknown.
Have a play, I think you will end up having to manually publish a topic from HA and subscribing on the ESP. In other words manually doing what a switch would do but ignoring state feedback.
Do you have two switches? one helper on HA and one on esphome to control the pump?
If you were using the API instead of mqtt, you could import the HA switch into esphome and check the state on boot or when the esp32 connects to the HA.