Templating a sensor for a specific Google Calendar event?

Hi
I had the old method of Google Calendar installed monitoring a scheduler calendar.

It was set up so that I had a calendar entity for each different typical event.
e.g. Heating, sleeping, days, nights, wfh, holiday etc…
Happy days

Today it broke and all my entity calendars suddenly switched “on” when only one should have been.

So now I’m in the process of trying to transfer all my automations and template sensors over to the integrated Google calendar method.

Automations I can do, but I’m stuck when trying to update my template sensors as well as some YAML I used in my dashboard as I can only select the my_schedule calendar rather than per description.

Previously a template sensor would be on/off with the following code:

- sensor:
    - name: house_is_sleeping
      icon: >
        mdi:sleep
      state: >
        {% if is_state('calendar.sleeping', 'on') %}
        on
        {% else %}
        off
        {% endif %}

Another Pre example, displaying when my heating is due to be active on on my dashboard

                {% if states('calendar.heating')=='on' %}
                {{as_timestamp(state_attr('calendar.heating','start_time')) |
                timestamp_custom('%H:%M')}} -
                {{as_timestamp(state_attr('calendar.heating','end_time')) |
                timestamp_custom('%H:%M')}}

Now I don’t know how to pull the “heating” info out for the dashboard, esp if there are other events happening beforehand :frowning:

After some very odd things like lights and heating and blinds all opening and closing at odd times I’ve only just realised that this has happened!

We were coming into a cold snap here so didn’t have the time to fiddle about with it. Needed my heating automations to work, so I gave up in the end and created a separate calendar for each schedule.

It’s a shame as it used to be great having everything neat in a single calendar.

This is fixed in a patch release. Was a bug, not an intentional removal.

Ta. I saw that it had been scheduled in for 2022.11.2

For my setup it’s too important to be vulnerable to bugs.
About 50% of my system is managed by the calendar(s) with manual overrides if needed.

It works as it is now, so I’ll leave it this way.
Piece of mind and all that :smiley:

Yeah the unfortunate thing was the feature that broke this was meant to make calendars more reliable.

So you are saying that each event is supposed to have their own sensor generated and that it is temporarily broken?

One of my instances is running an old version: 2021.7.4
WIth google configuration in configuration.yaml and not from the GUI
It still does not have a sensor for each event in one calendar. I know I have had this working before but not sure which version that was. Just started to try and get it up and running again.

When it worked I could access start time of the next event of a specific name like this:

states.calendar.event_name.attributes.start_time

So the sensors where based on the event name.

Now I have just calendar based sensor like:

states.calendar.calendar_name

And thus I have only access to the next upcoming event and none later

No event sensors generated in 2022.11.3 either with integrated Google Calendar service

Is there a config needed in order for event names to generate sensors?

I was talking about the search functionality that broke in 2022.11.0 and fixed in patch releases. I don’t recommend using the yaml search because of all the caveats, but it’s still there if you use google_calendars.yaml. I instead recommend using automation and a binary sensor template that matches the event start/event end events example though i want to make this a built in feature in the UI in the future.

Can you please give me some directions on how to get an event as a sensor itself?

The use case:

Calendar name: My calendar
Event at day 2: Vacation
Event at day 4: Swimming
Event at day 6: Trash

Let’s say we are at day 1
So for me this gives me a sensor available as below:

calendar.my_calendar

This sensor has only information about vacation on day 2 with start end time etc.

Is there a way that I can get a sensor populated as below:

calendar.trash

WIth info about this event with start, end etc.

Then I can create a template sensor with “Days to trash”

  - platform: template
    sensors:
      timetotrash:
        friendly_name: Days to Trash
        entity_id: sensor.time
        value_template: >-
          {% if states.calendar.trash.attributes.start_time  %}
            {{((as_timestamp(states.calendar.trash.attributes.start_time) - as_timestamp(states('sensor.date_time_iso')) ) / ( 60 * 60 * 24 ) ) | default(99) | round(2, 'common') }}
          {%- else -%}
            0
          {%- endif %}
# this sensor would now have the value 5 ( 6-1 )
sensor.timetotrash

I have had this available before but it is a while so I have no idea which version it was working in. Each event name in one calendar had a sensor populated by home assistant.

I have managed to check next available event if it is what I’m looking for but would like to be able to access data for events that are not the next in the calendar.

I’m sorry if I have missed some info you provided that would solve this.

Thanks!

At the moment you can only do this using the google_calendars.yaml described at Google Calendar - Home Assistant under More configuration.

However, what I do is have a binary sensor for “Tomorrow”. I don’t care how many days it is until trash, just when it’s tomorrow:

- trigger:
    - platform: calendar
      event: start
      entity_id: calendar.device_automation_schedules
      offset: "-24:00:00"
    - platform: calendar
      event: end
      entity_id: calendar.device_automation_schedules
      offset: "-24:00:00"
  binary_sensor:
    - name: Trash Pickup Tomorrow
      state: "{{ iif('Trash Pickup' in trigger.calendar_event.summary, iif(trigger.event == 'start', 'on', 'off'), states('binary_sensor.trash_pickup_tomorrow')) }}"

I managed to add the event sensors by modifying the google_calendars.yml

# before
- cal_id: [email protected]
  entities:
  - device_id: my_calendar
    ignore_availability: true
    name: My Calendar
    track: true
# after
- cal_id: [email protected]
  entities:
  - device_id: my_calendar
    ignore_availability: true
    name: My Calendar
    track: true
  - device_id: custom_cal_sensor_a
    ignore_availability: true
    name: Custom Sensor A
    track: true
    search: 'Event Name A'
  - device_id: custom_cal_sensor_b
    ignore_availability: true
    name: Custom Sensor B
    track: true
    search: 'Event Name B'

After this I ended up with two new sensors for next matching event with specified name in search

calendar.custom_cal_sensor_a
calendar.custom_cal_sensor_b

They have all the info about the event, start, end, name etc.

So this is exactly what I was looking for.
I guess this is how I did it last time and lost the modified config file and just got the generated without additional config somewhere along the way.

Thank you very much!

PS. What is the iif condition? Never seen.

That’s how I had mine. I’d followed SlackerLabs tutorial and it worked fine for me up until 2022.11.0

It would be great to merge things back to a single scheduler calendar again, but I’m hesitant to re-activate the google_calendars.yaml file just in case…