How to use a list of different times?

I have an automation that is to be activated at different fixed times.
If I specify only one time, the automation works, but it doesn’t work when I specify more times.

- condition: template # Is the time now equal to a time in the list?
  value_template: '{{ now().time().strftime("%H:%M") == "10:00" }}'

I understand that I should use a list of times, but I don’t understand how to do it.
I would be grateful if someone could point me in the right direction.
This is how I thought it should be written, but it doesn’t work no matter how I write.

- condition: template # Is the time now equal to a time in the list?
  value_template: '{{ now().time().strftime("%H:%M") == ("10:00", "10:30", "12:00", "14:00") }}'

If you want it “activated” at certain times then time should be your trigger, not a condition.

trigger:
  - platform: time
    at:
      - "10:00:00"
      - "10:30:00"
      - "12:00:00"
      - "14:00:00"

Otherwise, you can use in as a test to see if the the current time is in your list:

- condition: template
  value_template: '{{ now().strftime("%H:%M") in ["10:00", "10:30", "12:00", "14:00"] }}'
1 Like

What Drew says, but to answer the technical question:

value_template: '{{ now().time().strftime("%H:%M") in ["10:00", "10:30", "12:00", "14:00"] }}
1 Like

For the purposes of this application, there’s no need to use the time() method; you can use the strftime() method directly.

- condition: template
  value_template: "{{ now().strftime('%H:%M') in ['10:00', '10:30', '12:00', '14:00'] }}"

Can you post the automation that uses this condition?

1 Like

Thank you very much for your help! This code suited me perfectly.

- condition: template
  value_template: "{{ now().strftime('%H:%M') in ['10:00', '10:30', '12:00', '14:00'] }}"

This is my automation, an automatic irrigation system for my greenhouse.
It’s not quite finished yet but it’s ready to use.

The length of the irrigation time is calculated from the temperature in the greenhouse (parabolic curve).
Irrigation is activated at certain times between 08:00 and 16:00, with a more frequent interval in the middle of the day when it is hottest.
What I have left to do is to activate the time multiplier for manual adjustment, and maybe I will use capacative soil moisture sensors to sense the soil moisture.

automation:
  - alias: "Greenhouse irrigation"
    description: "Start irrigation in the greenhouse"
    trigger:
      platform: time_pattern
      minutes: "/1" # = Run the automation every minute.
    condition:
      condition: and 
      conditions: # Are all the conditions for starting irrigation met?
            
        - condition: state # Is the automatic irrigation activated "on"?
          entity_id: input_boolean.bevattning_v_hus_enable
          state: "on"      

        - condition: template # Is it warm enough for irrigation?
          value_template: "{{ states.sensor.bevattning_v_hus_temp.state|int >= 5 }}"
          
        - condition: template # Is the estimated irrigation time greater than 0 seconds?
          value_template: "{{ states.sensor.bevattning_v_hus_runtime.state|float > 0 }}"

        - condition: template # Specify all times for irrigation 08:00 to 16:00.
          value_template: "{{ now().strftime('%H:%M') in ['08:00', '09:00', '10:00', '10:30'] }}"

    # Start irrigation if all conditions are met.
    action:
      - service: switch.turn_on
        entity_id: switch.smart_switch_gen5 # Start irrigation

      # Wait the estimated irrigation time, 2 to 30 sec.
      - delay: "{{ states('sensor.bevattning_v_hus_runtime')| int }}"

      - service: switch.turn_off
        entity_id: switch.smart_switch_gen5 # Turn off irrigation

# Input sensors
input_number:
  bevattning_v_hus_tidsmultiplikator: # Time multiplier for manual time adjustment.
    name: Tidsmultiplikator_v_hus
    icon: mdi:timer
    min: -5
    max: 5
    step: 0.5

# Boolean sensors
input_boolean:
  bevattning_v_hus_enable: # Enable / Disable the automatic.
    name: Auto På / Av

# Template sensors
sensor:
  - platform: template # Calculate the irrigation time, parabolic curve,  based on the temperature, 10 to 30°C.
    sensors:
      bevattning_v_hus_runtime:
        friendly_name: "Bevattningstidens längd"
        unit_of_measurement: "s"
        value_template: >
          {% if ((states('sensor.lumi_temp_1')|float) >= 10) %}
          {% if not states('sensor.lumi_temp_1') == 'unknown' %}
          {{(-5*((states('sensor.lumi_temp_1')|float)/((states('sensor.lumi_temp_1')|float)-35)))|int }}
          {%endif%}
          {%else%}
            0
          {%endif%}

The Time Pattern Trigger is frequently misused.

If your intention is to compute an irrigation schedule based on the temperature, it can be performed by a Template Sensor (whose device_class is timestamp). Then the automation can employ a Time Trigger. It will trigger exclusively at the time(s) set by the Template Sensor (and not every minute like it’s now doing).

I’m going to echo Taras’ comment… using Time Pattern as your trigger (at least with the conditions you have included) doesn’t make much sense and is very inefficient. Your automation is being triggered 1440 times a day, but a maximum of 4 of those trigger events can actually lead to executing the actions.

Thanks for the information, I have now changed the way to trigger my automation. I come from the Fibaro environment where most things are triggered every minute.
Since I am a beginner with Home Assistant, the old way of thinking is still there :slight_smile:
But I take your tips to heart and am grateful for any help I get.
Home Assistant is a wonderfully nice system and I’m glad to have left Fibaro which has a hopelessly outdated and boring user interface.