Finding a way to switch.turn_off before going deep_sleep?

Hi,

I am looking for a way my esp32 to wait a bit, for this case to let the servo motor move a bit,
for a second or two, just before going deep sleep

  on_shutdown:
    then:
      - switch.turn_off: esp32_move_servo
      - delay: 5s

This didn’t work, because I knew from another thread that delay is asynchronous so it just goes to deep_sleep before the servo moves. I see from the log that the switch is triggered but not enough
time to complete it.

  on_shutdown:
    then:
      - switch.turn_off: esp32_move_servo
      - wait_until:
          condition: 
            text_sensor.state:
              id: esp32_current_time
              state: 'This is for intentional timeout. Never goes TRUE.'
          timeout: 5s

This wait_until condition didn’t work either.
The only way I can think of is to calculate the time to trigger this switch after wake up,
say like (run_duration - 5 seconds), before the deep_sleep process kicks in.
But is that the only way I can achieve it?
A fake 5s switch perhaps?

Thank you.

Hi
I’m bit confused about your servo command. I haven’t used servos in esphome , mostly just relays.

to make a servo move esphome documents say use:

  then:
  - servo.write:
      id: my_servo
      level: -100.0%

to stop a servo use

on_...:
  then:
  - servo.detach: my_servo

In your case does switch.turn_off allow the servo to move to some default position?

@Spiro

Yes. That is how that switch, esp32_move_servo is made of.
It is made to behave like the arm of switchbot’s move.

By the way, do you know if switches are synchronous so that deep_sleep would wait until the switch completes?

When the switch/servo then returns to the position you can tell how it that reported on HA e.g. closed. You could set up an automation to publish an mqtt message to topic esp32/servo_position to tell it to only go into deep sleep then.

  on_message:
    - topic: esp32/servo_position
      payload: 'CLOSED'
      then:
        - deep_sleep.enter: deep_sleep_1        

you would have to set your sleep time default to longer

deep_sleep:
  id: deep_sleep_1
  run_duration: 30s
  sleep_duration: 60min 

In one of my esp devices when I want to run a pump by relay I sent an mqtt to turn off deep sleep until the pump has finished.

I use the mqtt message to also turn off deep sleep when I want to run OTA the next time it wakes up.

@Spiro

Thank you so much for your help.
I would like to implement your idea, but the way I would like to move the servo is not to deep_sleep
after the servo moves, but at the time just before it goes deep_sleep.
It is different behavior because I still need to control the time when to move the servo, with your idea.
Please correct me if I am wrong.

For my case, run duration is constant for 5min, and I want 5 minutes to perform other tasks.

Thank you.

I think if you run deep sleep just before the servo moves then the esp will not be able to move the servo unless the servo can move back to the desired position without power. The esp can’t do anything while it is in deep sleep except wait until it wakes again.

Thanks.

Yes that is what is happening now.
Your idea makes me better think to issue the closed payload from an HA automation.
I wanted to script this locally at esp32, but seems feasible other way around.

I will test it and will let you know the outcome.

Thank you.

If you need any help with automations or esp I will try to help. We all learn as we help. There is nothing like working out the details by yourself. :+1:

1 Like

@Spiro

I ended up using your idea and it is working great. Thanks!!

Now the whole timing is controlled via HA’s automation,
so that esp32 goes deep_sleep, after the physical move of the servo is completed.
The point I understood from this case is that HA’s delay parameter is synchronous,
so it would correctly wait before it fires the next command (deep_sleep).