Calendar - new entitites or helper that show a list of calendar events within a certain scope

I also like to announce my appointments of the day with TTS in my morning routine

1 Like

Hello everyone,
I have installed “HASS Calendar Addon”, but I can’t see a sensor anywhere, what am I doing wrong.
Can someone help me with this?
Thanks for every response

What does the addon’s log say?

Thanks for you reaction

s6-rc: info: service s6rc-oneshot-runner: starting
s6-rc: info: service s6rc-oneshot-runner successfully started
s6-rc: info: service fix-attrs: starting
s6-rc: info: service fix-attrs successfully started
s6-rc: info: service legacy-cont-init: starting
cont-init: info: running /etc/cont-init.d/00-banner.sh
-----------------------------------------------------------
 Add-on: Hass Calendar Addon
 Addon to consume caldav and google calendars and make calendar events available as sensordata in Home Assistant.
-----------------------------------------------------------
 Add-on version: 0.301
 You are running the latest version of this add-on.
 System: Home Assistant OS 10.0  (amd64 / qemux86-64)
 Home Assistant Core: 2023.4.6
 Home Assistant Supervisor: 2023.04.1
-----------------------------------------------------------
 Please, share the above information when looking for help
 or support in, e.g., GitHub, forums or the Discord chat.
-----------------------------------------------------------
cont-init: info: /etc/cont-init.d/00-banner.sh exited 0
cont-init: info: running /etc/cont-init.d/01-log-level.sh
cont-init: info: /etc/cont-init.d/01-log-level.sh exited 0
s6-rc: info: service legacy-cont-init successfully started
s6-rc: info: service legacy-services: starting
s6-rc: info: service legacy-services successfully started
added 80 packages, and audited 81 packages in 14s
23 packages are looking for funding
  run `npm fund` for details
found 0 vulnerabilities
npm notice 
npm notice New minor version of npm available! 9.1.2 -> 9.6.5
npm notice Changelog: <https://github.com/npm/cli/releases/tag/v9.6.5>
npm notice Run `npm install -g [email protected]` to update!
npm notice 
System locale: en-US
Luxon locale set to: nl-BE
System timeZone: Europe/Brussels
Luxon timezone set to: Europe/Brussels
Previously stored events posted to sensor(s) at: Sun Apr 23 2023 17:50:11 GMT+0200 (Central European Summer Time)
postEventsAllCalendars error: TypeError: Cannot read properties of undefined (reading 'length')
postEventsAllCalendars error: TypeError: Cannot read properties of undefined (reading 'length')
postEventsAllCalendars error: TypeError: Cannot read properties of undefined (reading 'length')

There was an issue about that log error a couple months ago… related to an intermittent problem with empty calendars. I don’t know if it was fixed yet.

Make sure there is at least one event in your calendar(s) within the time frame of fetchDaysPast - fetchDays, restart the addon. Remember that the sensor only sends a fetch request based on your fetchCron time configuration variable. If you use the default, that means it will fetch on the hour and at half-past. Sometimes the first fetch after restart fails, so let it run uninterrupted through at least 2 fetch cycles.

Hi,

Thank you for your response.

In attachment you see my calendar to test and also my settings.

If I understand you correctly, the add-on will retrieve the data from the calendar every 30 minutes? Is that correct?


After waiting 30 minutes I get this in the log:

s6-rc: info: service s6rc-oneshot-runner: starting
s6-rc: info: service s6rc-oneshot-runner successfully started
s6-rc: info: service fix-attrs: starting
s6-rc: info: service fix-attrs successfully started
s6-rc: info: service legacy-cont-init: starting
cont-init: info: running /etc/cont-init.d/00-banner.sh
-----------------------------------------------------------
 Add-on: Hass Calendar Addon
 Addon to consume caldav and google calendars and make calendar events available as sensordata in Home Assistant.
-----------------------------------------------------------
 Add-on version: 0.301
 You are running the latest version of this add-on.
 System: Home Assistant OS 10.0  (amd64 / qemux86-64)
 Home Assistant Core: 2023.4.6
 Home Assistant Supervisor: 2023.04.1
-----------------------------------------------------------
 Please, share the above information when looking for help
 or support in, e.g., GitHub, forums or the Discord chat.
