Need some ideas or guidance on the following please.
Have a ESP32 as a external sensor (TSIM 7600 5G/ESP32) so not in ESPHome.
Via MQTT it is connected to a operators network and sends data to Home Assistant such as temp, vibration, PIR so its acting as a remote alarm sensor.
I want the ability to send it to sleep via a MQTT payload of say “1234”. My code works below with a NodeRed flow
if (String(topic) == topicDeepSleepTSIM) {
//if (!strncmp((char*)payload, "1234", len)) { //works but use atoi
if (atoi((char*)payload) == 2163) { //restart if payload matches code
So that part is working and I can send the code 1234 and the ESP enters a deep sleep.
Now I just want a simple switch on Home Assistant Dashboard to enabled/disable
Thanks that’s works changed the payload to my code and it restarts the device as expected. Only step now is to have switch turn off i.e., send the payload and then switch off sort of toggle but more momentary on.
Cool, good to hear you are on the right path. Not an expert in how this stuff works, but what I think is the right way to do that command & state topics should be different.
Your esp listens to the command topic & when it receives the command ON, it performs its actions and then sends a payload of “ON” to the state topic. If it is supposed to turn itself off after 5 minutes, it then sends the “OFF” payload to the state topic & this will be reflected on the HA dashboard.
The different command & state topics also tell you directly that the esp is working - if it has lost connection, HA will flick the switch to ON when you push it, but after a second or so, it will flick back to OFF if it doesn’t receive a confirmation on the state topic.