I have an automation that is triggered when I arrive home after sunset. Within the automation I have executing multiple scripts. In one section of the automation, I would like to conditionally choose which script to run based on what time it is.
My pseudo code would be something like this, where the first couple of scripts always execute, and the last section is conditional based on the time:
- service: script.some_script
- service: script.some_other_script
- service_template: >
{% if [current time before 11 pm] %}
script.evening_script
{% else %}
script.late_night_script
{% endif %}
I’m just not sure how to write the conditional. Ideas?
Do you always want to execute either script.evening_script or script.late_night_script or is there some time where you want neither of these to be executed. If you always want to execute either of the scripts then your template should work like this. If you have a time frame where you don’t want to execute either of the scripts, add a condition before you service template.
@Burningstone Maybe it will help if I post what I am using today. Below is the script, with a condition. Where it currently uses script.arrive_home_evening_kichen_lights, I would like to modify it to use that script if prior to midnight. And if after midnight, use a different script.
I would like the arrive_home_evening_outside_lights script to always fire, assuming the conditions are met.
- id: arrive_home_evening_lights
alias: 'Arrive Home Evening Lights'
initial_state: 'true'
trigger:
- platform: state
entity_id: binary_sensor.arrive_home
to: 'on'
condition:
condition: or
conditions:
- condition: sun
after: sunset
after_offset: "-00:20:00" # Starts firing 20 minutes before sunset
- condition: sun
before: sunrise
before_offset: "01:00:00" # Stops firing 1 hour before sunrise
action:
- service: script.arrive_home_evening_outside_lights
- service: script.arrive_home_evening_kichen_lights
Try like this: it should fire script.arrive_home_late_night_kitchen_lights after 11 PM until 6 AM and script.arrive_home_evening_kitchen_lights the between 6 AM and 11 PM. I didn’t test this.
- id: arrive_home_evening_lights
alias: 'Arrive Home Evening Lights'
initial_state: 'true'
trigger:
- platform: state
entity_id: binary_sensor.arrive_home
to: 'on'
condition:
condition: or
conditions:
- condition: sun
after: sunset
after_offset: "-00:20:00" # Starts firing 20 minutes before sunset
- condition: sun
before: sunrise
before_offset: "01:00:00" # Stops firing 1 hour before sunrise
action:
- service: script.arrive_home_evening_outside_lights
- service_template: >
{% if now().hour > 22 or now().hour < 6 %}
script.arrive_home_late_night_kitchen_lights
{% else %}
script.arrive_home_evening_kitchen_lights
{% endif %}
This looks great, and way less complex than I was imagining! Question: I think I can omit the “or now().hour < 6” since I have the before sunrise condition. Correct?
Then what what happens if you arrive at 1 AM in the morning? This is before 1 hour before sunrise, so one of your conditions is met and the hour is not greater than 22, therefore it executes the evening script, but it should execute the late night script right?
Glad that it works can you please mark the post containing the solution or helped you find the solution as a solution. This way future readers will find the solution quickly.