Hi.
I am new to HA.
I connected my calendars (there are few) and now I would like to automate sending of a daily agenda email just like google sends.
I am not best at programming but I can figure things out.
where to start?
I found few examples of sending emails but finding the events and adding them to the email is a different matter.
You will need to use the calendar.get_events
action to retrieve the events from your calendars. If you do a search of the forums for that action you will find many examples. Also, make sure to check out the description and example provided in the docs
You will likely want to use a template to process the value that is returned by the action into something more useful for an email message. Please describe what formatting needs you have so we can help you on that front.
alias: DailyAgenda
description: ""
trigger:
- platform: time
at: "05:00:00"
action:
- target:
entity_id:
- calendar.anniversaries
- calendar.birthdays
data:
start_date_time: "{{ now().strftime('%Y-%m-%d 00:00:00') }}"
end_date_time: "{{ now().strftime('%Y-%m-%d 23:59:59') }}"
response_variable: events
action: calendar.get_events
- variables:
all_events: |-
{% set all_events = [] %}
{% for calendar, event_list in events.items() %}
{% set all_events = all_events + event_list.events %}
{% endfor %}
sorted_events: |-
{% set sorted_events_list = all_events | sort(attribute='start') %}
{{ sorted_events_list }}
agenda: |-
{% for event in sorted_events -%}
{% if 'T' in event.start -%}
{% set start_date = event.start | as_datetime %}
{% set end_date = event.end | as_datetime %}
{{ "%02d:%02d" | format(start_date.hour, start_date.minute) }} - {{ "%02d:%02d" | format(end_date.hour, end_date.minute) }}: {{ event.summary }}
{% else %}
All day event: {{ event.summary }}
{% endif -%}
{% endfor -%}
- data:
message: "{{ agenda }}"
title: Daily Agenda
target: [email protected]
action: notify.dailyagenda
mode: single
So that’s where I ended up
- trigger - works
- querying multiple calendar - works
- sending email - works
I struggle the “agenda” variable
I would like to have one line line per event and display
- only start ad end time (in local time zone) and and summary for non all day events
- All day event: summary for all day events
events sorted by start time
I suppose there will be challenges with events that start in one day and end with another like say I fly for 12h and start 10pm
Here’s a slightly different way to approach it which will set any overnight events to have midnight start times so they sort.
alias: DailyAgenda
description: ""
trigger:
- platform: time
at: "05:00:00"
action:
- target:
entity_id:
- calendar.anniversaries
- calendar.birthdays
data:
start_date_time: "{{ today_at('00:01') }}"
end_date_time: "{{ today_at('23:59') }}"
response_variable: events
action: calendar.get_events
- variables:
all_events: |
{{ events.values() | map(attribute='events') | sum(start=[]) }}
agenda: |
{% set ns = namespace(cal_events=[]) %}
{% for event in all_events|selectattr('start','contains','T') %}
{%- set start = event.start|as_datetime|as_local %}
{%- set start = start if start >= today_at() else today_at() %}
{%- set end = event.end|as_datetime|as_local %}
{%- set ns.cal_events = ns.cal_events + [(start).strftime('%H:%M')~'-'~(end).strftime('%H:%M')~': '~ event.summary] %}
{%- endfor %}
{% set ad = all_events|rejectattr('start','contains','T')|map(attribute='summary')|list %}
{{ '' if ad|count == 0 else (['All day events:'] + ad) | join('\n\- ') }}
{{ ns.cal_events | sort | join('\n') if ns.cal_events | count > 0 else 'Nothing to do Today!'}}
- action: notify.dailyagenda
data:
message: "{{ agenda }}"
title: Daily Agenda
target: [email protected]
mode: single
This is so great I would not shy to call it beautiful.
Thank you!