Send notifcation for google calendar events (today / tomorrow)

Hello,

I need some help with my calendar automation. I will send a notifications to my phone when:

  • tomorrow is an event
  • today is an event

Usually, there is only one event per day. I am able to send notifications to my phone, but I have no idea how I can

The following code sends me a notfication, but only today’s event:

  • id: notify-me
    alias: Example Notfication
    trigger:
    • platform: template
      value_template: ‘{{ states.sensor.date.state == strptime(states.calendar.mycalendar.attributes.start_time, “%Y-%m-%d %H:%M:%S”).strftime("%Y-%m-%d")}}’
    • platform: time
      at: ‘20:00:00’
      action:
      service: notify.notification
      data:
      message: “Tomorrow: {{ states.calendar.mycalendar.attributes.message}}”

No one an idea how to send a notification when a google calendar event is today / next day?

does it give a date and time? What does it look like in the states page?

I found a solution, which sends me a notification when a specific event is tomorrow.
I am going to add my code later.

this is what I have which uses Google Calendar events as the triggers. It might be of some help to you.

#   Package to track bin days based on Google Calendar entries and perform reminders

input_boolean:
  bin_reminder:
    name: Bin Reminder
    icon: mdi:delete-outline
    initial: on

#  https://community.home-assistant.io/t/need-template-assistance-please/80237/7

sensor:
  - platform: template
    sensors:
      recycling_bin_tomorrow:
        entity_id:
          - calendar.recycling_bin_day
          - sensor.time
        friendly_name: "Recycling Bin Tomorrow"
        value_template: >
          {% set e = strptime(
               states.calendar.recycling_bin_day.attributes.start_time,
               '%Y-%m-%d %H:%M:%S') %}
          {{ e.strftime('%j')|int - now().strftime('%j')|int == 1 and now().hour >= 12 }}
  
  - platform: template
    sensors:
      green_waste_bin_tomorrow:
        entity_id:
          - calendar.green_waste_bin_day
          - sensor.time
        friendly_name: "Green Waste Bin Tomorrow"
        value_template: >
          {% set e = strptime(
               states.calendar.green_waste_bin_day.attributes.start_time,
               '%Y-%m-%d %H:%M:%S') %}
          {{ e.strftime('%j')|int - now().strftime('%j')|int == 1 and now().hour >= 12 }}

  - platform: template
    sensors:
      recycling_bin_today:
        friendly_name: "Recycling Bin Today"
        value_template: "{{ is_state('calendar.recycling_bin_day', 'on') }}"

  - platform: template
    sensors:
      green_waste_bin_today:
        friendly_name: "Green Waste Bin Today"
        value_template: "{{ is_state('calendar.green_waste_bin_day', 'on') }}"


automation:
  - alias: Bin Reminder TTS
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.recycling_bin_tomorrow
        to: 'True'
      - platform: state
        entity_id: sensor.green_waste_bin_tomorrow
        to: 'True'
      - platform: state
        entity_id: sensor.recycling_bin_today
        to: 'True'
      - platform: state
        entity_id: sensor.green_waste_bin_today
        to: 'True'
    condition:
      - condition: state
        entity_id: input_boolean.bin_reminder
        state: 'on'
    action:
      - wait_template: "{{ is_state('binary_sensor.kitchen_multi_sensor_sensor', 'on') }}"
        timeout: '10:00:00'
        continue_on_timeout: 'false'
      - wait_template: "{{ is_state('media_player.googlehome3019', 'off' or 'idle') }}"
      - service: tts.google_say
        entity_id: media_player.googlehome3019
        data_template:
          message: "Dont forget you need to put out the {{ trigger.to_state.attributes.friendly_name }}"

  - alias: Reset Bin Reminder
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.recycling_bin_today
        to: 'True'
        for:
          hours: 24
    action:
      - service: homeassistant.turn_on
        entity_id: input_boolean.bin_reminder
1 Like

I have the following automation which is partly working.

