Templating in state condition, FOR-statement?

Hi, using a state condition in a script, including the number of seconds the entity should be in a particular state (FOR-statement).
However, I would like to use a template in this FOR-statement. See the code below. Including a template like this generates an error however. Are templates indeed not supported in for-statements? Is there another way to go about this?

- condition: state
  entity_id: input_select.cv_modus
  state: 'weersafhankelijk'
  for: "{{ [( 2 * (15 - states('sensor.outside_temperature_stats')|float) )|multiply(60), 0]|max |int }}"

Thanks!

I think you have to put it under a unit:

condition: state
entity_id: input_select.cv_modus
state: "weersafhankelijk"
for: 
  minutes: "{{ [( 2 * (15 - states('sensor.outside_temperature_stats')|float) )|multiply(60), 0]|max |int }}"

It should probably be

seconds: "{{ [( 2 * (15 - states('sensor.outside_temperature_stats')|float) )|multiply(60), 0]|max |int }}"

Because when you cut and paste into developer tools you get 120 for each temperature degree difference.

Thanks @Didgeridrew @AllHailJ for your suggestion, unfortunately this does not help. When checking the conf, HA still complains that scripts could not be set up.

Break your condition into 2 pieces…

condition:
  - condition: state
    entity_id: input_select.cv_modus
    state: "weersafhankelijk"
  - condition: template
    value_template: >
      {% set x = [( 2 * (60 - states('sensor.outside_temperature_stats')
       |float) )|multiply(60), 0]|max | float %}
      {{ now() - timedelta(seconds=x) >= states.input_select.cv_modus.last_changed }}

1 Like

Thanks Drew, great solution!