How to use Google Calendars' event notification as a trigger and then reference the event in your Actions like you would if you directly referenced it?

So, if I understand the Google Calendar- and the HA Calendar documentation correctly, there’s only a straightforward way to do automations based on when an event starts/ends, but not when its’ notifications trigger, no?

I’m asking because I want to mirror those notifications to see them on all devices as soon as they’re created (at the point of the appointment is obviously gonna be too late)
and I don’t know how that’s supposed to be possible if the API doesn’t directly include that functionality.

Currently I’m mirroring it like this:

  trigger:
  - platform: calendar
    event: start
    entity_id: calendar.ars4l4n123_gmail_com
  condition: []
  action:
  - service: notify.pushover
    data:
      message: '{{ trigger.calendar_event.summary }} {{ (trigger.calendar_event.start
        | as_datetime).strftime(''%d.%m.%Y, %H:%M'') }}'

so the action references (right terminology?) the calendar event in this example but I suppoose it would have to be done differently if the trigger was the events’ notification rather than the event itself, wouldn’t it?

There are multiple ways to make this work, depending on how you have everything set up, but it is not part of HA’s Calendar integration.

If your calendar is sending standard notifications, you can use the HA companion app’s Last Notification sensor. You will need to make sure the calendar app on your device is in the Allow list for the sensor.

If your calendar is sending email notification, you will need to set up an IMAP sensor and Content sensor to trigger your automation.

1 Like

okay but how would you get the event title and date/time into the pushover notification? Could you post a code example?

This can vary based on the calendar application running on your phone… not all phone manufacturers install the base calendar app package. If your device uses a different calendar app package you will need to figure out which attributes you need to use.

alias: Relay Google Calendar Notification Pushover
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.YOUR_DEVICE_last_notification
condition:
  - condition: template
    value_template: |
      {{ trigger.to_state.attributes.package == 'com.google.android.calendar' and
      trigger.to_state.attributes.channel_id == 'REMINDERS' }}
action:
  - service: notify.pushover
    data:
      message: |-
       {{ trigger.to_state.state }}
       {{ trigger.to_state.attributes['android.title'] }}
mode: queued
1 Like

Can you achieve what you want by using the calendar trigger offset? That is, the automation can fire X minute before the event. You can’t do it dynamically based on the exact notification offset time in google calendar, but it its fixed (e.g. “5 minutes before event start”) before all events it will work. I assume you may have already thought about this, but just wanted to clarify the offset feature in case that was missed.

hi, nice effort

You will need to make sure the calendar app on your device is in the Allow list for the sensor.

how do I do that? The documentation I found didn’t tell me how exactly to set it up.

I suppose it’s not normal that I don’t see a sensor.Galaxy_S7_last_notification even though I have the companion app for that device

Also, When I entered your template as a condition it gave me the error “Message malformed: template value should be a string for dictionary value @ data[‘action’][0][‘data’]”
What’s going on with that?

Not all sensors are enabled automatically, you enable them in the Settings menu.

In the mobile app:

Setttings > Companion App > Manage Sensors > Last notification > Allow list (at the bottom of the page)

Make sure there is a check in the box next to your calendar app, then save the list. Samsung is kind of notorious for using non-standard app packages, so you may need to do some research to figure out which one you need to enable.

You will likely also need to change the values in the Condition template since other apps often use different variable names than those used by the base Android/Google applications.

That error is regarding the value for data: in the notify action. You likely have an indentation or quotation error somewhere… There’s no way for us to tell without seeing your configuration.

1 Like

okay, great, I set the companion app up now

you’re right
when I pasted it into the automation UI it added some additional quotes to where your quotes were

there’s another thing, upon testing the condition it says:

"Error occurred while testing condition

template value should be a string for dictionary value @ data[‘value_template’]. Got None"


I tested the automation and it only worked 1 out of 6 times for some reason.
Asides from that, how can I check what a notfications’ channel ID is called?

The tester does not work with Template conditions… especially in this case since there is no trigger event to populate the trigger variable used by this template.

You will have to go to the Developer Tools > States tool and check the attributes of the last notification sensor immediately after a calendar notification is received.

1 Like

@Didgeridrew your code seems to run without any issue for some weeks.
I was wondering if there’s a way to extend the functionality even further to make it act like an actual calendar notification, meaning that when it’s tapped it opens the event from that notification in the calendar app

I don’t use Pushover, so you’ll need to do a bit of experimenting…