-----------------------------------------------------------
cont-init: info: /etc/cont-init.d/00-banner.sh exited 0
cont-init: info: running /etc/cont-init.d/01-log-level.sh
cont-init: info: /etc/cont-init.d/01-log-level.sh exited 0
s6-rc: info: service legacy-cont-init successfully started
s6-rc: info: service legacy-services: starting
s6-rc: info: service legacy-services successfully started
added 80 packages, and audited 81 packages in 14s
23 packages are looking for funding
  run `npm fund` for details
found 0 vulnerabilities
npm notice 
npm notice New minor version of npm available! 9.1.2 -> 9.6.5
npm notice Changelog: <https://github.com/npm/cli/releases/tag/v9.6.5>
npm notice Run `npm install -g [email protected]` to update!
npm notice 
System locale: en-US
Luxon locale set to: nl-BE
System timeZone: Europe/Brussels
Luxon timezone set to: Europe/Brussels
Previously stored events posted to sensor(s) at: Mon Apr 24 2023 10:54:18 GMT+0200 (Central European Summer Time)
postEventsAllCalendars error: TypeError: Cannot read properties of undefined (reading 'length')
postEventsAllCalendars error: TypeError: Cannot read properties of undefined (reading 'length')
postEventsAllCalendars error: TypeError: Cannot read properties of undefined (reading 'length')
postEventsAllCalendars error: TypeError: Cannot read properties of undefined (reading 'length')
Calendar(s) queried at: Mon Apr 24 2023 11:30:01 GMT+0200 (Central European Summer Time)
postEventsAllCalendars error: TypeError: Cannot read properties of undefined (reading 'length')
postEventsAllCalendars error: TypeError: Cannot read properties of undefined (reading 'length')
postEventsAllCalendars error: TypeError: Cannot read properties of undefined (reading 'length')
postEventsAllCalendars error: TypeError: Cannot read properties of undefined (reading 'length')
Events posted to sensor(s) at: Mon Apr 24 2023 11:30:01 GMT+0200 (Central European Summer Time)
axios error: [object Object]Request failed with status code 401
axios error: [object Object]Request failed with status code 401
axios error: [object Object]Request failed with status code 401
axios error: [object Object]Request failed with status code 401

Hello everyone,

For me it works now.

But the “caldavUrl:” setting should not be
https://pXX-caldav.icloud.com/{DSid}/calendars/{pGUID}” or
https://pXX-caldav.icloud.com/{DSid}/calendars/{calendarName}
but
https://pXX-caldav.icloud.com/{DSid}/calendars/{GUID}”.

This way I receive all data very well.

However, if I
https://pXX-caldav.icloud.com/{DSid}/calendars/{pGUID} or
https://pXX-caldav.icloud.com/{DSid}/calendars/{calendarName}
I keep getting
´´´´
axios error: [object Object]Request failed with status code 404
´´´´
error message.

At least this way it works for me!!

Thank you to all the people who gave me a little feedback!!

I am trying to use your code and I am struggling to have the sensor populate with anything. On restart, I get the agenda sensor but its empty. Following the RESTful documentation, I used the same details and used cURL and that returned calendar events. Here is what I I have in my config

  - platform: rest
    resource: https://myurl.com/api/calendars/calendar.calendar_me
    name: Agenda
    unique_id: calendar_agenda
    method: GET
    scan_interval: 900
    headers:
      authorization: !secret rest_events_token
    params:
      start: >
        {{ utcnow().strftime('%Y-%m-%dT%H:%M:%S.000Z') }}
      end: >
        {{ (utcnow() + timedelta(days=5)).strftime('%Y-%m-%dT%H:%M:%S.000Z') }}
    value_template: >
      {% set events = value_json %}
      {% for event in events %}
        {% if event.start.dateTime %}
          {{ (event.start.dateTime | as_datetime).strftime('%Y-%m-%d %H:%M') }}: {{ event.summary }}
        {% else %}
          {{ event.start.date }}: {{ event.summary }}
        {% endif %}
      {% endfor %}

I have a calendar and it has events in the last 5 days and next 5 days. I’ve set up a long-term token in my secret file, I’ve checked the logs and there is nothing in there on this.
I used this command from the docs (modifying for my instance)

curl \
  -H "Authorization: Bearer TOKEN" \
  -H "Content-Type: application/json" \
  "https://myurl.com/api/calendars/calendar.calendar_me?start=2022-05-01T07:00:00.000Z&end=2022-06-12T07:00:00.000Z"

and it returns events.

Have I missed a vital step? I’d really appreciate a steer.

Thanks in advance.

