Automation help, turn off switch after a period of time

This is really two questions. First one, I have the following automations:

- id: 'butt_truffles_kitchen'
  alias: Butt Truffles Kitchen
  trigger:
    platform: state
    entity_id:
      - switch.kitchen_bathroom_fan
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 20
      seconds: 10
  action:
    service: switch.turn_off
    data:
      entity_id:
        - switch.kitchen_bathroom_fan
- id: 'butt_truffles_master'
  alias: Butt Truffles Master
  trigger:
    platform: state
    entity_id:
      - switch.master_bathroom_fan
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 20
      seconds: 10
  action:
    service: switch.turn_off
    data:
      entity_id:
        - switch.master_bathroom_fan

Those will turn off bathroom fans after 20 minutes of being on. Since those are exactly the same, just different switches, how can I combine those in to a single automation? I could list all the entities under each section, but Iā€™m assuming that if one fan hit 20 mins and triggered the turn_off, it would turn all the fans off and not just the one that hit 20 mins.

Second question: How can I make that one more reliable? It seems that while Iā€™m actively testing it, it works exactly as designed. However, the next day it seems it no longer works and the fans stay on indefinitely unless I go play with something in HA. If I reload automations, it starts to work again. I think really what I would like is instead of triggering on the state change from off to on, I would like to just turn off after itā€™s detected itā€™s been on for 20 minutes. Is this possible? Or what would be a more reliable way to do this same thing?

so I donā€™t use automations, so Iā€™m not 100% sure if you can combine the entity_id section of the trigger. I would assume it would treat the entity_id list as ā€˜orā€™ but it could treat it as ā€˜andā€™. You might have to experiment.

- id: 'butt_truffles_kitchen'
  alias: Butt Truffles Kitchen
  trigger:
    platform: state
    entity_id:
      - switch.kitchen_bathroom_fan
      - switch.master_bathroom_fan
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 20
      seconds: 10
  action:
    service: switch.turn_off
    data_template:
      entity_id: "{{ trigger.entity_id }}"

Worse case scenario is that you have to have 2 triggers.

I have to ask, what is ā€˜Butt Trufflesā€™?

2 Likes

Lol, this automation is so when someone goes in to use the restroom, they turn on the fan, use the restroom and can close the door and leave the fan on so the stink doesnā€™t get out. The fan does two things, pulls in fresh air more quickly than just letting it naturally come in, and it serves as a warning to others that if the fan is on, enter at your own risk. My naming is just to entertain myself :grinning:

Thanks, Iā€™ll give this a try. I think

data_template:
      entity_id: "{{ trigger.entity_id }}"

Is what I was missing on my original attempts.

It seems that doing it this way it sort of works. If only one switch is turned on, it works fine. If one switch is turned on, then a few seconds later (before the first one turns off) another turns on, it will only act on the first switch and not the second.

You may want to keep the automations separated if itā€™s not behaving the way you want it to. Itā€™s not bad to have multiple similar automations. Itā€™s just more overhead. You could move to appdaemon to handle your automations and you could combine these two. But appdaemon runs off python, so you need to know python.

Thanks, Iā€™ll look at AppDaemon. Python is not an issue, I live in python (be that good or bad) all day for work. I hate duplicating code, so thatā€™s why I was trying to reuse the same automation but if it just canā€™t do it then Iā€™ll just have to duplicate. Thanks for your help.

if you know python, youā€™ll end up switching to all appdaemon. I did it a few years back, and itā€™s worth it. Especially if you have 10 of the same automation. Or if you want to perform any for loop.

Thanks. Iā€™ve installed AppDaemon but hadnā€™t yet looked at it. Iā€™ve also had many times that Iā€™ve wanted to loop through something, sounds like I should just figure out AppDaemon.

The first solution @petro provided above should work, but doesnā€™t (sometimes) because of a bug that has been in the state & numeric_state triggers for a long time. I just fixed them both. (See PR 24904 & PR 24910.) Should be in 0.96.

To explainā€¦ If you have multiple entities listed when using to & for, an independent ā€œtimerā€ will be started for each entity when it satisfies the trigger criteria (i.e., to, and possibly from.) It shouldnā€™t matter what the other entities do during that time, as long as its own state doesnā€™t change during that period. If it does change, the ā€œtimerā€ will be canceled. If it stays the same, then when the ā€œtimerā€ expires the automation will ā€œtrigger.ā€

But due to the bug, however, if another entity changed in the scenario described above, and the other entityā€™s new state value differed from the to state, then that would incorrectly cancel the first entityā€™s ā€œtimer.ā€ This is what you were experiencing.

1 Like

This ā€œtimerā€ā€¦ how exactly does it work?

  1. Is the clock state within the running instance of HA? What happens if you want something to trigger at 5pm and running for 1h and HA was restarted at say 5:30pm - when it restarts, does it look for the last state change, ie that it was at 5pm and that there are 30 minutes left and it will set the clock for 30 minutes instead of an hour?

  2. Letā€™s say for the above example we are talking about a simple on/off state. What happens if within the hour the state was ā€œunavailableā€ / ā€œofflineā€, whatever the sensor reports when it canā€™t get the value. Would this change reset the ā€œtimerā€ or would it consider this bad state a blip and continue with however many minutes are left on the 1h clock?

The timer used to implement the for: option will not span a HA restart. All automation triggers reset when HA starts.

Once the trigger: criteria is met, the for: timer starts. If the state of the entity changes such that the trigger: criteria is no longer met, before the for: timer expires, then the trigger, and hence the for: timer, will be reset. Changing to unavailable would probably cause the trigger to reset (unless, of course, the trigger was ā€œto: unavailableā€.)

1 Like