Script hanging when statement not true

Hi

I have issues with one of my scripts. When triggered it never finishes when statement is not true. When statement is true, the script is working fine. What are I missing here?

Script:

  sequence:
  - delay:
     minutes: 20
  - entity_id: switch.plug_158d0002XXXXXX
    service_template: "{% if as_timestamp(now()) - as_timestamp(states.sensor.power_load_kitchen_sensor.last_updated)\
      \ | int < 600 %}\n  switch.turn_off\n{% endif %}\n" 

You don’t have an else case for the service template to do in the case that the if statement evaluates as false.

Just use a template condition to to halt execution of the sequence instead.

Thanks that did he trick, I thought that the end if was good enough.

where are people getting the idea of putting all of the newline characters in their templates coming from?

In the past I’ve never seen anyone doing this. But in the last couple of months it’s been happening more and more.

you don’t need those. Instead, you can do it in two ways and the second one is a bit easier to read especially if you put in the additional lines of code as suggested by Tom:

service_template: "{% if as_timestamp(now()) - as_timestamp(states.sensor.power_load_kitchen_sensor.last_updated) | int < 600 %} switch.turn_off {% endif %}" 

or

service_template: >
  {% if as_timestamp(now()) - as_timestamp(states.sensor.power_load_kitchen_sensor.last_updated) | int < 600 %}
    switch.turn_off
  {% endif %} 
1 Like