Changing delay in automation depending of temperature

Hello everyone
There is automation:

- alias: Test
   trigger:
    -  platform: numeric_state
       entity_id: sensor.average_temp
       above: 19.80
   action:
     - service: switch.turn_on
       entity_id: switch.sonoff_1000df7ffb_3
     - delay: '00:00:05'
     - service: switch.turn_off
       entity_id: switch.sonoff_1000df7ffb_3

How can I change delay when the temperature changes? For example:
when temp 19-20 set delay to '00:00:04’
when temp 18-19 set delay to '00:00:03’

How can the value of sensor.average_temp ever be less than 19.8? The automation’s Numeric State Trigger is configured to trigger only when the sensor’s value is greater than 19.8.

I agree, this is as an example. I have a question about delay, is it possible to change it automatically depending on the temperature?

Yes.

     - delay:
         seconds: "{{ iif(trigger.to_state.state|float(0) < 19, 3, 4) }}"
1 Like

Thank you for the answer. So, in my case it will look as:

- delay:
         seconds: "{{ if(trigger.sensor.average_temp.state|float(0) < 19, 3, 4) }}"

is everything right?

If I want to add range such as: when temp 21-22 set delay to '00:00:05’, How will it look?
when temp 19-20 set delay to '00:00:04’
when temp 18-19 set delay to '00:00:03’

No, that’s an invalid use of the trigger variable and an improper use of the if statement.

  • The example I posted uses the trigger variable’s to_state property which exists when the automation is triggered by a Numeric State Trigger. Reference: Templating - Numeric State Trigger. The trigger variable doesn’t have a sensor property.

  • The Immediate If statement is written as iff and not as if whose usage and syntax is quite different.

Use the example I posted above.