Yeah you’re right about the service. That was stupid of me. I’m not actually sure you can template a delay.
Edit: yeah you should be able to.
action:
- service: switch.turn_on
entity_id: switch.portable_heater
- delay: "{% if is_state('input_select.plant_light_cycle_choice', 'Flower') %} 20:00:00 {% else %} 12:00:00 {% endif %}"
- service: switch.turn_off
entity_id: switch.portable_heater
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: