Battery powered device with switch - can I change the switch during deepsleep?

Hello,

I want to have a battery powered ESP8622 with a switch component. Can I somehow achieve that the switch can be operated at any time, but will only take effect once the ESP restarts from deepsleep? At the moment I get an error message when I want to change the state of that switch during deepsleep.

Regards,
Anduril

Never tried it but perhaps something like a boolean that you flip.
This boolean triggers an automation to wait for the device to reconnect, when it has reconnected it sends the state change.

I just talked about my problem with a friend and he pointed me to a solution:

I created a helper in HA called wanted_state and then the following in ESPHome:

binary_sensor:
  - platform: homeassistant
    entity_id: input_boolean.wanted_state
    id: wanted
    publish_initial_state: true     # This is important!
    on_state:
      then:
        if:
          condition:
            lambda: return x;
          then:
            - logger.log: "wanted on"
            - switch.turn_on: switch1
          else:
            - logger.log: "wanted off"
            - switch.turn_off: switch1

just in case someone ever has a similiar problem…