Add calendar friendly name to get_calendar_events output

I am pulling in my calendar events fine and storing them using this:

template:
  - unique_id: get_calendar_events
    trigger:
      - platform: time_pattern
        minutes: 3
    action:
      - service: calendar.get_events
        data:
          duration:
            hours: 36
        target:
          entity_id:
            - calendar.gavin
            - calendar.gavin_work
            - calendar.general
            - calendar.kids
            - calendar.whole_family
        response_variable: calendars
      - variables:
          e: |
            {% set ns = namespace(cal_events=[]) %}
            {%- for key, value in calendars.items() %}
              {%- for event in value.events %}
                {%- set ns.cal_events = ns.cal_events + [event] %}
              {%- endfor %}
            {%- endfor %}
            {{ ns.cal_events | sort(attribute='start') | list }}
    sensor:
      - name: "MultiCal 72 hour agenda"
        unique_id: multical_agenda
        state: "{{ e | count }}"
        attributes:
          events: "{{ e }}"
          entity_id: "{{ ['calendar.gavin'] }}"

My markdown is working pretty well and I am using this:

cards:
  - type: markdown
    content: |
      {% for l in range(0,(states.sensor.multical_72_hour_agenda.state | int(0))) %}
        {{ as_timestamp(state_attr('sensor.multical_72_hour_agenda','events')[l].start) | timestamp_custom("%d %b %H:%M") }} {{ state_attr('sensor.multical_72_hour_agenda','events')[l].summary }}
      {%- endfor %}

which outputs this:

13 Mar 00:00 NAPLAN H and G
16 Mar 07:00 Parkrun
16 Mar 08:30 Girl Guides Stall
16 Mar 14:00 Pure Imagination
16 Mar 14:00 Pure Imagination
17 Mar 00:00 Edie James birthday (2016)
17 Mar 08:00 JSC Training
17 Mar 14:00 Shift
17 Mar 14:40 Cobras vs Panthers Fd2
17 Mar 16:10 Touch vs Slayers Fd 5 Away
17 Mar 16:45 Restorative Yoga
17 Mar 17:00 Sapphires vs Red Frogs Fd 2 Home
18 Mar 00:00 Recycling Bin

What I would like is to also prefix the event title with the friendly name of the calendar that it originated from. Any ideas how they might be achieved (if possible at all?)

1 Like

it’s possible.
see calendar_name below. this pulls out the friendly name. it doesn’t exist in the event. need to then add it to your events object and pull it out down below

            {% set ns = namespace(cal_events=[]) %}
            {%- for key, value in calendars.items() %}
              {%- for event in value.events %}
                {% set calendar_name = state_attr(key, 'friendly_name') %}
                {%- set ns.cal_events = ns.cal_events + [dict(event,  calendar_name=calendar_name)]  %}
              {%- endfor %}
            {%- endfor %}
            {{ ns.cal_events | sort(attribute='start') | list }}

then pull it out here

cards:
  - type: markdown
    content: |
      {% for l in range(0,(states.sensor.multical_72_hour_agenda.state | int(0))) %}
        {{ as_timestamp(state_attr('sensor.multical_72_hour_agenda','events')[l].start) | timestamp_custom("%d %b %H:%M") }} 
{{ state_attr('sensor.multical_72_hour_agenda','events')[l].calendar_name}}
{{ state_attr('sensor.multical_72_hour_agenda','events')[l].summary }}
      {%- endfor %}

warning, untested code! but hopefully it gets you the key idea.

2 Likes

Thanks it’s not working out of the box but I will have more of a look in the next few days when I have time. I see the general idea.

Just didn’t need the double braces around state_attr

            {% set ns = namespace(cal_events=[]) %}
            {%- for key, value in calendars.items() %}
              {%- for event in value.events %}
                {% set calendar_name = state_attr(key, 'friendly_name') %}
                {%- set ns.cal_events = ns.cal_events + [dict(event,  calendar_name=calendar_name)]  %}
              {%- endfor %}
            {%- endfor %}
            {{ ns.cal_events | sort(attribute='start') | list }}
1 Like

oh, duh. yup. i’ll update mine as well. i wrote the code above blind w/o being able to test it.

but the key concept was correct to pull the friendly name via the key and then add it to the dict.

1 Like