This looks very similar. I’m not sure what the problem is from spot checking, but maybe I can suggest a couple things.

  • turn up debug logging for the rest component homeassistant.components.rest and see if there are any details in the logs about failed requests
  • make the template return “foo” to confirm its running
  • set the return value to " {{ value_json }} " and you can dump the whole thing and make sure that is working

Perfect - thanks for the pointer

Seems we finally got something to ask for data using in automation. In our case calendar data. See 2023.7: Beta release notes - Home Assistant

Yep! This is meant to address this problem, but in the works for awhile now and happy to get it out!

1 Like

Good job Allen. Thanks!

  • calendar.list_events - This service allows you to ask Home Assistant for a list of events on your calendar.

Yes! I scrolled all the way through this post hoping for an answer, and it’s just a few days from release! Very happy to see this added. :slightly_smiling_face:

Is there a way of merging responses from multiple calendars ?

You’ll have to send separate calls for each calendar. Perhaps you can merge the events lists? I have not tried this

I made this script, for german language, using the new calender.list_events feature, maybe it helps as a starting point for other people.
@ allenporter thx for this new feature. Unfortunately your featured Blueprint did not work correctly.

Start-End: {% if event.start is defined %}{{ event.start }} to {{ event.end }}{% else %}All Day{% endif %}

did not work, because event start and end is always defined, at least for google calendar. My workaround was to check whether an event lasts exactly 86400 seconds = 1 day, in this case it’s an “All Day” event.

alias: 24h_termine_NestTTS_de
sequence:
  - service: calendar.list_events
    data:
      duration:
        hours: 25
        minutes: 0
        seconds: 0
    target:
      entity_id: calendar.your_calendar
    response_variable: agenda
  - service: tts.google_translate_say
    data:
      entity_id: media_player.nest_room1, media_player.nest_mini_room2
      language: "de"
      message: >-
        {% set tage =
        ["Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag","Sonntag"]
        %} {% set months = ["Januar", "Februar", "März", "April", "Mai", "Juni",
        "Juli", "August", "September", "Oktober", "November", "Dezember"] %}

        {%- if agenda.events %}
          {%- for event in agenda.events %}
            {% if event.start is defined %}
              {% if (((as_timestamp(event.start)) - (as_timestamp(now())) < 86400) and ((as_timestamp(event.start)) - (as_timestamp(now())) > 0)) %}     
                Am {{ tage[as_datetime(event.start).weekday()] }}
                den {{ as_timestamp(event.start) | timestamp_custom ('%d/%m/%Y') }}
                  {% if (as_timestamp(event.end)) - (as_timestamp(event.start)) == 86400 %}
                    ganztägig
                  {% else %}
                    um {{ as_timestamp(event.start) | timestamp_custom ('%H:%M %p') }} bis {{ as_timestamp(event.end) | timestamp_custom ('%H:%M %p') }}
                  {% endif %}
                ist {{ event.summary }}
                  {%- if event.description is defined %}
                    Hinweise, {{ event.description }}
                  {% endif -%}
                  {%- if event.location is defined %}
                    Ort, {{ event.location }}
                  {% endif -%}
              {% endif -%}  
            {% endif %}
          {%- endfor %}
        {% endif -%}

OT: Feature Request to HA:
Please make strftime respect locale or integrate babel

1 Like

If you’re interested, you can simplify the template’s arithmetic if you convert the values of event.start and event.end to datetime objects.

{%- if agenda.events %}
  {%- for event in agenda.events if event.start is defined -%}
    {%- set start = event.start | as_datetime | as_local -%}
    {%- set end = event.end | as_datetime | as_local -%}
    {%- if start > now() and start - now() < timedelta(days=1) %}     
      Am {{ tage[start.weekday()] }} den {{ start.strftime('%d/%m/%Y') }}
      {%- if end - start == timedelta(days=1) %} ganztägig
      {%- else %} um {{ start.strftime('%H:%M %p') }} bis {{ end.strftime('%H:%M %p') }}
      {%- endif %} ist {{ event.summary }}
      {%- if event.description is defined %}
      Hinweise, {{ event.description }}
      {%- endif -%}
      {%- if event.location is defined %}
      Ort, {{ event.location }}
      {% endif -%}
    {% endif -%}  
  {%- endfor %}
{%- endif -%}
1 Like

I’m really at a loss for proper documentation, of how HA jinja2 works, seems very stupid to me, why not normal javascript. Whatever.

Start here: