How to set configuration variable through "if" condition?

I am trying to set a derivative configuration variable through an if condition like this:
(I wanna apply a time_window for the first 60 seconds of a sensor s2 turning on, and after that, no time_window)

  - platform: derivative
    name: "timeWindowConditional"
    source: sensor.s1
    unit_time: min
    round: 2
    time_window: >
      {% if is_state(binary_sensor.s2,  'on') and (as_timestamp(now()) - as_timestamp(states.binary_sensor.s2.last_changed) | float <= 60) %}
      "00:01:00"
      {% else %}
      "00:00:00"
      {% endif %}

And I get an error when I do “check configuration”:

Invalid config for [sensor.derivative]: offset {% if is_state(binary_sensor.s2, 'on') and (as_timestamp(now()) - as_timestamp(states.binary_sensor.s2.last_changed) | float <= 60) %} "00:01:00" {% else %} "00:00:00" {% endif %}  should be format 'HH:MM', 'HH:MM:SS' or 'HH:MM:SS.F' for dictionary value @ data['time_window']. Got '{% if is_state(binary_sensor.s2, \'on\') and (as_timestamp(now()) - as_timestamp(states.binary_sensor.s2.last_changed) | float <= 60) %} "00:01:00" {% else %} "00:00:00" {% endif %} \n'. (See ?, line ?). 

It seems that it takes the syntax very literally and not applying the logic…

I checked the condition syntax and it seems to work; also somewhere else in a template sensor, set the “state” using a similar syntax and it worked… it only doesn’t work with the derivative…

Any ideas how I can fix this? Or this is not doable at all?

Thanks!

Only a limited number of configuration keys actually accept templating. From the docs it it doesn’t look like that’s one of the ones that does.

1 Like

Thanks!
It seems you’re right, and in fact when I think about it, it doesn’t make much sense. Derivative needs a fixed time window and not one that changes on fly.