Count events and tasks in local calendar

Hi all.

first of all, please be gently with me - cause im pretty new to this HA

How do i count and display number of events in local calendar in a template

can someone please help me with this?
I just want to display a number on how many events and tasks for today…
So i can use it in a notification on my mobile.
“You have 3 events and 2 task in your calendar today”…

i have search the web, but can’t find anything that does this for me…

best regards…

There’s no easy way to get this information into a template sensor currently. You can get this information in a notification without a template sensor.

calendar_events_today:
  alias: Calendar Events Today
  sequence:

  - service: calendar.list_events
    target:
      entity_id: "{{ calendar }}"
    data:
      start_date_time: "{{ today_at() }}"
      end_date_time: "{{ today_at() + timedelta(days=1) }}"
    response_variable: agenda

  - service: notify.your_mobile_device
    data:
      message: You have {{ agenda.events | count }} events in {{ state_attr(calendar, 'friendly_name') }} today

Then you can use this script anywhere

service: script.calendar_events_today
data:
  calendar: calendar.abc

Thank you so much, Mister.

It works like a charm :slight_smile:
Once again, thank you :slight_smile:

Apologies for bringing up an old thread, but I’m trying to do the same, but I’m struggling with the first block of code… is that an automation or a script?
I’ve added the code to both of those but when I run the second block of code as a script, I keep getting “UndefinedError: ‘calander’ is undefined”.
I’ve also tried to rename instances of calander to my local calander entity, but with the same ressult. :frowning:
I’d appreciate any additional guidance that you could provide.
Thanks in advance!

Script

The word calendar was misspelled as “calander”.

Thanks Taras for the response - got it working!

Hello everyone,

Sorry to dig up an old topic, but when I try the code you gave @petro , it throws an error : Message malformed: extra keys not allowed @ data[‘calendar_events_today’]

I must be missing something, but I can’t tell what… any idea ?

You put it in the script section of your configuration.yaml.

Hi Petro, I am updating my automation at the moment which is close to 100% working, the only issue I have is trying to imbed a count events. I used the config in this thread which I presume should have worked however the example is a script not an automation, so does that mean that {{ agenda.events | count }} cannot be used in an automation, as it seems to not produce the expected result (always 0) even though the automation lists the events? The automation is below - dont take any notice of the time pattern only for testing.

alias: Garys Agenda test
description: ""
triggers:
  - seconds: "1"
    trigger: time_pattern
    enabled: false
conditions: []
actions:
  - action: calendar.get_events
    metadata: {}
    data:
      duration:
        hours: 24
        minutes: 0
        seconds: 0
    target:
      entity_id: calendar.wilkinson_business_gmail_com
    response_variable: garys_agenda_2
  - action: notify.homeassistant_gw_gmail_com
    data:
      title: Daily agenda for {{ now().date() }}
      target: [email protected]
      message: >-
        Gary; you have {{ garys_agenda_2.events | count }} events in your agenda:<BR>
        {% for event in
        garys_agenda_2["calendar.wilkinson_business_gmail_com"]["events"] %} 
        {%- set start = event.start | as_datetime | as_local -%} {{ ['Today',
        'Tomorrow'][(start.date() - now().date()).days] }}, at {{
        start.strftime("%I:%M %p") }}, {{ event.summary }}<BR> {% endfor %}
    enabled: true