It sends a message the day before the trash event starts. So if tomorrow is an google calendar event, I receive a notification via telegram, that tomorrow the trash will be collected. But it also sends a notification on the trash day at 18 o clock?
Whats wrong with my code?

  • id: garbage-notify-1
    alias: ‘Yellow Bin Notfication’
    trigger:
    platform: time
    at: ‘18:00:00’
    condition:
    • condition: template
      value_template: >-
      {%- if (as_timestamp(now()) > (as_timestamp(states.calendar.trash.attributes.start_time) - 86400)) -%}
      true
      {%- endif -%}
    • condition: template
      value_template: >-
      {{ states.calendar.awigo.attributes.message == ‘yellow bin’ }}
      action:
    • service: notify.telegram
      data:
      message: ‘INFO: Tomorrow the yellow bin will be collected!’

Anyone an idea how to solve this? I only want to send a notification if tomorrow is a specific event…see my last post.

@sparkydave I already tried to use your code but I observed a weird behaviour. I getting notification for events which are not today / tomorrow. Do you have a working example with the whole code?

If you want to use a specific event from the calendar you need to ensure the google_calendars.yaml data is correctly set up to look for it.

google_calendars.yaml

- cal_id: [email protected]
  entities:
  - device_id: recycling_bin_day
    ignore_availability: true
    name: Recycling Bin Day
    track: true
    search: "recycling bin"
    offset: "!!"

  - device_id: green_waste_bin_day
    ignore_availability: true
    name: Green Waste Bin Day
    track: true
    search: "green waste bin"
    offset: "!!"

  - device_id: rent_inspection
    ignore_availability: true
    name: Rent Inspection
    track: true
    search: "rent inspection"
    offset: "!!"

bin_reminder.yaml (package)

#   Package to track bin days based on Google Calendar entries and perform reminders

input_boolean:
  bin_reminder:
    name: Bin Reminder
    icon: mdi:delete-outline
    initial: on

#  https://community.home-assistant.io/t/need-template-assistance-please/80237/7

sensor:
  - platform: template
    sensors:
      recycling_bin_tomorrow:
        entity_id:
          - calendar.recycling_bin_day
          - sensor.time
        friendly_name: "Recycling Bin Tomorrow"
        value_template: >
          {% set e = strptime(
               states.calendar.recycling_bin_day.attributes.start_time,
               '%Y-%m-%d %H:%M:%S') %}
          {{ e.strftime('%j')|int - now().strftime('%j')|int == 1 and now().hour >= 12 }}
  
  - platform: template
    sensors:
      green_waste_bin_tomorrow:
        entity_id:
          - calendar.green_waste_bin_day
          - sensor.time
        friendly_name: "Green Waste Bin Tomorrow"
        value_template: >
          {% set e = strptime(
               states.calendar.green_waste_bin_day.attributes.start_time,
               '%Y-%m-%d %H:%M:%S') %}
          {{ e.strftime('%j')|int - now().strftime('%j')|int == 1 and now().hour >= 12 }}

  - platform: template
    sensors:
      recycling_bin_today:
        friendly_name: "Recycling Bin Today"
        value_template: "{{ is_state('calendar.recycling_bin_day', 'on') }}"

  - platform: template
    sensors:
      green_waste_bin_today:
        friendly_name: "Green Waste Bin Today"
        value_template: "{{ is_state('calendar.green_waste_bin_day', 'on') }}"


automation:
  - alias: Bin Reminder TTS
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.recycling_bin_tomorrow
        to: 'True'
      - platform: state
        entity_id: sensor.green_waste_bin_tomorrow
        to: 'True'
      - platform: state
        entity_id: sensor.recycling_bin_today
        to: 'True'
      - platform: state
        entity_id: sensor.green_waste_bin_today
        to: 'True'
    condition:
      - condition: state
        entity_id: input_boolean.bin_reminder
        state: 'on'
    action:
      - wait_template: "{{ is_state('binary_sensor.kitchen_multi_sensor_sensor', 'on') }}"
        timeout: '10:00:00'
        continue_on_timeout: 'false'
      - wait_template: "{{ is_state('media_player.googlehome3019', 'off' or 'idle') }}"
      - service: tts.google_say
        entity_id: media_player.googlehome3019
        data_template:
          message: "Dont forget you need to put out the {{ trigger.to_state.attributes.friendly_name }}"

