Hi, total novice at HA and YAML. Steep learning curve! I originally set up an automation to turn on some lights and start music at 07:00am weekdays. The automation has been working perfectly. Now I’d like to venture further afield and enhance it to adjust the start time based on whether or not I have a specific iCloud calendar event (e.g. Fridays ever other week or so but it can vary). I’ve enabled CalDAV integration for iCloud calendar, can see my cal entries in calendar.family, etc but I can’t figure out how to get my automation to inspect the current day’s calendar events for the code word ‘B-12’ and decide whether or not to start the alarm an hour earlier than 07:00. I’ve tried all sorts of recommendations from ChatGPT and Grok but nothing seems to work. Either I get syntax errors or the logic just gets skipped. I’m just learning and fumbling along right now, trying to learn as I go.
Any recommendation on how to approach this? All thoughts welcome. Cheers,
S.
First, post your current automation.
There are a few ways to approach this… so we need more information.
Is the “B-12” calendar event an “all-day” event or does it have a specific start time? If it’s not all-day, is the start always at the same time? If either of these are true, it should be possible to use a Calendar trigger with an offset so it fires at 6am.
If the “6am” automation runs should the “7am” automation still run as well?
Just FYI, ChatGPT and the other bullshit engines are bad at Home Assistant and produce a lot of plausible-looking, non-functional configurations. HA updates frequently and the majority of available examples that LLMs can pull from are old and/or from people posting non-working configs here or on Reddit. Deciphering and correcting LLM slop often requires a higher level of knowledge than it would take to just create the config yourself from scratch.
Many thanks for the quick replay. Here’s some more details
Use Case:
Trigger a set of actions (turn on lights, turn on music) weekdays at 7:00am unless for today there is a B-12 entry in calendar.family in which case trigger the set of actions at 6:00am instead of 7:00am. The first part before the ‘unless’ is working perfectly. The B-12 events have a start time that can vary between 7:15 and 9:15; it depends on various external factors. The title of the event is always beginning with ‘B-12’.
Current Working Automation:
alias: Morning_Wake_Up
description: ""
triggers:
- trigger: time
at: "07:00:00"
conditions:
- condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
actions:
- action: media_player.play_media
metadata: {}
data:
media_content_type: favourite
media_content_id: "4"
target:
entity_id: media_player.beosound_emerge_33724524
- action: light.turn_on
metadata: {}
data: {}
target:
device_id:
- ca31ebeaec825f3a36cab49b3225df9d
- 9ca7d7d3babac696e10d5b25aebeb2a2
- 79c426020a20f47e4907fec84afc48f1
- 65410d2f21f2e0e494abcd8e51c4ee8f
Non-working Enhancement:
alias: Morning_Wake_Up
trigger:
- platform: time
at: "05:59:00"
condition:
- condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
actions:
- action:
- variables:
has_b12_event: >
{% set today = now().date() %}
{% set calendar = states.calendar.family %}
{% for entity in calendar %}
{% if entity.attributes.start_time is defined %}
{% set event_date = as_timestamp(entity.attributes.start_time) | timestamp_custom('%Y-%m-%d') %}
{% if event_date == today.strftime('%Y-%m-%d') and
entity.attributes.message is defined and
'B-12' in entity.attributes.message %}
true
{% endif %}
{% endif %}
{% endfor %}
false
alarm_time: >
{{ '06:00:00' if has_b12_event else '07:00:00' }}
- wait_template: "{{ now().strftime('%H:%M:%S') >= alarm_time }}"
- action: media_player.play_media
metadata: {}
data:
media_content_type: favourite
media_content_id: '4' ....
I’m running 2025.3.4 on a Pi4 with 8GB Ram.
Cheers!!
Ok, with those answers you’re going to need to use the calendar.get_events
action and a little bit of templating.
alias: Morning_Wake_Up
description: ""
triggers:
- trigger: time
at:
- "06:00:00"
- "07:00:00"
conditions:
- condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
actions:
- alias: "Get all of today's events from the calendar"
action: calendar.get_events
target:
entity_id:
- calendar.family
data:
start: "{{ today_at('00:00')}}"
end: "{{ today_at('23:59')}}"
response_variable: agenda
- alias: "Check whether it is either 6am on a B12 day or not-6am on a non-B12 day"
condition: template
value_template: |
{% set b12 = agenda['calendar.family']['events'] | selectattr('summary', 'match', 'B-12') | list | count > 0 %}
{{ b12 == (now().hour == 6) }}
- action: media_player.play_media
metadata: {}
data:
media_content_type: favourite
media_content_id: "4"
target:
entity_id: media_player.beosound_emerge_33724524
- action: light.turn_on
metadata: {}
data: {}
target:
device_id:
- ca31ebeaec825f3a36cab49b3225df9d
- 9ca7d7d3babac696e10d5b25aebeb2a2
- 79c426020a20f47e4907fec84afc48f1
- 65410d2f21f2e0e494abcd8e51c4ee8f
Many thanks for looking at this!!! I’ll give this a try. Cheers!!!
Well, some success today, thanks @Didgeridrew !!
It’s kind of working except it doesn’t trigger the action when there is no ‘B-12’ entry in the calendar. I ran the following test a few times, and discovered it triggers the notify.notify action twice when there is a ‘B-12’ entry in the calendar and won’t trigger the action notify.notify when there is not a ‘B-12’ entry. I’m using notify.notify as a proxy for the actual actions of turning on lights and turning on music. I’ve a lot to learn!!
alias: DummyTest
description: ""
triggers:
- trigger: time
at:
- "06:00:00"
- "07:00:00"
conditions:
- condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
actions:
- alias: Get all of today's events from the calendar
action: calendar.get_events
target:
entity_id:
- calendar.family
data:
start_date_time: "{{ today_at('00:00')}}"
end_date_time: "{{ today_at('23:59')}}"
response_variable: agenda
- alias: Check whether it is either 6am on a B12 day or not-6am on a non-B12 day
condition: template
value_template: >
{% set b12 = agenda['calendar.family']['events'] | selectattr('summary',
'match', 'B-12') | list | count > 0 %}
{{ b12 == (now().hour == 6) }}
- action: notify.notify
metadata: {}
data:
message: Good
mode: single
I’m wondering if it’s possible to make the trigger start time a variable and depending on the result either trigger a start at 6 or 7. I think that would avoid the double trigger plus would handle both conditions of there being a ‘B-12’ entry present or not.
First, don’t use the notify.notify
action, you don’t know where it will send the notification.
From the Notify integration docs:
Second, post the automation’s debug trace so we can see what happened.
That is basically what the template condition already does. But, if you want to use the time from the trigger
variable instead of getting it from the now
function, you can change it as follows:
alias: Check whether the trigger fired at either 6am on a B12 day or not-6am on a non-B12 day
condition: template
value_template: >
{% set b12 = agenda['calendar.family']['events'] | selectattr('summary','match', 'B-12') | list | count > 0 %}
{{ b12 == (trigger.now.strftime('%H:%M') == '06:00') }}
I have tested the automation and cannot replicate what you have described…
In this run there was a B-12 event in the calendar but the notification wasn’t sent because the time element didn’t match:
The runs that should execute the notification action did:
with the help of this table I think I understand your point about the template condition so it should only trigger the action(s) when B-12 & 6 are true OR when not-B-12 and not 6 are true.
I’m not sure why I got an action trigger with the 4th item in the table. I’ll test it again. Many thanks!
I reran the tests again and am not able to reproduce the problems either so we have a winner!!! I’ll try it out for real tomorrow morning. Hopefully I don’t sleep in!! Thanks for your help in sorting this out.
Friday the automation went off without a hitch at 6:00am and today it went off at 7:00am as expected for most of the time. Awesome!!! Thanks again @Didgeridrew for the brainpower!!!