Dear Community,
So to give some more context. I am using Alarm clock add on from HACS and I would like that every sunday at 20:00 an automation triggers that checs the next 5 days (Mon-Fri) in two of my calendars (Vacations and spain_ct). If there is any event in these calendars in any of the next 5 days if turns off the corresponding days alarm.
At the moment the below code works excpet for the variable Monday. I am trying to read the attribute summary in agenda_mo variable and if there is any value (different from empty/blank) then return the value true to the variable Monday. If there is no value return False.
Is any able to help?
This is a snipet of my code referring just to the Monday, it then repeats for other days:
triggers:
- at: "20:00:00"
trigger: time
weekday:
- sun
conditions: []
actions:
- sequence:
- alias: Get calendar events for Monday
data:
start_date_time: "{{ today_at() + timedelta(days=1) }}"
duration:
hours: 24
response_variable: agenda_mo
action: calendar.get_events
target:
entity_id:
- calendar.vacations
- calendar.spain_ct
- variables:
Monday: >
{{ agenda_mo['calendar.vacations'].events | selectattr('summary',
'search', '*', true) | list | count > 0 }}
if:
- condition: template
value_template: "{{ Monday == true }}"
then:
- type: turn_off
device_id: xxxxxxx
entity_id: xxxxxxx
domain: switch
any help would be greatly appreciated, maybe there is a more elegant way to do this.
What is the purpose of | selectattr('summary', 'search', '*', true)? Do these calendars contain events that shouldn’t be used?
First, if your calendar includes all-day events, you’re going to cause yourself issues using today_at(), instead use today_at('0:01:00'). There’s a weird quirk where all day events that end on the day you are querying are pulled in. So, the same goes for the duration or end_date_time, set it so it ends just before midnight.
Second, you are currently only factoring in the data from one calendar into the Monday variable. Use merge_response(agenda_mo) to merge the response variable into a single list of events.
If you are just doing the same thing over and over for the different days of the week, use a Repeat action. You can use the count index to provide the day offset for the calendar.get_events action.
Thanks For the tips, there some i will try. In fact the issue is exactly with the part of code you have indicated. The purpose is to search is any of the calendar event summaries have any value (different to empty/blank). I want to know if the calendar.get.events found any event on that specific day. from what I see though the * does not work in yaml like it does in some other language as a wildcard.
Really all I want to does is run the calendar.get.events for tomorrow for the two calendars and if the is any event tomorrow turn of the alarm.
If there is no event on that day, then the response will return an empty list… there’s no need to search the summary unless there are some summary values that need to be ignored when deciding to turn the switch off.
This is kind of quick and dirty, and could be made a more resilient by setting it up in such a way that it calculated the current day instead of requiring it to run on Sunday:
triggers:
- at: "20:00:00"
trigger: time
weekday:
- sun
conditions: []
actions:
- repeat:
for_each:
- weekday: monday
offset: 1
- weekday: tuesday
offset: 2
- weekday: wednesday
offset: 3
- weekday: thursday
offset: 4
- weekday: friday
offset: 5
sequence:
- alias: Get calendar events for day
data:
start_date_time: "{{ today_at('0:01') + timedelta(days=repeat.offset) }}"
end_date_time: "{{ today_at('23:59') + timedelta(days=repeat.offset) }}"
response_variable: agenda_mo
action: calendar.get_events
target:
entity_id:
- calendar.vacations
- calendar.spain_ct
- condition: template
value_template: "{{ merge_response(agenda_mo) | count > 0 }}"
- action: switch.turn_off
target:
entity_id: switch.example_{{repeat.weekday}}