On_turn_on and delayed off

Probably a silly question.

If I have an automation in my YAML that turns on a switch, then an automation on the switch so that on_turn_on it delays for 20 minutes and then turns off, how does it handle extra turn_ons?

For example I have a PIR to turn on a relay, then the relay starts its 20 minute delay before turning back off. If the PIR triggers again after ten minutes will ESPHome register a second on_turn_on event and start the 20 minutes again resulting in the relay being on for 30 minutes, or will the turn_on be ignored as the switch is already on?


It depends how you define the delay.

If using wait or delay in the actions of an automation the delay will be “jumped over” if the automation re-tirggers and actions following after the delay will be immediately executed.

The way around this is to use for: in the automation trigger.


  platform: state
  entity_id: switch.relay
  to: 'on'
    minutes: 20
    service: switch.turn_off
    entity_id: switch.relay

Thanks, but I think you’re talking about HA automations. Don’t think I was clear enough, I meant in the YAML used to compile the ESPHome firmware.

My mistake. I didn’t read the category. Sorry.

I’m not actually sure for the ESPhome case.

Hello there!

I am not really sure where you define the delay. I understand that you register the PIR motion event and start the relay toggle and a timer?
Or does the relay depend on the signal by the PIR itself?

Some time ago i had a setting where i had a PIR motion event and then the followong action on my ESP and toggled a switch depending on the state of the PIR. And according to the GPIO Binary Component i set the delay in the PIR itself. For example:

  - platform: gpio
    pin: D2
    name: ...
      - delayed_off: 20min

This way if the PIR triggers on and then (depending on the PIR off after i.e. 3s) the off signal is delayed for 20min. If the PIR is triggered again in this timeframe, the off signal is delyed again for the above value. So in your case described above the signal from the PIR would stay on for 30min and the toggle the switch afterwards.

But i don’t know how the delay defined in the switch itself would behave…

Thanks. Will test myself and report findings, as soon as I get the chance.

Still haven’t had the chance to test this but it does say in the ESPHome docs:

This is a “smart” asynchronous delay - other code will still run in the background while the delay is happening.

That suggests that what I’m trying to achieve will work. I’m trying to make a relay for my burglar alarm siren switch off after 20 minutes. So if under my PIRs I switch the relay on motion detected, delay 20 minutes and then switch off the relay it will do one of two things on a second trigger during the 20 minute delay:

  1. The automation not to trigger for a second time as it’s already running, though it is possible other automations can run during the delay.

  2. The automation fires a second instance i.e. the relay is already on and it will count down to turning it off from the first trigger, but it will start a second instance of the automation i.e. trying to turn on the relay (already on during first delay so nothing happens) then it will count down to turning it off 20 minutes later, by which time it will already be off from the first trigger’s delay so again it does nothing.

Either way I would get desired behaviour so is good, i.e. relay turns off 20 mins after the first trigger, not the last.

Fingers crossed for testing :laughing:

Thanks for that. I wouldn’t want to use the delayed_off function though as I may want the ability to use the PIR for other things. If I lock it in the on state for 20 minutes it won’t be usable.

I was literally looking at an automation for a turning on event under the PIR to switch the relay on, wait 20 minutes and switch it off again, on the condition that the system is armed.

EDIT: Worked as predicted.

1 Like