Add timer to turn off automation

How do i add a timer to this automation to turn off light after 5:00 minutes

- id: '1734129575705'
  alias: Shop light on
  description: Shelly turns on after garage door opens
  triggers:
  - device_id: 7047a9fdbb2a553384e81e63c1093c79
    domain: cover
    entity_id: a638868a5a8d3fb9335cec083a2f1305
    type: opened
    trigger: device
  conditions:
  - condition: time
    after: '17:00:00'
    before: '23:00:00'
    weekday:
    - sun
    - mon
    - tue
    - wed
    - thu
    - fri
    - sat
  actions:
  - type: turn_on
    device_id: 28fa8085cdaa34130061cbf5923ee43e
    entity_id: 53dd1e814e83c96328465311ee38b8a6
    domain: switch
  mode: single

Add delay and turn off at the end?

  - delay:
      minutes: 5
  - type: turn_off
    device_id: 28fa8085cdaa34130061cbf5923ee43e
    entity_id: 53dd1e814e83c96328465311ee38b8a6
    domain: switch

It’s not usually a good idea to have automations pausing for long periods, but five minutes is probably OK.

I prefer to keep automations very simple and have each one do only one thing. Then different automations all come into play, which makes maintenance mnuch simpler and the whole thing is much more flexible.

For example -

One automation just turns the light on with the conditions you specified, trigered by the action you specified.

One automation starts a timer when the light is switched on, but the timer is started with a duration specified by an input_number which you can show as a slider to change in your dashboard.

One automation simply turns the light off when the timer finishes.

Then you can add alot more functionality and logic to it in a much simpler manner going forward.

Thoughts?

Also you can have an automation that justg turns the light on when the timer is started, and then your original automation could just start the timer if you like. Or also add an automation that just stops the timer when the garage door closes (which would turn off the light), etc., etc.

It makes it much more flexible and easier to maintain keeping all of the automations simple - and opens alot more possibilities!

1 Like

This is how I use a Timer Helper to accomplish what you’re after:

Triggers

  • On trigger
  • ---- occupancy detected
  • Off trigger
  • ---- occupancy cleared
  • Timer Trigger
  • ---- timer completed

These would be options the triggers would. . . er, trigger:

  • ON Trigger
  • ---- turn on device
  • ---- cancel Timer
  • Off Trigger
  • ---- turn on timer
  • Timer Trigger
  • ---- turn off device
alias: presence.Den
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.presence_den_occupancy
    to: "on"
    id: occupancy-detected
  - trigger: state
    entity_id:
      - binary_sensor.presence_den_occupancy
    to: "off"
    id: occupancy-cleared
    from: "on"
  - trigger: event
    event_type: timer.finished
    event_data:
      entity_id: timer.timer_den
    id: timer-finished
conditions:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - occupancy-detected
        sequence:
          - metadata: {}
            data:
              brightness_pct: 50
              transition: 4
            target:
              entity_id:
                - light.desk_group_lights
            action: light.turn_on
          - metadata: {}
            data: {}
            target:
              entity_id:
                - timer.timer_den
            action: timer.cancel
      - conditions:
          - condition: trigger
            id:
              - occupancy-cleared
        sequence:
          - target:
              entity_id:
                - timer.timer_den
            data:
              duration: "00:05:00"
            action: timer.start
      - conditions:
          - condition: trigger
            id:
              - timer-finished
        sequence:
          - metadata: {}
            data: {}
            target:
              entity_id:
                - light.desk_group_lights
            action: light.turn_off
mode: single

Learning how to utilize a Timer HELPER was key.

When I create the Helper timer do I just create a “Timer” for 5:00 minutes? Then create an automation that see the light come on and then the “Timer” starts with a trigger to Turn_off light when timer completes?

1 Like

Sorry I will let Bob answer, but I believe so… When you start the timer it would then run for 5 minutes. But, when you start the timer you can specify how long it runs, so you could pass to it the value from an input_number which you can then just display on a dashboard as a slider. (I then have another automation that restarts the timer with the new duration when the slider is changed - but only if the timer is already active.)

Derek,

If I follow - You can set any duration when creating a timer. When utilizing that timer in an automation the duration can be changed IF one prefers.