I’m starting out with Home Assistant and I was wondering if you could help me.
I have an Input Select with 3 options.
Morning
Afternoon
Evening
Can I create one automation which detects what time of day it is and based on that select morning, afternoon or evening? Or do I need to have one automation for each mode?
Assuming Morning is before 12 and afternoon is before 18 you can use a condition as below:
{%- if as_timestamp(now()) | timestamp_custom('%H') | int < 12 -%}Morning
{%- elif as_timestamp(now()) | timestamp_custom('%H') | int < 18 -%}Afternoon
{%- else -%}Evening{%- endif -%}
The full automation would be:
- alias: Set Input SelectTime of Day
initial_state: false
trigger:
- platform: time
hours: '/1'
minutes: 0
seconds: 0
action:
- service: input_select.select_option
data_template:
entity_id: input_select.time_of_day
option: >
{%- if as_timestamp(now()) | timestamp_custom('%H') | int < 12 -%}Morning
{%- elif as_timestamp(now()) | timestamp_custom('%H') | int < 18 -%}Afternoon
{%- else -%}Evening{%- endif -%}
You’d need to replace input_select.time_of_day with the actual entity name of your input_select and replace Morning/Afternoon/Evening with the actual options you have set up for your input_select (if different)
Note I’ve not tested the above but should get you well on your way
Thank you so much for the replies. I’ve added the following to my automations file but it doesn’t seem to be working. Any advise on what I am doing wrong?
no that should be fine because he’s using elif and not a standard if so 3 would be caught on the first if statement.
It does mean that as soon as you pass midnight it will be morning though, not night anymore…
this needs to be reworked alittle the way it’s written. the last elif won’t be reached due to the fact that < 0 doesn’t exist when dealing with time, if you want it to be 18 to 24 hours, you should use 24 instead (which is essentiall midnight). Also, I’m guessing you don’t want night to never occur, so you need to add in the correct timing.
It’s been a while but for those that come looking… I think the answer to @rlyons20 is that we need a time_pattern trigger. I went with a sensor template as per the above because you don’t have to wait until the top of the hour for a value to populate, but if you want to use an automation:
Found this looking for something like this. Lots of out of date stuff.
Now Stardate 20221004. Its 4 years on. Try:
Variable; ‘dayperiod’ stores the data and can be used else where in
programming .Written in Sensors.yaml.