my wife wants to have an automation in Home Assistant, that switches a single light.
The conditions for switching on are:
automation is enabled
I have added a boolean helper:
input_boolean.automation_moodlight_kitchenwindow_activation
the light shall be switched on depending the value of a list:
sundown -2 hours
sundown -1 hour
sundown
sundown +1 hour
sundown + 2 hours
5 PM
6 PM
7 PM
8 PM
9 PM
10 PM
I have added a dropdown helper with the values.
Name of the entity:
input_select.automation_moodlight_kitchenwindow_activation_on
For example:
activation is enabled, 6 PM is selected → kitchenwindow moodllight shall be switched on at 6 PM
Duration shall be selected from another list:
1 hour
2 hours
3 hours
4 hours
5 hours
6 hours
I have added a dropdown helper with the values.
Name of the entity:
input_select.automation_moodlight_kitchenwindow_activation_off
The kitchenwindow moodllight shall be switched off after the selected value.
How do I get this working?
I have set up a card in the right dashboard with a condition:
when input_boolean.automation_moodlight_kitchenwindow_activation is on, the two dropdown list with the conditions are shown.
I have tried to set up an automation, but I dont get it managed.
From what you have described, one way to approach it is to have individual triggers for each of the item from the first list. Set the ID of each trigger to match the values in the Input Select, so a simple template condition can reject non-selected triggers:
I have tried to create an automation, but I got no result that seems to be close to a solution. Thats why I dont had something to show except the card in the dashboard. But I think, that this is uninteresting.
I know, that there are not all triggers inside and the duration is missing. I will add that when the triggers are right. Are they ok?
I dont think so. Automation will switch on the light if ist is 5 PM or if it is sunset -1 hour.
I want to have it it 5 PM when 5 PM is selected from the list.
That is the purpose of the Template condition I posted above… it only allows the actions to be executed if the Trigger ID matches the state of the input… i.e. “5 PM when 5 PM is selected”
Regarding the delay, I would add an action after you turn the light on to set a Timer. Use a second automation that turns the light “off” when the timer finishes (there are examples in the link that show how to use an Event trigger to listen for the timer_finished event).
You are right. I was confusing it. This is my first time with trigger IDs.
With your help, I got the automation for switching on working.
Switching off is missing now.
Do I have to add 6 automations:
If trigger ID is “1 hour” then set duration to 1 hour
If triffer ID is “two hours” then set duration to 2 hours
The switching on automation doesnt work correctly.
It switches on at 4 PM, 5 PM, 6 PM, 7 PM, 8 PM 9 PM, 10 PM, 2 hours before sunset, 1 hour before sunset, at sunset, 1 hour after sunset and 2 hours after sunset.
I havent worked with the triggers so I cannot judge, if it is the right way.
I want the light to switch on at the time that is set in the dropdown and not at every time that is possible to choose from the dropdown.
Do the trigger IDs you have assigned exactly match the options available in the Input Select? For example 5PM is not the same as 5 PM and sundown -1 hour is not the same as Sundown -1 Hour… make sure they match.
No.
Don’t use delays, they don’t survive restart or reloading automations… use a Timer. I would also avoid using toggle unless that the actual action you want to happen every time. What if someone turned the light off manually at some point during the 6 hour delay? Do you really want it to turn on?
Turn On automation with Timer start
alias: Moodlight
description: ""
triggers:
- id: 4 PM
trigger: time
at: "16:00:00"
- id: 5 PM
trigger: time
at: "17:00:00"
- id: 6 PM
trigger: time
at: "18:00:00"
- id: 7 PM
trigger: time
at: "19:00:00"
- id: 8 PM
trigger: time
at: "20:00:00"
- id: 9 PM
trigger: time
at: "21:00:00"
- id: 10 PM
trigger: time
at: "22:00:00"
- id: 2 hours before sundown
trigger: sun
event: sunset
offset: "-2:00:00"
- id: 1 hour before sundown
trigger: sun
event: sunset
offset: "-1:00:00"
- id: sundown
trigger: sun
event: sunset
- id: 1 hour after sundown
trigger: sun
event: sunset
offset: "+1:00:00"
- id: 2 hours after sundown
trigger: sun
event: sunset
offset: "+2:00:00"
- id: "off"
trigger: event
event_type: timer.finished
event_data:
entity_id: timer.YOUR_TIMER_ID_HERE
conditions:
- condition: state
entity_id: input_boolean.automation_moodlight_kitchenwindow_activation
state: 'on'
- condition: template
value_template: >-
{{ trigger.id in ['off', states('input_select.automation_moodlight_kitchenwindow_activation_on')] }}
actions:
- action: light.turn_{{ 'off' if trigger.id == 'off' else 'on' }}
metadata: {}
data: {}
target:
entity_id: light.YOUR_LIGHT_ID_HERE
- condition: template
value_template: "{{ trigger.id != 'off'}}"
- action: timer.start
metadata: {}
target:
entity_id: timer.YOUR_TIMER_ID_HERE
data:
duration: >
{% set hours =
(states('input_select.automation_moodlight_kitchenwindow_activation_off')).split()[0]|int(1) %}
{{hours * 3600}}
mode: queued
switched on by automation
automation_moodlight_kitchenwindow_activation sunset with offset
15:10:42 - 3 hours ago
Sundown was at 17.11.
That means, that the automation was activated by this part of the automation:
- id: 2 hours before sundown
trigger: sun
event: sunset
offset: "-02:00:00"
The helper is called “2 hours before sundown”.
Does the sequence matter?
In the automation, it is
4 PM
5 PM
6 PM
7 PM
8 PM
9 PM
10 PM
2 hours before sundown
1 hour before sundown
sundown
1 hour after sundown
2 hours after sundown
In the helper list, it is:
2 hours before sundown
1 hour before sundown
sundown
1 hour after sundown
2 hours after sundown
4 PM
5 PM
6 PM
7 PM
8 PM
9 PM
10 PM
When switching on works correclty, I will look for switching of like you said.