Action chose wont execute trigger when holding state logic is added

I am always so close. I have an automation using the chose statement and I want a different action depending on whether a switch is on or off. The automation works fine when I remove the “for:” criteria. But I only want the automation to trigger when the switch is on for 30 minutes . Thanks in advance
I am getting this error:
Invalid config for [automation]: [to] is an invalid option for [automation]. Check: automation->condition->0->conditions->0->to.

Commenting it out works
entity_id: switch.spiral
state: 'on'
       # for:
       #   minutes: 30 
I also tried but no luck :
to: "on"
for: "00:30:00"
  alias: kidinbed
  trigger:
  - platform: state
    entity_id: switch.spiral
  condition: []
  action:
  - choose:
    - conditions:
      - condition: state
        entity_id: switch.spiral
        state: 'on'
        for:
          minutes: 1 
      sequence:
      - service: input_boolean.turn_on
        entity_id: input_boolean.kidsleepmode
      - service: switch.turn_on
        entity_id: switch.adaptive_lighting_sleep_mode_kid_adaptive
      - service: tts.google_say
        data_template:
          entity_id: media_player.dinning_room_speaker
          message: kid is now in Bed
      - service: homeassistant.turn_on
        entity_id: light.notificationlights
        data:
          brightness: 30
          rgb_color:
          - 0
          - 0
          - 255
      - delay: 00:00:15
      - service: homeassistant.turn_off
        entity_id: light.notificationlights
    - conditions:
      - condition: state
        entity_id: switch.spiral
        state: 'off'
      - condition: state
        entity_id: input_boolean.kidsleepmode
        state: 'on'        
      sequence:
      - service: input_boolean.turn_off
        entity_id: input_boolean.kidsleepmode
      - service: tts.google_say
        data_template:
          entity_id: media_player.dinning_room_speaker
          message: kid is Up
      - service: homeassistant.turn_on
        entity_id: light.notificationlights
        data:
          brightness: 255
          rgb_color:
          - 255
          - 0
          - 0
      - service: xiaomi_aqara.play_ringtone
        data:
          gw_mac: 7811DCFD69DF
          ringtone_id: 10001
          ringtone_vol: 100
      - delay: 00:00:58
      - service: homeassistant.turn_off
        entity_id: light.notificationlights
      - delay: 00:02:00
      - service: switch.turn_off
        entity_id: switch.adaptive_lighting_sleep_mode_kid_adapt
  mode: restart  

It triggers on the switch state change either to off or on. So the state just changed. The for condition is never going to be true. It could also trigger on an attribute of the switch changing but for your automation you can’t rely on that.

I thought it would check for the state to change to on and once its in that state for the specified time interval it would execute?

How could I accomplish it trigger on holding a state for the specified time?

Nope. It triggers, then starts executing the actions. The first one of which is the choose action, the condition in that chose action is evaluated and the flow is decided right then.

What you want is a wait action.

Consider this:

At the moment the door opens, check if the door has been open for a minute.

It can never be ‘open for a minute’ because the door just opened.

The for option in a condition doesn’t make it wait for that amount of time to pass, it makes it test if that amount has already passed.

Here’s what I suggest you try. It employs two State Triggers:

  1. Detects when the switch turns on and remains on for at least 1 minute.
  2. Detects when the switch turns off.
  alias: kidinbed
  trigger:
  - platform: state
    entity_id: switch.spiral
    from: 'off'
    to: 'on'
    for: '00:01:00'
  - platform: state
    entity_id: switch.spiral
    from: 'on'
    to: 'off'
  condition: []
  action:
  - choose:
    - conditions: '{{ trigger.to_state.state == 'on' }}'
      sequence:
      - service: input_boolean.turn_on
        entity_id: input_boolean.kidsleepmode
      - service: switch.turn_on
        entity_id: switch.adaptive_lighting_sleep_mode_kid_adaptive
      - service: tts.google_say
        data:
          entity_id: media_player.dinning_room_speaker
          message: kid is now in Bed
      - service: light.turn_on
        entity_id: light.notificationlights
        data:
          brightness: 30
          rgb_color:
          - 0
          - 0
          - 255
      - delay: 00:00:15
      - service: light.turn_off
        entity_id: light.notificationlights
    default:
    - condition: state
      entity_id: input_boolean.kidsleepmode
      state: 'on'        
    - service: input_boolean.turn_off
      entity_id: input_boolean.kidsleepmode
    - service: tts.google_say
      data:
        entity_id: media_player.dinning_room_speaker
        message: kid is Up
    - service: light.turn_on
      entity_id: light.notificationlights
      data:
        brightness: 255
        rgb_color:
        - 255
        - 0
        - 0
    - service: xiaomi_aqara.play_ringtone
      data:
        gw_mac: 7811DCFD69DF
        ringtone_id: 10001
        ringtone_vol: 100
    - delay: '00:00:58'
    - service: light.turn_off
      entity_id: light.notificationlights
    - delay: '00:02:00'
    - service: switch.turn_off
      entity_id: switch.adaptive_lighting_sleep_mode_kid_adapt
  mode: restart  
1 Like

