action:
service: switch.turn_on
data_template :
entity_id: switch.plant_lights
delay: >
{% if is_state("input_select.plant_light_cycle_choice", "Grow") %}
20:00:00
{% if is_state("input_select.plant_light_cycle_choice", "Flower") %}
12:00:00
{% else %}
You need something here if there is another choice or if it is undefined.
{% endif %}
If there are only two choices in your input select pick a default in case it becomes undefined (e.g. “grow” in this example):
Be very careful that the automation does not trigger again while waiting in the delay. Or the delay will be skipped and the switch will turn off. Also restarting home assistant will stop the delay and the lights won’t turn off.
A better way would be to turn the switch on with one automation and use another to turn it off. Like this:
###Plant light timer on
- id: plant_light_cycle_on
alias: Plant lights time cycle on
trigger:
platform: template
value_template: "{{ states('sensor.time') == (states.input_datetime.plant_light_start_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
action:
service: switch.turn_on
entity_id: switch.plant_lights
###Plant light timer off
- id: plant_light_cycle_off
alias: Plant lights time cycle off
trigger:
platform: state
entity_id: switch.plant_lights
to: 'on'
for: "{% if is_state('input_select.plant_light_cycle_choice', 'Flower') %} 20:00:00 {% else %} 12:00:00 {% endif %}"
action:
service: switch.turn_off
entity_id: switch.plant_lights
The thing to note here is that restarting home assistant will reset the off timer to 12 or 20 hours. Have a read of this if that worries you: