Run custom python script

Hi guys,
i’ve come across this python script that can pull my google calendar events and write them to a text file.

I tested it on my windows pc and it worked, what I would like is to have this run in home assitant OS and then create a morning automation to have google read it out to me along with the a few other things.

it is possible to get home assitant OS to run this python script?

import requests
import sys
reload(sys)
sys.setdefaultencoding('utf8')
page = requests.get("https://calendar.google.com/calendar/htmlembed?mode=AGENDA&src=myemailadrress&ctz=uk/london")

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('sensor_scripts/todays_events.txt', 'w')
fo.write(output)

fo.close()

Did you try the google calendar integration yet?

1 Like

I did but it seems this is used more as a trigger. I’m wanting to have it read out to me every morning along with the todays weather ect

Off the top of my head, the way to do this would be to use a command line sensor, with the command line being to run the script and the sensor ‘state’ being the result.

Presuming that’s right, you’re going to hit a limitation that states have a maximum length, so I guess you’ll want the state to be today’s date and then get the script to return the data you want to be read out as an attribute (or a number of attributes).

Then your automation can be set to read the attribute(s) when it triggers.

1 Like

Thank you
Thinking down the road of command line could it been done with a switch, trigger once just past midnight.

I dont know the correct syntax I did a search and can’t seem to find anything

I’m not sure about the switch tbh, or even anything more helpful for the sensor.

I’d have to do the same googling as you to work it out. If you’re really struggling with Google-fu I can try and work something out for you over the weekend.

1 Like

If you could help me with this I would really appericate it.

No worries, I’ll hopefully get a chance to look at it properly tomorrow afternoon :+1:

1 Like