Light Timer in Package

Hi All,
I just moved house and thought I’d get infrastructure done first thing AND I’d bite the bullet and move to Lovelace. I got all the devices and entities populated, solved my DuckDNS issues and thought I was well on myway. My computer then complained about lack of disc space and so (stupidly) I deleted some images and config backups. ZAP all my best examples of how to do stuff disappeared ! >:- <<<<<
So, I thought I’d bite another bullet and move over to packages, a nice way of picking an aspect of home automation and keep ALL relevant items in the one spot/file (input_boolean, input_number, binary_sensor, automations, scripts) well apart from the lovelace config (still monolythic on that).
Anyway, my first task was Home Occupancy and that went pretty smoothly, so next task : - Patio light timer.
Generally we just want to switch the light on so the dog can go for a pee and then let it switch itself off. I’m happy to disable it for other uses.
So I wanted a boolean to say if I want the off timer and a number to say how long and then stick it together. It all looks fine but it just doesn’t work. The Check Config says it’s clean … but …
Please have a look and see what I’ve done wrong this time. (I definately had this working pre-lovelace, but now I’m only working from memory ; - ((( )

input_boolean:
  ib_light_patio_link2kdoor:
    name: Link To Door
    initial: off
    icon: mdi:door
  ib_light_patio_timer_enable:
    name: Timer Enable
    initial: on
    icon: mdi:timer

input_number:
  in_light_patio_timer:
    name: On Timer (mins)
    initial: 2
    min: 1
    max: 300
    step: 1
    mode: box
    icon: mdi:alarm

automation:
    #name: Light Patio Off Delay - goto customise
  - alias: au_light_patio_offdelay
    trigger:
      - platform: state
        entity_id: light.fib_fgd212dim2_ptio_level
        to: 'on'
    condition:
      - condition: state
        entity_id: input_boolean.ib_light_patio_timer_enable
        state: 'on'
      - condition: state
        entity_id: input_boolean.ib_light_patio_link2kdoor
        state: 'off'
    action:
      - service: script.cancel
        entity_id: script.sc_light_patio_timer
      - service: script.sc_light_patio_timer

script:
  sc_light_patio_timer:
    alias: Light Patio Timer Script
    sequence:
    - delay: '00:{{state.input_number.in_light_patio_timer | int}}:00'
    - service: light.turn_off
      entity_id: light.fib_fgd212dim2_ptio_level

After this is working I’ll build on it (read: break it by fiddling with it - (if it works, it probably isn’t ‘feature rich’ enough yet) ) I’ll add the door switch to turn the light on and start the timer once the door closes, but only if the sun is below the horizon etc.

The UI bit to go with this looks like this : -

  - type: entities
    title: Patio Light
    show_header_toggle: false
    entities:
    - entity: light.fib_fgd212dim2_ptio_level
    - entity: input_boolean.ib_light_patio_link2kdoor
    - entity: input_boolean.ib_light_patio_timer_enable
    - entity: input_number.in_light_patio_timer

Thanks in advance, I know it’ll be something stuid

The main issue is the template in the delay. At the very least is should be:

    - delay: '00:{{states.input_number.in_light_patio_timer.state | int}}:00'

Better:

    - delay: '00:{{states("input_number.in_light_patio_timer") | int}}:00'

Even better:

    - delay:
        minutes: '{{states("input_number.in_light_patio_timer") | int }}'
1 Like

Automations will stop immediately if one of the services fails.

Your first action in the automation is script.cancel

There is no such service.

Automation stops before it starts.

Ah, good catch. Missed that one. Obviously should be script.turn_off.

1 Like

Phil, Thanks
Shows that my memory is failing faster than …
I went with your second suggestion, two reasons : -

  1. It’s the most compact and goes on 1 line.
  2. I can use this whereever I have a timer, whether it’s for hours, minutes or seconds (or combinations therof). Consistency in all things and that way I might remember it a bit better.

Packages Are Great !
Cheers