Is there any way to access more than one event on a Google Calendar?

I’m working on a TTS script to run in the morning after my alarm, and I want it to summarize all of the events I have for that day. Currently it seems like HA can only access the next event in my calendar, but not any events after that.

Is there any way to access future events after the current next event in a calendar in HA?

1 Like

Also worth noting that from my experience if you have overlapping events and you’re still in the time frame for the first one, HA still reads the first one rather than the ‘next’ one.

Example:

You have an entry for you receiving a call that could come anytime between 10.00 and 12.00

You have to pick the kids up from swimming at 11, you mark it as 1100 - 11.30

You have a meeting at work from 16.00 - 18.00

Prior to 10 HA will show next event as incoming call. This will stay the case until 12, then next event will then show as meeting at 4pm.

HA will not pick up the swimming entry.

1 Like

Seeing as it seems that HA doesn’t have this capability, I came up with a workaround. I shared my google calendar publicly so that I could access it via a webscraper.

This python script scrapes the relevant calendar information for my day from my calendar in the agenda format, creates a sentence that summarizes my day and saves that sentence into a txt file. With cron, I have this run at 1am everyday so it is updated when I wake up.

import requests
page = requests.get("https://calendar.google.com/calendar/htmlembed?mode=AGENDA&[email protected]&ctz=America/New_York")
from bs4 import BeautifulSoup
soup = BeautifulSoup(page.content, 'html.parser')
table = soup.select('div.date-section-today tr')
rows = [tb.get_text() for tb in table]
output=""
wholedaycount = 0
partialdaycount=0
for x in rows:
	temp = x.split("\n")
	if len(temp[0])>1:
		if wholedaycount>0 and partialdaycount==0:
			output+=". "
		if partialdaycount==0:
			output+="At "+temp[0]+" you have "+temp[1]
			partialdaycount=1
		else:
			output+=", at "+temp[0]+" you have "+temp[1]
	else:
		if wholedaycount==0:
			output+="Today you have "+temp[1]
			wholedaycount=1
		else:
			 output+=" and "+temp[1]
output+="."
fo = open('/home/homeassistant/.homeassistant/todays_events.txt', 'w')
fo.write(output)
fo.close()

In my config I added a file sensor that takes the contents of the file and makes it the state for the sensor.

> # Google Calendar Scraper
>sensor:
>     - platform: file
>       file_path: "/home/homeassistant/.homeassistant/todays_events.txt"
>       name: Today's Events

And I added a TTS script to the config to read off the state of the file_sensor (the day’s summary).

> script:
>   - service: tts.google_say
>     data_template:
>       entity_id: media_player.lab_chromecast_audio
>       message: {{states.sensor.todays_events.state}}

This successfully reads out the summary of my day through my chromecast audio

2 Likes

Hi @Jon_Ross perfect work around.

I wanted to find out how did you get your python script running ? Did you create a custom component ?

I am trying to scrape info from a website but cannot get it to display on HA as library import (bs4 in my case) is not permissible with python in HA. How did you manage to do that?

I will probably use a similar script to yours but the information I require is in a tabulated format from which I would need to create at least 5 sensors. Can you help me with that please?

Thanks.

I had the python script running on my pi completely independent of HA. I just set it up with cron so that the file would run once a day and create the text file that HA actually looks at

oh right I see.

so in your python code what is the part which saves the sentence in the text file?

I have something like:

name_box = soup.find('div', attrs={'id':'ja-user2'})
name = name_box.text.strip() 

and what I am trying to do is to save the output of “name” in the text file.

This is the relevant piece of code for writing the string to the file. I think you should use the whole path to the file and place it within the homeassistant folders so that HA can access it.

fo = open('/home/homeassistant/.homeassistant/todays_events.txt', 'w')
fo.write(output)
fo.close()
1 Like

hi @Jon_Ross

could you please help, i really want to get this set up but no matter where i put the file hassio doesn’t like it

I never got it working with Hassio, as I don’t think it would allow me to rund scripts on the pi independent of Hassio. I did this with just the normal Home Assistant configuration

im running the python script from my htpc and saving it in the config of hassio

yet the sensor file location doesn’t work

Is it giving you any kind of error code in hass? Maybe check to make sure the content is in the file and the file location put in the config.yaml is the correct whole path to the file

- platform: file
  name: Todays Events
  file_path: /root/share/todays_events.txt

i get

Failed config
  sensor.file:
    - Invalid config for [sensor.file]: not a file for dictionary value @ data['                                                                             file_path']. Got '/root/share/todays_events.txt'. (See ?, line ?). Please check                                                                              the docs at https://home-assistant.io/components/sensor.file/
    - platform: file
      file_path: /root/share/todays_events.txt
      name: Todays Events

It would be really great if we could just do it with the help of Home Assistant.
Unfortunately, I also belong to the rather forgetful people. :smiley:
I would like to send myself a bundled message (once or several times a day) about all upcoming dates on that day.
As far as I could read up, this is not possible with the current google calendar component, until now. :frowning:

hi

what do i need to change in the python, so i can see all upcomong events, not only today
but maybe like an variable: 7 days or something?

thnx

The events pulled by the Google Calendar integration is available under: <hass-url>/calendar.<calendar_id> I think it is better to access that information by a restful sensor or some other magics.

I’ll report back when I’ve created the needed configuration.

I use https://github.com/atomic7777/atomic_calendar card and can view 7 days of events on multiple Google Calendars. Seems to work quite well.

It’s available in HACS

yes, but thats card, we want it to be visible in a sensor, so we can create our own cards/sensors/… :slight_smile:

You said in your post that you wanted to “see” the events.

…if you want something else, look at the code and alter the method to suite. That’s what HA is all about.

yeah i know, but i am not a programmer :slight_smile:

Anyone get anywhere with this??