The main reason I have those templates in there is to create a window of time where the sensor: true because I didn’t just want to know if the calendar entry was tomorrow, but only be true in the evening of the day before. You can play with / remove that function however you want.

Once you have a sensor: true, just use that as a trigger for your notification. I only use it for a conditional Lovelace card and the TTS.

You need to format your code as per the instructions at the top of the page otherwise we can’t tell if your indentation is correct or not.

Thanks, I am going to give your code a second chance.

Your automation “Reset Bin Reminder” is missing in your last post, is that not needed? If needed, is there no reset for the green_waste_bin_today?

I didn’t include it this time because it isn’t needed. That was something I coded in case I was to tell Google to turn off the reminder one day, but the way my wait_template works in the automation, I only get the TTS once anyway. I could just delete it since its actually pointless being there. It was just that I had a thought to remove the wait_template and have the automation fire every time I went into the kitchen until I turned off the reminder input_boolean, but I didn’t go that route in the end.

Ok. So I can replace the following part:

action:
  - wait_template: "{{ is_state('binary_sensor.kitchen_multi_sensor_sensor', 'on') }}"
    timeout: '10:00:00'
    continue_on_timeout: 'false'
  - wait_template: "{{ is_state('media_player.googlehome3019', 'off' or 'idle') }}"
  - service: tts.google_say
    entity_id: media_player.googlehome3019
    data_template:
      message: "Dont forget you need to put out the {{ trigger.to_state.attributes.friendly_name }}"

with:

action 
  - service: notify.telegram
    data_template:
      message: "{{ trigger.to_state.attributes.friendly_name }}"

Yep. As long as the triggers relate to your calendar entries, all should work

@sparkydave
Seems to be working - but when I use {{ trigger.to_state.attributes.friendly_name }} in the data_template, no message will be send.

EDIT:
Ok, it seems that data_template is not supported for telegram notifications. Also, when I use only “data” instead of “data_template”, the code in the brackets doesnt work…

The bracket stuff IS the template so HA requires you to use data_template rather than just data. Are you able to try another notification platform in place of telegram for testing purposes? I wasn’t aware of that restriction with Telegram, which I hope can be worked around because I was considering moving my Pushbullet stuff over to Telegram…

I tried it with Pushbullet and its working.

When I use telegram with another variable like {{ states.sensor.dark_sky_temperature.state }}, its working fine.
So there is a problem with {{ trigger.to_state.attributes.friendly_name }}

I suddenly remembered why I put the “Reset Bin Reminder” automation in my code… it’s because I have the TTS set to give a reminder the day before AND on the day of bin collection, however if I hear the TTS the day before and go and put the bins out then I dont want the TTS the next day, so I turn OFF the bin reminder input_boolean (using voice with Google Assistant), then the input_boolean will get reset later on the collection night, ready for the next week of TTS announcements. The reason I forgot about that was because I have only just coded this and haven’t yet actually bothered to ask Google to turn off the reminder… I’ve just been letting it run the TTS both days. I need to get in the habit of doing so if I put the bins out the day before, which is very common for me.

Has anyone worked out how to find a calendar event which does NOT match a string ?
I want to find the next event ignoring ones with a certain phrase in.

post the entities and their attributes and I can help.

@petro thanks for responding. Look up google calendar integration. When setting up the calendar settings there is one called ‘search’ which, at the simple level, will cause it to only include events which contain a string. But there is an indication that it uses regex. I tried ^((?!Airbnb).)*$ to exclude items with Airbnb in to no avail.