How to turn off a light depending on the actual time?

Hi,

I need a little help with scripting and template.

The goal is to turn off a light after x minutes depending on the time of day. Between 00h and 06h I’d like the delay to be 3 minutes and 30 minutes the rest of the time.

I have a script that turn off the light after a fix time, that work fine.
It’s the dynamic delay that I cannot get working.

Here is the code I have now:

script:
  timer_off:
      alias: "Turn off lamp after 20 minutes"
      sequence:
        - delay:
            minutes: '{% if now().hour < 6 % }3{% else %}30{% endif%}'
        - service: light.turn_off
          data:
            entity_id: light.white_strip
            transition: 50

The script is marked as invalid by hass, I just cannot figure why… does someone have an idea?

Shouldn’t there be a space before and after each { } ?

{ % if now().hour < 6 % }3{ % else % }30{ % endif% }

Thanks for this hint.

I just tried with the space, but got this error:

Invalid config for [script]: expected int for dictionary value @ data['script']['timer_off']['sequence'][0]['delay']['minutes']. Got '{ % if now().hour < 6 % }3{ % else % }30{ % endif% }'. (See ?, line ?). Please check the docs at https://home-assistant.io/components/script/

timer_off: [source /config/packages/white_led_strip.yaml:66]
  alias: Turn off lamp after 20 minutes
  sequence: [source /config/packages/white_led_strip.yaml:68]
    - delay: [source /config/packages/white_led_strip.yaml:69]
        minutes: { % if now().hour < 6 % }3{ % else % }30{ % endif% }

I also tried without the ' but got:

17-02-27 15:34:43 ERROR (Thread-1) [homeassistant.util.yaml] while scanning for the next token
found character '%' that cannot start any token
  in "/config/packages/white_led_strip.yaml", line 70, column 22

I was wrong about the location of the spaces. Try this.

{% if now().hour < 6 %} 3 {% else %} 30 {% endif %}

Still got the same:

17-02-27 15:44:29 ERROR (MainThread) [homeassistant.bootstrap] Invalid config for [script]: expected int for dictionary value @ data['script']['timer_off']['sequence'][0]['delay']['minutes']. Got '{% if now().hour < 6 %}3{% else %}30{% endif %}'. (See ?, line ?). Please check the docs at https://home-assistant.io/components/script/

Am I missing a data_template tag?

Edit: I also tried with '{{% if now().hour < 6 %}3{% else %}30{% endif %}}' to respect the syntax foung in https://home-assistant.io/docs/scripts/#delay but got the same again.

17-02-27 15:48:28 ERROR (MainThread) [homeassistant.bootstrap] Invalid config for [script]: expected int for dictionary value @ data['script']['timer_off']['sequence'][0]['delay']['minutes']. Got '{{% if now().hour < 6 %}3{% else %}30{% endif %}}'. (See ?, line ?). Please check the docs at https://home-assistant.io/components/script/

Check my example here.
Instead of using only minutes, you should use the whole time form

1 Like

Try spaces before and after 3 and before and after 30.

{% if now().hour < 6 %} 3 {% else %} 30 {% endif %}

When i plug that ^ in to template dev i get back 30

1 Like

I tried:

timer_off:
  alias: "Turn off lamp after 20 minutes"
  sequence:
    - delay: '00:{{% if now().hour < 6 %}3{% else %}30{% endif %}}:00'

and got:

17-02-27 15:52:42 ERROR (MainThread) [homeassistant.bootstrap] Invalid config for [script]: [delay] is an invalid option for [script]. Check: script->script->timer_off->sequence->0->delay. (See ?, line ?). Please check the docs at https://home-assistant.io/components/script/

I’ll try with the new spacing

EDIT: with the new spacing:
- delay: '00:{{% if now().hour < 6 %} 3 {% else %} 30 {% endif %}}:00'

17-02-27 15:55:10 ERROR (MainThread) [homeassistant.bootstrap] Invalid config for [script]: [delay] is an invalid option for [script]. Check: script->script->timer_off->sequence->0->delay. (See ?, line ?). Please check the docs at https://home-assistant.io/components/script/

Try

'00:{% if now().hour < 6 %} 3 {% else %} 30 {% endif %}:00'

If you go to your_ip:8123/dev-template you can validate and debug your template

1 Like

Awesome, so too much {} was the problem :slight_smile: (and likely the wrong spacing too)

script:
  timer_off:
      alias: "Turn off lamp after x minutes"
      sequence:
        - delay: '00:{% if now().hour < 6 %} 3 {% else %} 30 {% endif %}:00'
        - service: light.turn_off
          data:
            entity_id: light.white_strip
            transition: 50