Hello,
I have a sensor that wakes up from sleep, takes a few measurements, publishes MQTT messages, and goes back to sleep right away. Unfortunately, I cannot figure out how to ensure MQTT messages have made it through. Right now, to make things work, I introduce a an arbitrary delay before entering sleep, but this is brittle, and may not work on slow networks. It is also not power efficient on fast networks. Is there a way to ensure MQTT is idle (no messages pending, nothing to receive (like OTA messages), before going to sleep? At least for the send part, if sensor values are sent with qos=1, esphome can wait to ensure all pending messages have been delivered before entering sleep. However, this does not seem to be happening now.
Something like a mqtt.idle
flag, used like this:
globals:
- id: collected
type: bool
restore_value: no
initial_value: 'false'
sesnor:
on_value:
then:
- lambda: 'id(collected) = true;'
script:
- id: loop
then:
- delay: 1ms
- if:
- and:
- mqtt.connected:
- lambda: 'return id(collected) == true;'
- mqtt.idle:
then:
- script.execute: enter_sleep
- script.execute: loop