Allow entities in for: commands in triggers

It would be nice if we could use entities such as input_numbers in the for: section of a timed trigger.

ie:

input_number:
  kitchen_light_auto_off_time:
    name: Kitchen light auto off time
    min: 1
    max: 9
    step: 1
    icon: mdi:camera-timer
    unit_of_measurement: min

automation:
  - alias: 'Kitchen light OFF after x mins'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.kitchen_multi_sensor_sensor
        to: 'off'
        for: 
          minutes: input_number.kitchen_light_auto_off_time
    action:
      - service: homeassistant.turn_off
        entity_id: light.kitchen_light

The above is not allowed. Instead we have to call a script and have the timer in there which means more code. I just think it would be nice and clean to be able to run dynamic timers in the triggers directly.

Thoughts?

I was just thinking this today. I want an adjustable delay for turning my lights off dynamically. The way it is now I have to trigger every minute and use a condition template. Trigger templates would be much more cpu resource friendly.

You can do it without a template, but I still feel it’s not as neat as it should be. I’ll post my work-around as soon as I can for you. Templates in the triggers would be so much better though

This is how I currently do it:

input_number:
  kitchen_light_auto_off_time:
    name: Kitchen light auto off time
    min: 1
    max: 9
    step: 1
    icon: mdi:camera-timer
    unit_of_measurement: min

automation:
  - alias: 'Kitchen light auto off'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.kitchen_multi_sensor_sensor
    action:
      - service: script.turn_on
        entity_id: script.kitchen_light_auto_off_timer

script:
  kitchen_light_auto_off_timer:
    alias: Kitchen light off after preset time
    sequence:
    - delay: '00:0{{ states.input_number.kitchen_light_auto_off_time.state | int }}:00'
    - service: homeassistant.turn_off
      data:
        entity_id: light.kitchen_light_level

There is a lot more to the kitchen light control, but that is the part that achieves the adjustable timer. For example, I stop the auto_off script if motion is re-triggered.

Hmm. What happens if the script is called, then called again while the first instance is waiting in the delay?

In order for that to happen the motion detector would have had to change to ‘motion detected’, then back to ‘no motion’. When motion is detected I stop the delay timer script, so once the change of state back to ‘no motion’ occurs, the script isn’t currently running. I’ll post the whole package to explain it.

This is the full package so you can see how the script gets cancelled etc.

kitchen_light.yaml

#  All code relating to the functioning of the kitchen light

input_number:
  kitchen_light_auto_off_time:
    name: Kitchen light auto off time
    min: 1
    max: 9
    step: 1
    icon: mdi:camera-timer
    unit_of_measurement: min


automation:
  - alias: 'Kitchen Remote Button 1'
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: single
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: double
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: long_click_press

    action:
      - service_template: >
          {% if trigger.event.data.click_type == 'single' %}
            script.kitchen_button_single_click
          {% elif trigger.event.data.click_type == 'double' %}
            script.kitchen_button_double_click
          {% elif trigger.event.data.click_type == 'long_click_press' %}
            script.kitchen_button_long_press
          {% endif %}
          


  - alias: 'Kitchen light auto ON motion'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.kitchen_multi_sensor_sensor
        to: 'on'
    condition:      
      condition: or
      conditions:
      - condition: sun
        after: sunset
        after_offset: '-00:10:00'
      - condition: sun
        before: sunrise
        before_offset: '-01:00:00'
      - condition: template
        value_template: '{{ states.sensor.kitchen_multi_sensor_luminance.state | int < 10 }}'
    action:
      - service: script.turn_off
        entity_id: script.kitchen_light_auto_off_timer
      - service: light.turn_on
        data:
          entity_id: light.kitchen_light_level



  - alias: 'Kitchen light auto off'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.kitchen_multi_sensor_sensor
    action:
      - service: script.turn_on
        entity_id: script.kitchen_light_auto_off_timer


script:
  kitchen_button_single_click:
    alias: Kitchen button single click
    sequence:
    - service: switch.turn_off
      data:
        entity_id: switch.circadian_lighting_kitchen_living_circadian_lighting
    - service: light.turn_on
      data:
        entity_id: light.kitchen_light_level
        brightness: 255
    - service: automation.turn_off
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off
  
  kitchen_button_double_click:
    alias: Kitchen button double click
    sequence:
    - service: light.turn_off
      data: 
        entity_id: light.kitchen_light_level
    - service: automation.turn_off
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off
  
  kitchen_button_long_press:
    alias: Kitchen button long press
    sequence:
    - service: switch.turn_on
      data:
        entity_id: switch.circadian_lighting_kitchen_living_circadian_lighting
    - service: automation.turn_on
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off
  
  kitchen_light_auto_off_timer:
    alias: Kitchen light off after preset time
    sequence:
    - delay: '00:0{{ states.input_number.kitchen_light_auto_off_time.state | int }}:00'
    - service: homeassistant.turn_off
      data:
        entity_id: light.kitchen_light_level
1 Like