ESPHome device go online every 5 minutes, do an action and disconnect

I have an ESPHome device that i want to connect to wifi every 2 minutes, get some values from HA, reply that received the values and then disconnect the wifi until the next 2 minutes.
I have done a lot of searching but the fact that the device is disconnected make the whole process difficult…
I would like to do something like:

interval:
  - interval: 120s
    then:
      - wifi.enable:
      - delay: 40s
      - service: switch_service
        variables:
          my_state: bool
          my_number: int
          my_string: string
        then:
          - lambda: |-
              if ( my_state ) {
                  id(relay).turn_on();
                  ESP_LOGD("custom", "STATE ON curTime: %s", my_string.c_str());
              }
              else {
                id(relay).turn_off();
                ESP_LOGD("custom", "STATE OFF curTime: %s", my_string.c_str());
              }
              api.template("my_string_sensor").send_state("i received the value");
      - wifi.disable:

What is the best way to do that?
I guess i could use MQTT also, but according to documentation, the api is preferred (although going offline causes many problems).

Would deep sleep work for your application?

Some of the ESPHome deep sleep features make this sort of thing a little easier.

Tracking data updates is still a little clunky in my opinion though.

Unfortunately i cannot use deep sleep because I use Sonoff s26 that hasn’t the wiring for wake up

1 Like