Local Calendar Events with Automations

Long time lurker, first time poster. I am working on an automation for the start of our school year. I have created a local calendar that I have put in all the days that my boys will be going to school. I want to use this automation to start with to be an alarm to wake them up in the morning on school days. I have gone through the whole year and added an All Day event with the message being their name. When I attempt to make an automation that would trigger on their name to turn on their lights gradually, it has not worked. I was curious if it is because I have the event setup as an all day event or if I should switch them to a time when school starts and offset for when they need to wake up. I was hoping to us the all day event to trigger other ideas down the road, but wanted to start with this. My automation looks like this:

alias: School Day - Kid1
description: ""
trigger:
  - platform: time
    at: "06:30:00"
condition:
  - condition: template
    value_template: |
      {{ state_attr('calendar.school', 'all_day') and
         state_attr('calendar.school', 'message') == 'Kid1' and
         as_timestamp(state_attr('calendar.school', 'start_time')).strftime('%Y-%m-%d') == now().strftime('%Y-%m-%d') }}
    alias: Check Event Title
action:
  - data:
      message: School Day Kid 1
    action: notify.dad
  - action: light.turn_on
    metadata: {}
    data:
      transition: 120
      brightness_pct: 100
    target:
      device_id: 1361f7d72de5b
mode: parallel

Any thoughts on what I should do different?

strftime() is a datetime method, it doesn’t work directly on timestamp integers. The analogous function for timestamps is timestamp_custom(). I usually just use datetimes:

...
condition:
  - condition: template
    value_template: |
      {{ state_attr('calendar.school', 'all_day') and
       is_state_attr('calendar.school', 'message', 'Kid1') and
       (state_attr('calendar.school', 'start_time')|as_datetime|as_local).date() == now().date() }}
    alias: Check Event Title
....

The other possible issue is if the calendar you are using has overlapping/concurrent events. When calendar events overlap, only one is represented in the calendar entity’s state object so the event you are conditioning for may not be the one whose data is available. If overlapping events might possibly occur, you need to use the calendar.get_events action.

1 Like

That makes sense! I will have it run tomorrow and test it out.

I have another setup that I am trying for kid2 tomorrow as well:

{% set today = now().date() %} {% set events = state_attr(‘calendar.school’, ‘entries’) %} {% for event in events %}
{% if event.start_time.date() == today and event.message == ‘Kid2’ %}
true
{% endif %}
{% endfor %} false

I am using one calendar and have each of my boys listed the days they have school (two events for one day). I was thinking about breaking theirs into two separate calendars and then marking which has school in each calendar. Was going for the WAF by having one calendar but, sacrafices. :slight_smile: