Send a remote MQTT payload from Home Assistant to remote sensor

Hi,

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

image

  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

sensor:
  - platform: mqtt 
    name: "Remote Deepsleep"
    state_topic: "protect/DeepSleepTSIM"
    #payload: "1234" <-- add payload value here
    unit_of_measurement: "#"
    icon: mdi:electric-switch

or

  - platform: mqtt #https://www.home-assistant.io/integrations/switch.mqtt/
    name: "Remote Deepsleep"
    command_topic: "protect/DeepSleepTSIM"
    state_topic: "protect/DeepSleepTSIM"

I cant seem to send the payload value of ‘1234’?

Any advice on how to do this please TIA.

You need a mqtt switch, not a sensor.

Thanks the second is in the switch domain but I dont see how to send a topic payload of 1234?

switch:
 - platform: mqtt #https://www.home-assistant.io/integrations/switch.mqtt/
    name: "Protech Deepsleep"
    command_topic: "protect/DeepSleepTSIM"
    state_topic: "protect/DeepSleepTSIM"

Change your configuration to this (add the last 2 lines):

switch:
 - platform: mqtt 
    name: "Protech Deepsleep"
    command_topic: "protect/DeepSleepTSIM"
    state_topic: "protect/DeepSleepTSIM"
    payload_on: "ON"
    payload_off: "OFF"

that will send “ON” or “OFF” to your mqtt topic “protect/DeepSleepTSIM” when you flip the switch.

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.

command_topic: "protect/DeepSleepTSIM"
state_topic: "protect/DeepSleepState"

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.

Thanks making some changes and will test further.