For the HA companion app and Android devices, I would add a click action with an Android intent, but Pushover only seems to allow opening a url. The intent url may work if all your target devices are Android. Try the following:

  - service: notify.pushover
    data:
      message: |-
       {{ trigger.to_state.state }}
       {{ trigger.to_state.attributes['android.title'] }}
      url: 'intent://www.google.com/calendar/event#Intent;scheme=https;package=com.google.android.calendar;end'

If any of your targets are iOS, your best option is probably to just add the public url of the calendar.

hm upon changing the action to the version you presented the Pushover notifications for this Automation don’t fire anymore at all
And I’m pretty sure I added the code correctly this time

  action:
  - service: notify.pushover
    data:
      message: |-
       {{ trigger.to_state.state }}
       {{ trigger.to_state.attributes['android.title'] }}
      url: 'intent://www.google.com/calendar/event#Intent;scheme=https;package=com.google.android.calendar;end'

Considering one of my devices has iOS I tried just adding the URL in this simpified action:

  action:
  - service: notify.pushover
    data:
      message: |-
      test
      url: 'https://calendar.google.com/calendar/u/0?cid=YXJzNGw0bjEyM0BnbWFpbC5jb20'

but even that threw an error:
“Error running action
Error rendering data template: Result is not a Dictionary”

I also tried the variant that was in the Pushover documentation:

  action:
  - service: notify.pushover
    data:
      message: test
      data:
        url: https://calendar.google.com/calendar/u/0?cid=YXJzNGw0bjEyM0BnbWFpbC5jb20

which on iOS upon tapping the notification only causes the pushbullet app to open with the link there for me to tap again to open google calendar. I was hoping for the page to pop up from just tapping the notification though, just like how the actual calendar app would behave. Additionally, it opens the Google Calendar Website thereby clogging my tabs, not the Google Calendar App.

Lastly, I tried this variant:

service: notify.pushover
data:
  title: Kalender
  message: |-
    {{ trigger.to_state.state }}
    {{ trigger.to_state.attributes['android.title'] }}
  data:
    url: 'intent://www.google.com/calendar/event#Intent;scheme=https;package=com.google.android.calendar;end'

but my iOS device wasn’t able to open the link.

I didn’t find comprehensive documentation, nor actual package names, just some vague userposts so I also tried google calendar://calendar.google.com/calendar/u/0?cid=YXJzNGw0bjEyM0BnbWFpbC5jb20 and google_calendar://calendar.google.com/calendar/u/0?cid=YXJzNGw0bjEyM0BnbWFpbC5jb20 for the last line with no success

any further suggestions?

iOS stuff is outside my wheelhouse… have you looked into setting up a Siri Shortcut to launch the Calendar. You can fire Shortcuts via HA service call with additional data, so that might be an avenue to explore.

no, I’ll try it out
hm, seems like an app can’t be launched via siri shortcuts if they’re not listed there
I’d have to contact the developer about it or so

@Didgeridrew
I’ve been noticing a issue recently which I can’t exactly pinpoint because it doesn’t occur consistently
I think it’s probably something in my Galaxy S6s’ tasker configuration (because upon disabling all Profiles the issue disappeared) but everything is set up correctly so it must be a bug

The issue is that sometimes the Pushover notification just gets immediately canceled

Do you have any suggestions or know a way to set this calendar up which is less prone to bugs?

Update: I just quit using the Galaxy S6 and used a Pixel 3 + Galaxy S7 and iPhone 12 mini instead and the issue went away.

I noticed a new issue with @Didgeridrew 's automation: When I set two Calendar notifications at the same time only one of them triggers the automation. So, the other one isn’t mirrored.
It’s not even in Home Assistant’s History page.

The issue occurs on my Galaxy S7 and Pixel 3.

Make sure the automation mode is set to queued or parallel.

that didn’t do anything and I would’ve been surprised if it did since it’s not the automation not firing but actually the value not being reported by the HA Companion App.

@Didgeridrew
I actually found the reason for this happening and a workaround.
So, on Github, a guy working on HA says this seems to happen because the notifications are so close to one another that by the time we are able to send the data over to HA the database was already updated.

He suggests to use the Active Notification Count sensor as a workaround as that contains the notification title as an attribute.
So, I tried redoing the automation with that sensor, but I got stuck because for this sensor the attributes are different in that each notification has an individual number on the left side of the colon
chrome_2024-07-02_21-35-19

and I don’t know how to make templates for that. Would you help me out with that?