Strange problem with automation and presence detection timing addition

So, I have an over arching automation to control power to our AV systems at home when we are there and not there. Conditions with presence detection work in all cases except one that is intended to wait for 45 (short delay in place for testing) minutes before taking action. If the “for” keyword is used the action never fires, if the keyword is removed it fires as it should.

According to the docs, this should work.Conditions

Here is the automation, sorry but it is long and has some comments where I have tested things and failed. The set that fails is condition 1.

- id: tv power control
  alias: TV power control
  initial_state: true
  trigger:
    - platform: state
      entity_id:
        - group.family
    - platform: time
      at:
        - "00:30:00"
        - "06:30:00"
  mode: restart
  action:
    - choose:
### Turn TV's on @ 6:30am and before 12:15am if someone home 
        - conditions:
            - condition: state
              entity_id: group.family
              state: "home"
            - condition: state
              entity_id: 'switch.bnc60_2_switch'
              state: 'off'
            - condition: time
              after: '06:30:00'
              before: '00:15:00'
          sequence:
            - service: switch.turn_on
              entity_id:  switch.bnc60_2_switch
            - service: switch.turn_on
              entity_id:   switch.tg_02
            - delay: '00:02:00'
            - service: media_player.turn_off
              entity_id: media_player.yamaha_receiver
### Turn TV's off if nobody home for 45 minutes
        - conditions:
            - condition: state
              entity_id: 'media_player.sony_bravia_tv'
              state: 'off'
            - condition: state
              entity_id: group.family
              state: "not_home"
              for:
                hours: 0
                minutes: 01
                seconds: 00
          sequence:
            - service: switch.turn_off
              entity_id:  switch.bnc60_2_switch
            - service: switch.turn_off
              entity_id:   switch.tg_02
### Turn TV's off after 12:30am for the night
        - conditions:
            - condition: state
              entity_id: 'media_player.sony_bravia_tv'
              state: 'off'
            - condition: state
              entity_id: group.family
              state: "home"
            - condition: numeric_state
              entity_id: " sensor.tg_02_energy_current"
              below: .1
            - condition: time 
              weekday:
                - mon
                - tue
                - wed
                - thu
                - fri
                - sat
                - sun
            - condition: time
              after: "00:30:00"
              before: "06:30:00"
          sequence:
            - service: switch.turn_off
              entity_id:  switch.bnc60_2_switch
            - service: switch.turn_off
              entity_id:   switch.tg_02

If I leave the delay in and change the state of group.family to “not_home”, this is my trace of the automation:

If I comment out the delay and change the state of group.family to “not_home”, this is the trace:

Thing is, I don’t see a delay anywhere. I only see a condition check. The for only checks if it’s valid at that moment. If you want a delay, use a delay :smiley:

The error from the trace isn’t helping though. Choose: Error: undefined simply means no condition was met so it would like to do the default case which is empty. Think that’s fixed in 2021.5

I misspoke, delay was a poor choice of words. Yes, it breaks when I am checking on the state remaining for X minutes.

If you want it to happen after 45 minutes of being from home you not only have to check for that in the condition but also be sure the automation is actually triggered after 45 minutes :wink: Now, it’s only triggered when it actually changes at which time the “it’s been 45 minutes” will never be true.

So, a cat and mouse game… Short of a time based trigger, lets say /5… how would one make this happen? What is the preferred method? Why is “for” an option if it works that way? Maybe start a timer as part of the sequence and then either allow it to finish or cancel it if someone returns home before the end of the timer is reached? I will have to try that.

What started this problem? My wife walks a bunch and would leave wifi and gps geofence range and the tv/av/cable boxes shutdown and then take 5 or 6 minutes to boot when she gets home… She grumbled about the wait as a reboot of the main box knocks out all of the slave boxes. So, 45 minutes is an arbitrary time delay to prevent the frequent short outages?

Not really, for also works perfectly fine on triggers as well.

And why it works like this? It’s the most flexible. The trigger might has no direct relation to the condition. You might want to trigger something if you open the front door (trigger) when your light where on for 30 minutes (condition).

But in your case they are the same. You want an action after 45 minutes of nobody home (trigger), but because you have other triggers as well, you want to check that (condition) before doing something. In this case, just make a trigger as well:

  alias: TV power control
  initial_state: true
  trigger:
    - platform: state
      entity_id:
        - group.family
    - platform: state
      entity_id:
        - group.family
      to: "not_home"
      for:
        hours: 0
        minutes: 45
        seconds: 00
    - platform: time
      at:
        - "00:30:00"
        - "06:30:00"
  mode: restart
  action:
    - choose:
### Turn TV's on @ 6:30am and before 12:15am if someone home 
        - conditions:
            - condition: state
              entity_id: group.family
              state: "home"
            - condition: state
              entity_id: 'switch.bnc60_2_switch'
              state: 'off'
            - condition: time
              after: '06:30:00'
              before: '00:15:00'
          sequence:
            - service: switch.turn_on
              entity_id:  switch.bnc60_2_switch
            - service: switch.turn_on
              entity_id:   switch.tg_02
            - delay: '00:02:00'
            - service: media_player.turn_off
              entity_id: media_player.yamaha_receiver
### Turn TV's off if nobody home for 45 minutes
        - conditions:
            - condition: state
              entity_id: 'media_player.sony_bravia_tv'
              state: 'off'
            - condition: state
              entity_id: group.family
              state: "not_home"
              for:
                hours: 0
                minutes: 45
                seconds: 00
          sequence:
            - service: switch.turn_off
              entity_id:  switch.bnc60_2_switch
            - service: switch.turn_off
              entity_id:   switch.tg_02
### Turn TV's off after 12:30am for the night
        - conditions:
            - condition: state
              entity_id: 'media_player.sony_bravia_tv'
              state: 'off'
            - condition: state
              entity_id: group.family
              state: "home"
            - condition: numeric_state
              entity_id: " sensor.tg_02_energy_current"
              below: .1
            - condition: time 
              weekday:
                - mon
                - tue
                - wed
                - thu
                - fri
                - sat
                - sun
            - condition: time
              after: "00:30:00"
              before: "06:30:00"
          sequence:
            - service: switch.turn_off
              entity_id:  switch.bnc60_2_switch
            - service: switch.turn_off
              entity_id:   switch.tg_02
1 Like

OMG, that is so simple. I could not see the forest for the trees! I will get that tested, I never thought of looking for any state change and a specific state change for the same entity.

Thanks a bunch!