How to pass message from calendar.get_events

Hey, new to HA, so bear with me. I have the following script. and I want to pass the summary of the day’s calendar event to tts.speak. But for some reason I can’t figure out how to get tts.speak to play the event.summary on my speaker. Any help is much appreciated

alias: Coffee Calendar notification
description: ""
trigger:
  - platform: device
    type: turned_on
    device_id: 6bd132949dd1d9fe359cdd967c10e8e4
    entity_id: ce61ba6e69eb0c042684f66a7c19d2d0
    domain: switch
condition: []
action:
  - service: calendar.get_events
    metadata: {}
    data:
      duration:
        hours: 48
        minutes: 0
        seconds: 0
    target:
      entity_id: calendar.family
    response_variable: CoffeeCalendarEvents
  - service: media_player.volume_set
    metadata: {}
    data:
      volume_level: 0.1
    target:
      entity_id: media_player.woonkamer
  - service: tts.speak
    metadata: {}
    data:
      cache: false
      media_player_entity_id: media_player.woonkamer
      language: nl
      message: null
    target:
      entity_id: tts.google_en_com
mode: single

These are the ‘changed variables’ that I get:

context:
  id: 01HZ3255VT77YBBTK904PCF1YH
  parent_id: 01HZ3255VTVHDMXBXZVTQE47T9
  user_id: null
agenda:
  calendar.family:
    events:
      - start: '2024-05-29'
        end: '2024-05-30'
        summary: ###
      - start: '2024-05-31T12:30:00+02:00'
        end: '2024-05-31T13:30:00+02:00'
        summary: '### '
      - start: '2024-05-31T18:00:00+02:00'
        end: '2024-05-31T22:15:00+02:00'
        summary: ###

The example you posted contains three summaries, one for each of the three days. Which one do you want to send to tts.speak? Or do you want all of them?

If all of them, do you want an introduction spoken before each summary (such as the date and time) or just speak all three in sequence?

Preferably a quick intro message followed by each three of the summaries with the time of the event

This should help you get started:

alias: Coffee Calendar notification
description: ""
trigger:
  - platform: device
    type: turned_on
    device_id: 6bd132949dd1d9fe359cdd967c10e8e4
    entity_id: ce61ba6e69eb0c042684f66a7c19d2d0
    domain: switch
condition: []
action:
  - service: calendar.get_events
    metadata: {}
    data:
      duration:
        hours: 48
        minutes: 0
        seconds: 0
    target:
      entity_id: calendar.family
    response_variable: CoffeeCalendarEvents
  - service: media_player.volume_set
    metadata: {}
    data:
      volume_level: 0.1
    target:
      entity_id: media_player.woonkamer
  - service: tts.speak
    metadata: {}
    data:
      cache: false
      media_player_entity_id: media_player.woonkamer
      language: nl
      message: >
        {% set events = CoffeeCalendarEvents['calendar.family'].events %}
        {% set ns = namespace(msg='') %}
        {% set ns.msg = 'Good day! There are ' ~ events | count ~ ' upcoming events. ' %}
        {% for e in events %}
          {% set dt = e.start | as_datetime | as_local %}
          {% set t = dt.strftime('%-H:%M') %}
          {% set ns.msg = ns.msg ~ 'On {}, {}, {}. '.format(dt.strftime('%A %b %d'), 'all-day event' if t == "0:00" else t, e.summary) %} 
        {% endfor %}
        {{ ns.msg }}
    target:
      entity_id: tts.google_en_com
mode: single

The generated message looks something like this:

Good day! There are 3 upcoming events. On Wednesday May 29, all-day event, Bob’s birthday. On Friday May 31, 6:30, Walk the dog. On Friday May 31, 12:00, Lunch with Lisa.

At a minimum, the example demonstrates how to iterate through the upcoming events and convert them into spoken English phrases. Obviously the template can be customized to produce the exact phrases you prefer in your native language.

1 Like

This is amazing. this will definitely help me figure it out.

What you will find is that iterating through the events is the easy part. The challenging part is converting the date, time, and message into a sentence that sounds natural when spoken. For example, it can get complicated if you want it to say “Today” and “Tomorrow” instead of saying the complete date (for events that occur today/tomorrow).

Be advised that strftime can only convert to English months and days.

1 Like