Wow, nice. I liked how you used trigger.to_state.state == ‘on’ to determine if the switch is on other wise use the default the default condition as the switch would be off. Thank so much . This is super helpful as it helps me see so how to think about these solutions.

1 Like

Thank you. I think I can use this in an application to see if there is motion after I leave for work after a certain time has passed to test this concept out once I get the concept down.

In this particular case, one of the two State Triggers checks for on and the other for off so that makes it easy for the choose to determine which of the two was responsible for triggering the automation.

Now imagine a more complicated situation like this where two of the four triggers are checking for the on state and the other two check for the off state. How would choose easily differentiate them?

  trigger:
  - platform: state
    entity_id: switch.spiral
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: switch.spiral
    from: 'off'
    to: 'on'
    for: '00:01:00'
  - platform: state
    entity_id: switch.spiral
    from: 'on'
    to: 'off'
  - platform: state
    entity_id: switch.spiral
    from: 'on'
    to: 'off'
    for: '00:02:00

It’s possible to refer to the trigger’s platform like this:

- conditions: "{{ trigger.platform == 'state' }}"

However, it wouldn’t be useful here because all four triggers are State Triggers.

Even using the following wouldn’t be effective because it won’t determine which one of the two ‘to: on’ State Triggers triggered the automation:

- conditions: "{{ trigger.to_state.state == 'on' }}"

Fortunately, there was a recent enhancement to Home Assistant that permits you to label each trigger, with a name of your choosing, using the id option:

  trigger:
  - id: 'switch_on'
    platform: state
    entity_id: switch.spiral
    from: 'off'
    to: 'on'
  - id: 'switch_on_1_min'
    platform: state
    entity_id: switch.spiral
    from: 'off'
    to: 'on'
    for: '00:01:00'
  - id: 'switch_off'
    platform: state
    entity_id: switch.spiral
    from: 'on'
    to: 'off'
  - id: 'switch_off_2_min'
    platform: state
    entity_id: switch.spiral
    from: 'on'
    to: 'off'

Now it becomes easy to determine which one of the four State Triggers was triggered:

- conditions: "{{ trigger.id == 'switch_on_1_min' }}"

Instead of using a shorthand Template Condition, it can also be written using the new Trigger Condition:

- conditions:
  - condition: trigger
    id: switch_on_1_min

This is so super cool. I am immediately rewriting my 6 automations for my ac now into one. This is really nice and solves so many issues I could only solve by writing a different automation for each . Thanks for even going further to help expand my mind more.

Lastly out of curiosity, I have several automations where I use my notification lights. Is it possible to have the lights trigger maybe 2 minutes(3 min) before the 5 minute “for :00:05:00” and if I addressed the situation toggled the switch/light, closed the door, the trigger would no longer go off? In the automation that goes off it would be nice if my kid is asleep I could prevent the voice announcement if saw the light notify me .

I am asking as I am going to redo a lot of automations now and I figured I could integrate this.

I wanted to clarify when I specify multiple conditions and do not explicitly say its an, “and” condition does it just assume that’s the “and” condition? It seems to interpret it this way.

- conditions:
  - condition: trigger
    id: switch_on_1_min
  - condition: state
      entity_id: input_boolean.kidsleepmode
      state: 'on' 	

Yes. The documentation states:

Unlike a trigger, which is always or, conditions are and by default - all conditions have to be true.

