Simple automation timer

I am new to home assistant, still trying to figure it all out. I want to create an automation that will turn off after ten minutes.
Can anybody help or advise how to add it to this?
Thank you

alias: Landing Motion
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 41f95856991320032c484d37b9f427a2
    entity_id: binary_sensor.landing_motion_occupancy
    domain: binary_sensor
condition:
  - condition: time
    after: "17:00:00"
    before: "08:30:00"
    weekday:
      - sun
      - sat
      - fri
      - thu
      - wed
      - tue
      - mon
action:
  - type: turn_on
    device_id: 774d9a0815f8b2abbadcf741d712d6fb
    entity_id: light.stairs
    domain: light
    flash: short
    brightness_pct: 50
mode: single

You might want to use the Wait For Trigger action

wait_for_trigger:
  - platform: state
    entity_id: binary_sensor.landing_motion_occupancy
    to: 'off'

You can add a for: to that to wait for ten minutes

1 Like

I am so bad at this lol
so I tried changing it a few ways and can’t get it right.
I was thinking something like this, because in my mind it would step through it. but home assistant won’t accept it

alias: Landing Motion
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 41f95856991320032c484d37b9f427a2
    entity_id: binary_sensor.landing_motion_occupancy
    domain: binary_sensor
condition:
  - condition: state
    entity_id: sensor.landing_motion_illuminance_lux
    attribute: unit_of_measurement
action:
  - type: turn_on
    device_id: 774d9a0815f8b2abbadcf741d712d6fb
    entity_id: light.stairs
    domain: light
    flash: short
    brightness_pct: 50
wait_for_trigger:
  - platform: state
    entity_id: binary_sensor.landing_motion_occupancy
    to: 'off'
action:
  - type: turn_off
    device_id: 774d9a0815f8b2abbadcf741d712d6fb
    entity_id: light.stairs
    domain: light
    flash: short
    brightness_pct: 50
mode: single

This is probably more complicated than you’d like. But I am pasting it because I think it helps to look at other’s code sometimes to help your own. I don’t like to use ‘waits’ in the code for longer periods of time because they don’t persist a home assistant restart nor a ‘automation reload’ (you saving ANY automation) and will cancel those waits… So be careful using them.

Here, I am using a timer to count down the shuttting off the light. So after the last motion of ‘x’ minutes, it will shut off my office light. If there’s more motion within that time, it will restart the timer.

alias: Front Office Motion Light
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.front_office_motion
    id: motionon
    to: "on"
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.front_office_timer
    id: vacancy
condition: []
action:
  - variables:
      light: light.front_office_light
      timer: timer.front_office_timer
      night: >-
        {% if now() >
        now().replace(hour=21).replace(minute=0).replace(second=0).replace(microsecond=0) 
           or now() < now().replace(hour=7).replace(minute=0).replace(second=0).replace(microsecond=0) %}
          true
        {% else %}
          false
        {% endif %}
      duration: "{% if night == 'true' %}240{% else %}1800{% endif %}"
      brightness: "{% if night == 'true' %}20{% else %}100{% endif %}"
  - choose:
      - conditions:
          - condition: trigger
            id: motionon
        sequence:
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ is_state(timer,'idle') }}"
                  - condition: template
                    value_template: "{{ is_state(light,'off') }}"
                sequence:
                  - service: light.turn_on
                    data:
                      brightness_pct: "{{brightness}}"
                      entity_id: "{{light}}"
            default: []
          - service: timer.start
            data:
              duration: "{{ duration | int }}"
            target:
              entity_id: "{{timer}}"
      - conditions:
          - condition: trigger
            id: vacancy
        sequence:
          - service: light.turn_off
            data:
              entity_id: "{{light}}"
    default: []
mode: single

Also, the reason the editor wouldn’t ‘take’ yours is it isn’t valid yaml. This would be valid…

alias: Landing Motion
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 41f95856991320032c484d37b9f427a2
    entity_id: binary_sensor.landing_motion_occupancy
    domain: binary_sensor
condition:
  - condition: state
    entity_id: sensor.landing_motion_illuminance_lux
    attribute: unit_of_measurement
action:
  - type: turn_on
    device_id: 774d9a0815f8b2abbadcf741d712d6fb
    entity_id: light.stairs
    domain: light
    flash: short
    brightness_pct: 50
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.landing_motion_occupancy
        to: "off"
  - type: turn_off
    device_id: 774d9a0815f8b2abbadcf741d712d6fb
    entity_id: light.stairs
    domain: light
    flash: short
    brightness_pct: 50
mode: single

Although you probably should have a timeout for hte wait_for_trigger…

1 Like

Thank you so much for that. It really has been most helpful. learning as I go.

It’s possible to simplify a few templates:

      night: "{{ now().hour > 21 or now().hour < 7 }}" 
      duration: "{{ iif(night, 240, 1800) }}"
      brightness: "{{ iif(night, 20, 100) }}"
2 Likes

This would go in the conditions section?

He was just simplifying the variables. To look like this:

alias: Front Office Motion Light
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.front_office_motion
    id: motionon
    to: "on"
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.front_office_timer
    id: vacancy
condition: []
action:
  - variables:
      light: light.front_office_light
      timer: timer.front_office_timer
      night: "{{ now().hour > 21 or now().hour < 7 }}"
      duration: "{{ iif(night, 240, 1800) }}"
      brightness: "{{ iif(night, 20, 100) }}"
  - choose:
      - conditions:
          - condition: trigger
            id: motionon
        sequence:
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ is_state(timer,'idle') }}"
                  - condition: template
                    value_template: "{{ is_state(light,'off') }}"
                sequence:
                  - service: light.turn_on
                    data:
                      brightness_pct: "{{brightness}}"
                      entity_id: "{{light}}"
            default: []
          - service: timer.start
            data:
              duration: "{{ duration | int }}"
            target:
              entity_id: "{{timer}}"
      - conditions:
          - condition: trigger
            id: vacancy
        sequence:
          - service: light.turn_off
            data:
              entity_id: "{{light}}"
    default: []
mode: single
1 Like