Problem setting a variable with an IF statement

Hi all!

I am really new with this system and not so familiar with the coding. I have been trying for a few days but could not make it work.

I am trying to set an automation to irrigate my hydroponics according to input helpers where I set the following:

  • start of daylight period
  • Irrigation frequency for daylight period
  • start of night period
  • Irrigation frequency for night period
  • Time of irrigation (same for both periods)

So, depending of the time of the day it should irrigate at a different frequency. Here is the code I am using:

 - id: '1601263156762'
   alias: Sistema riego por tiempo
   description: ''
   variables:
     frecuencia_riego: >
      {% if input_number.inicio_intervalo_diurno < now().hour < input_number.inicio_intervalo_nocturno %}
       "{{states('input_number.frecuencia_de_riego_diurno') | int}}"
      {% else %} 
       "{{states('input_number.frecuencia_de_riego_nocturno') | int}}"
      {% endif %}
   trigger:
   - platform: time_pattern
     hours: '*'
     minutes: "/{{frecuencia_riego}}"
     seconds: '*'
   action:
   - service: switch.turn_on
     data: {}
     entity_id: switch.sonoff_100090196f
   - delay: "00:{{input_number.tiempo_de_riego}}:00"
   - service: switch.turn_off
     data: {}
     entity_id: switch.sonoff_100090196f
   mode: single

The error I get is:
Invalid config for [automation]: invalid time_pattern value for dictionary value @ data[‘minutes’]. Got None. (See /config/configuration.yaml, line 10).

Trying to debug something, I found that if I change " minutes: “/{{frecuencia_riego}}” to " minutes: ‘/10’ " the error dissapears but does not work either.

Any help somebody could give me will be higle appreciated. Thanks!

What is frecuencia_riego and where is it set?

This key is not currently templateable.

Hi!
frecuencia_riego should be the frecuency of irrigation. Depending of the time of the day it should be set according to two input_numbers. This is:

The day is dived in to intervals set by two input numbers ´( input_number.inicio_intervalo_diurno and input_number.inicio_intervalo_nocturno)´

|-------------interval 1--------------------|-----------interval 2--------|

Each interval has its own frecuency setted by two different input numbers ´(input_number.frecuencia_de_riego_nocturno and input_number.frecuencia_de_riego_nocturno)´

Thanks for your answer!

Do you know if there is any way to set a variable time_pattern trigger?

I think you could do it like this:

trigger:
  platform: state
  entity_id: input_boolean.whatever
  to: 'on'
  for:
    minutes: "{{frecuencia_riego}}"
action:
- service: input_boolean.turn_off
  data:
    entity_id: input_boolean.whatever
- service: input_boolean.turn_on
  data:
    entity_id: input_boolean.whatever
- your actions here

Thank you Tom, you gave an idea using states as triggers. I made two differents automations, one for each interval, and changed input_number for input_datetime. Maybe it is not the most efficient way of doing it but is working.

Interval between start and end time:

- id: '1601594803344'
  alias: Riego diurno
  description: ''
  trigger:
  - platform: state
    entity_id: switch.sonoff_100090196f
    for: 00:{{ states('input_number.frecuencia_de_riego_diurno') | int}}:00
    to: 'off'
  condition:
  - condition: time
    after: input_datetime.inicio_intervalo_diurno
    before: input_datetime.inicio_intervalo_nocturno
  action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.sonoff_100090196f
  - delay: 00:{{ states('input_number.tiempo_de_riego') | int }}:00
  - service: switch.turn_off
    data: {}
    entity_id: switch.sonoff_100090196f
  mode: single

Interval for the rest of the day:

- id: '1601596411913'
  alias: Riego nocturno
  description: ''
  trigger:
  - platform: state
    entity_id: switch.sonoff_100090196f
    for: 00:{{ states('input_number.frecuencia_de_riego_nocturno') | int}}:00
    to: 'off'
  condition:
  - condition: time
    after: input_datetime.inicio_intervalo_nocturno
    before: input_datetime.inicio_intervalo_diurno
  action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.sonoff_100090196f
  - delay: 00:{{ states('input_number.tiempo_de_riego') | int }}:00
  - service: switch.turn_off
    data: {}
    entity_id: switch.sonoff_100090196f
  mode: single