Yes, just create a separate trigger with a shorter for duration (`for: ‘00:03:00’).

No, the State Trigger employing for: '00:05:00' can still trigger but all you need to do is add a condition (in the sequence that handles the trigger within choose) that prevents executing the balance of its `sequence if the door is already closed and the light is already in the desired state.

I wanted to say thanks a million. With what you shared I condensed my automations significantly and my though process has changed as well. I really appreciate you taking the type to help me .

Hi, Thanks for you opening me up to trigger ids.
I was trying to use a trigger ID but I am not having having luck.
The automation below shows up in the automation list when I comment out the id portion but it does not show when I uncomment the trigger id. I tried moving the Id to the top, at the end, using single quotes instead of double quotes and I feel I am missing something. Could you help me see where I went wrong . Thanks for helping better make use of the HA features.

- id: '16276162667617183740'
- alias: TurnOffDimLightsNoMotion
  trigger:
    - platform: state
   # id: "motion_on"  # I see the automation when I uncomment.
      entity_id: binary_sensor.motion_sensor_158d0001a8d4e9
      to: 'on'
 

  condition:
  - condition: state
    entity_id: media_player.55_tcl_roku_tv
    state: unavailable
  - condition: state
    entity_id: light.entertainment_area_3
    state: 'off'  
  action:
  - choose:
    - conditions:
       ########## MOTION ON ################
      - "{{ trigger.id == 'motion_on' }}" 
   #   - condition: trigger  
    #    id: "motion_on"     # I tried not using the short hand version to see if wat the #problem  
      sequence:
      - condition: state
        entity_id: light.ceilinglight
        state: 'off'
      
      - service: light.turn_on
        entity_id:  light.ceilinglight 

I Tried this as well still no luck. Which is what I see in documentation:

######################DIM Lights  Before Turning OFF ##################################
- id: '16276162667617183740'
- alias: TurnOffDimLightsNoMotion
  trigger:
    - platform: state
      id: "motionon"    
      entity_id: binary_sensor.motion_sensor_158d0001a8d4e9
      to: 'on'
  condition:
  - condition: state
    entity_id: media_player.55_tcl_roku_tv
    state: unavailable
  - condition: state
    entity_id: light.entertainment_area_3
    state: 'off'  
  action:
  - choose:
    - conditions:
       ########## MOTION ON ################
      #- "{{ trigger.id == 'motion_on' }}" 
      - condition: trigger
        id: "motionon"      
   #   - condition: trigger
    #    id: "motion_on"       
      sequence:
      - condition: state
        entity_id: light.ceilinglight
        state: 'off'
      
      - service: light.turn_on
        entity_id:  light.ceilinglight 

Not sure why you have it like this but I am surprised this even passes Check Configuration.

- id: '16276162667617183740'
- alias: TurnOffDimLightsNoMotion

That’s like two separate automations, the first with an id but nothing else, the second with no id but contains alias and other valid code for an automation.

Replace the hyphen on the second line with a space.

I did make the change but I am still not getting the automation to show up .

I am losing lot of hair. I wanted to see if this portion works before building out the automation further.

Thanks




######################DIM Lights  Before Turning OFF ##################################
- id: '1627616266761754183740'
  alias: TurnOffDimLightsNoMotionTwo
  trigger:
    - platform: state
      id: "motionon"    
      entity_id: binary_sensor.motion_sensor_158d0001a8d4e9
      to: 'on'
  condition:
  - condition: state
    entity_id: media_player.55_tcl_roku_tv
    state: unavailable
  - condition: state 
    entity_id: light.entertainment_area_3
    state: 'off'  
  action:
  - choose:
    - conditions:
       ########## MOTION ON ################
      #- "{{ trigger.id == 'motion_on' }}" 
      - condition: trigger
        id: "motionon"      
   #   - condition: trigger
    #    id: "motion_on"       
      sequence:
      - condition: state
        entity_id: light.ceilinglight
        state: 'off'
      
      - service: light.turn_on
        entity_id:  light.ceilinglight   

If after saving the automation you are executing Configuration > Server Controls > Check Configuration, it will report any errors it finds.

Even if you don’t do that and, after saving the automation, go directly to Configuration > Server Controls > Reload Automations, if it encounters errors it will refuse to reload automations and report the error (refer to Logs).

I cleared everything in my automations.yaml and left just this:

######################DIM Lights  Before Turning OFF ##################################
- id: 1627616266761754183740
  alias: TurnOffDimLightsNoMotionTwo
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d0001a8d4e9
      id: "motionon" 
      to: 'on'
  condition:
  - condition: state
    entity_id: media_player.55_tcl_roku_tv
    state: unavailable
  - condition: state 
    entity_id: light.entertainment_area_3
    state: 'off'  
  action:
  - choose:
    - conditions:
       ########## MOTION ON ################
      #- "{{ trigger.id == 'motionon' }}"  
    
      - condition: trigger
        id: "motionon"       
      sequence:
      - condition: state
        entity_id: light.ceilinglight
        state: 'off'
      
      - service: light.turn_on 
        entity_id:  light.ceilinglight    

This is what I see in the log:

Invalid config for [automation]: required key not provided @ data['action']. Got None required key not provided @ data['trigger']. Got None. (See /config/configuration.yaml, line 574).
Invalid config for [automation]: [id] is an invalid option for [automation]. Check: automation->id. (See /config/configuration.yaml, line 574).
Invalid config for [automation]: not a valid value for dictionary value @ data['action'][0]['choose'][0]['sequence'][0]['entity_id']. Got None. (See /config/configuration.yaml, line 574).
Invalid config for [automation]: expected str for dictionary value @ data['id']. Got None. (See /config/configuration.yaml, line 574).
Invalid config for [automation]: Unexpected value for condition: 'trigger'. Expected numeric_state, state, sun, template, time, zone, and, or, not, device @ data['action'][0]['choose'][0]['conditions'][0]. Got None expected str for dictionary value @ data['id']. Got None. (See /config/configuration.yaml, line 574).

It seems like it does not like something here:
- condition: trigger
id: “motionon”

but I don’t see what I am missing

What is the version of Home Assistant you are using?

Try this version:

- id: 1627616266761754183740
  alias: TurnOffDimLightsNoMotionTwo
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_158d0001a8d4e9
    to: 'on'
    id: 'motionon'
  condition:
  - condition: state
    entity_id: media_player.55_tcl_roku_tv
    state: 'unavailable'
  - condition: state 
    entity_id: light.entertainment_area_3
    state: 'off'  
  action:
  - choose:
    - conditions:
      - condition: trigger
        id: 'motionon'
      - condition: state
        entity_id: light.ceilinglight
        state: 'off'
      sequence:
      - service: light.turn_on 
        entity_id:  light.ceilinglight    

BTW, I assume you know this is a contrived example because it has only one trigger so there’s little value in using a trigger id.