Best way to switch off a switch after 4 hours?

Hi All,

Still new to esphome and trying to learn the proper syntax. I’m trying to make a toggle in HA that sets off a built-in timer within ESPHome that turns off a light that i have on a relay after 4 hours. I saw in the docs an example of using ‘delay’. Would that be the best to do here, maybe something like this?

Thanks for the help!

    - platform: template
      name: "Test Timer"
        - switch.turn_on: postlight
        - delay: 4h (maybe this needs to be in seconds?)
        - switch.turn_off: postlight
        - switch.turn_off: postlight

Currently (may change soon, or at least you’ll have options) when you call an automation that is already running : it will say “hey, I’m already running, you want to hurry me up, okay let’s just jump to the action part” (not literally but you get the idea)
So an automation that turns a cupboard light on for 10 mins when you open the door.
Open door - light on for 10 mins
Close door - light stays on for 10 min

Open door - close door - “oooh, I left my phone in there”
Open door - light goes off immediately

So you are best to have 2 automations
Open door - light on

Light goes on - (for 10 mins) - light goes off

I emphasise the “FOR” as it’s the command you need

so, I’ve almost got this acting right but when i add in an extra relay to turn on with an additional delay…within HA, the toggle switches on, then immediately off, but the delay still functions as expected, do I need to return something as True to keep the template switch ‘on’ within HA?

What I’m basically trying to do is say, when I turn ‘on’ the 4 hour timer, I want the toggle within HA to stay on for the duration of the timer and then turn off, and if i need to stop the timer I can just go and manually turn it off within HA.

    - platform: template
      name: "Test Timer"
        - switch.turn_on: postlight
        - delay: 30s
        - switch.turn_on: postlight2
        - delay: 4hours
        - switch.turn_off: postlight
        - switch.turn_off: postlight2
        - switch.turn_off: postlight
        - switch.turn_off: postlight2

This looks quite complicated to me, and I usually don’t trust delay much (doesn’ŧ survive HA restarts, gets cancelled when new trigger comes in).

I have a fan that shall go OFF when either the temperature falls below 23.5 °C (as measured on two thermometers) or it has been running for 4 hours (the “4 hours” triggered me to read this post, hee hee):

  # switch fan off
  - alias: "Lüfter Wohnzimmer aus"
    initial_state: true
      - platform: numeric_state
          - sensor.innentemperatur
          - sensor.sensor1_temperature
        below: 23.5
          minutes: 2
      - platform: state
        entity_id: switch.switch1
        to: 'on'
          hours: 4
      - service: homeassistant.turn_off
        entity_id: switch.switch1

As you see, I simply use for: in the trigger section, so that the switch goes off only when:

  • The temperature has been below 23.5 °C for 2 minutes, or
  • the switch (i.e., the fan) has been on for 4 hours.
  • … you switch it off manually.

And of course you can use switch.turn_off instead of homeassistant.turn_off.

This construct never failed me (yet), and seems to me much more reliable than using delays.

EDIT: Oops. Only after typing all this, I realize that you were talking about ESPHome … Sorry. :frowning: The syntax is so d**n lookalike. Well, maybe ESPHome also supports “for”? :wink:

No worries, thanks for looking anyways!

I’ve got this mostly working but it wont shut off after 4 hours, I am intermitently losing wifi on it though and I’m wondering if thats causing the issue. When it disconnects from wifi the lights stay on, but the timer gives up.

Yeah you should always be apprehensive to using any ‘delay’ functions on an esp8266. The wifi parts need to be constantly doing something to keep the connection alive, and sometimes delay() functions can interrupt those activities (delay them too long) to where you loose wifi connection as a result. While the latest esp8266 libraries are supposed to use an async version of arduino delay() (thus allowing wifi to do it’s thing in the background during a delay), it’s still not always full proof.

So this might be something that would be better off handling purely in HA automations (or nodered if you do that), rather than by the esp itself. I know I also always try to keep everything on the esp to keep things functioning even when the wifi is dead, but that’s all stuff that doesn’t have long delays (like toggling relays when switches are hit, etc). Anyways, with a 4hr delay, it is unlikely your router and HA would be down for that long… and if it was the light being on would be your notification hehe.