Need help with automation trigger based on a time template

I hope some one can help me with this automation to do the watering. I am having trouble implementing the turn-off part of the automation. I want the length of watering to be shorter when the temperature is lower, so trying to do that using a template. I am getting an error - hope someone can assist. Thanks in advance!

The automation is:

  • id: water_system_on
    alias: Turn on watering system
    initial_state: true
    trigger:
    platform: sun
    event: sunset
    offset: “-01:00:00”
    condition:
    condition: numeric_state
    entity_id: sensor.yr_precipitation
    below: 2
    action:

    • service: switch.turn_on
      entity_id: switch.switch_d
    • service: notify.pushover
      data:
      message: “The watering system switched on”
      title: “Home Assistant”
  • id: water_system_off
    alias: Turn off watering system depending on temperature
    initial_state: true
    trigger:
    platform: state
    entity_id: switch.switch_d
    to: ‘on’
    for: >
    {% if ‘sensor.yr_temperature’ > 25 %}
    minutes: 15
    {% elif ‘sensor.yr_temperature’ > 15 %}
    minutes: 10
    {% elif ‘sensor.yr_temperature’ > 8 %}
    minutes: 5
    {% else %}
    minutes: 2
    {% endif %}
    action:

    • service: switch.turn_off
      entity_id: switch.switch_d
    • service: notify.pushover
      data:
      message: “The watering system switched off”
      title: “Home Assistant”

The error I get is

Invalid config for [automation]: offset {% if ‘sensor.yr_temperature’ > 25 %}
minutes: 15
{% elif ‘sensor.yr_temperature’ > 15 %}
minutes: 10
{% elif ‘sensor.yr_temperature’ > 8 %}
minutes: 5
{% else %}
minutes: 2
{% endif %}
should be format ‘HH:MM’ or ‘HH:MM:SS’ for dictionary value @ data[‘trigger’][0][‘for’]. Got None. (See /config/configuration.yaml, line 71). Please check the docs at https://home-assistant.io/components/automation/

oops - forgot to copy and paste with proper formatting

- id: water_system_on
  alias: Turn on watering system
  initial_state: true
  trigger:
    platform: sun
    event: sunset
    offset: "-01:00:00"
  condition:
    condition: numeric_state
    entity_id: sensor.yr_precipitation
    below: 2
  action:
    - service: switch.turn_on
      entity_id: switch.switch_d
    - service: notify.pushover
      data: 
        message: "The watering system switched on"
        title: "Home Assistant"    
  
    
    
- id: water_system_off
  alias: Turn off watering system depending on temperature
  initial_state: true
  trigger:
    platform: state
    entity_id: switch.switch_d
    to: 'on'
    for: >
     {% if 'sensor.yr_temperature' > 25 %}
       minutes: 15
     {% elif 'sensor.yr_temperature' > 15 %}
       minutes: 10
     {% elif 'sensor.yr_temperature' > 8 %}
       minutes: 5
     {% else %}
       minutes: 2
     {% endif %}   
  action:
    - service: switch.turn_off
      entity_id: switch.switch_d
    - service: notify.pushover
      data: 
        message: "The watering system switched off"
        title: "Home Assistant"

You cannot use templating in a trigger outside the value_template trigger. That is your problem.

so for clarification, this section is not possible:

OK , thanks @petro for your help!
I will redo using scripts or a value-template trigger somehow.
Appreciate your quick response !

I’m trying to think of a way to do this with a value template and I can’t. You may need to utiliize the last_updated or last_changed (i forget the property) for the sensor.yr_temperature. Maybe even the history statistics component may get you the information you are looking for.

In case this helps anyone else, have worked out a solution using Timers (have defined a timer called ‘timer0’ for the automation below. Will test it tomorrow and let you know if it works OK.

- id: water_system_on
  alias: Turn on watering system
  initial_state: true
  trigger:
    platform: sun
    event: sunset
    offset: "-01:00:00"
  condition:
    condition: numeric_state
    entity_id: sensor.yr_precipitation
    below: 2
  action:
  - service: switch.turn_on
    entity_id: switch.switch_d
  - service: notify.pushover
    data: 
      message: "The watering system switched on"
      title: "Home Assistant"    
  - service: timer.start
    data:
      entity_id: timer.timer0
      duration: '00:02:00'
  - condition: numeric_state
    entity_id: sensor.yr_temperature
    above: 8
  - service: timer.start
    data:
      entity_id: timer.timer0
      duration: '00:05:00'      
  - condition: numeric_state
    entity_id: sensor.yr_temperature
    above: 15
  - service: timer.start
    data:
      entity_id: timer.timer0
      duration: '00:10:00'
  - condition: numeric_state
    entity_id: sensor.yr_temperature
    above: 25
  - service: timer.start
    data:
      entity_id: timer.timer0
      duration: '00:15:00'     
    
    
- id: water_system_off
  alias: Turn off watering system depending on timer
  initial_state: true
  trigger: 
    - platform: event
      event_type: timer.finished
      event_data: 
        entity_id: timer.timer0
  action:
    - service: switch.turn_off
      entity_id: switch.switch_d
    - service: notify.pushover
      data: 
        message: "The watering system switched off"
        title: "Home Assistant"

If you tried to use a data_template with the timer (if this is possible?), you might be able to use a multiplication against the temperature.

E.g. duration:
{{ states.sensor.yr_temperature.states * 0.85 | int }} minutes

Nice implementation, didn’t know about timers in HA. Good to know!

You could probably simplify your timer section with a data template:

  action:
  - service: switch.turn_on
    entity_id: switch.switch_d
  - service: notify.pushover
    data: 
      message: "The watering system switched on"
      title: "Home Assistant"
  - service: timer.start
    data_template:
      duration: >
        {% set temp = states('sensor.yr_temperature') %}
        {% if temp > 25 %}
          "00:15:00"
        {% elif  temp > 15%}
          "00:10:00"
        {% elif  temp > 8%}
          "00:05:00"
        {% else %}
          "00:02:00"
        {% endif %}

Nice idea, much cleaner. Will try it out. Thanks!

Interesting idea. Thanks!