Google Calendar Offset With Appdaemon

So with today being the first day of school and now the Mrs. has the kids school schedule all plugged into her google calendar. I have HA setup to pull in her calendar events. But of course I’m not going to ask her to tag her calendar events with !! in the name. So I was thinking since I need these to fire off ahead of the actual start time for the kids school (they’re silly TTS things to tell the kids its time to go). Instead since the start time is an attribute in the entity can I instead tell appdaemon to calculate a run_at time? Say set a variable equal to the start time of the event but then deduct X minutes from the start time of the event and have it trigger at that time? I wouldn’t be using the calendar entity in this case as a switch since I dont’ care about its on/off status only that its setup to run.
Thinking about this more then I would also need AD to fire off every morning prior to the start time to set the variable. Since the entity will change every day and we can’t put the variable in the initialize. Open to all suggestions here on how to accomplish this. I’ve seen where people do templates for this and what not but I don’t want to do a template for each calendar I need an offset on, I feel that AD is more than capable of doing this. Thanks for your help.

i think i can help with that.

i got 2 calendar apps.

  1. i imported a calendar for my wife that has all matches from the soccergame she loves and i let AD announce trough TTS 1 hour before, 1/2 hour before and on the startime, that the match is starting.
    to make sure that it happens on the right time i find the time and date 2,5 hours after each match and at initialise.

  2. i got all garbage collections in another calendar. but in that calendar it says: garbage is collected next day. that is an all day event. i let AD announce through TTS (and nowadays through Alexa) on that day in the calendar. all day long every hour on the hour untill an input_boolean is flipped that the garbage needs to go out.

so you see it is possible.

because of the fact that the second one uses the switch, thats not the one you want.
so ill post the relevant part from the first:

###########################################################################################
#                                                                                         #
#  Rene Tode ( [email protected] )                                                            #
#                                                                                         #
#  2017/09/04 Germany                                                                     #
#                                                                                         #
#                                                                                         #
#  an app to speak out and send notifies an hour before, a half hour before and           #
#  at starttime from a (soccer) game.                                                     #
#  the data is collected from an Google Calender                                          #
#                                                                                         #
###########################################################################################

import appdaemon.plugins.hass.hassapi as hass
import datetime
import time

import requests
from socket import timeout
from bs4 import BeautifulSoup

import threading


class calender(hass.Hass):

  def initialize(self):
    self.liveverslag = ""
    self.liveverslagactive = False
    self.laatstgelezenregel = 1
    runtime = datetime.time(12, 0, 0)
    self.timestamp = datetime.datetime.now()
    self._entity = "calendar.feyenoord"
    self.sound = self.get_app("soundfunctions")
    self.create_notifies(self)        

  def create_notifies(self, kwargs):
    game = self.get_state(self._entity,attribute = "message")
    teams = game.split("-")
    if "feyenoord" in teams[0] or "Feyenoord" in teams[0]:
      self.location = "een thuiswedstrijd in de kuip"
      self.opponent = teams[1]
    else:
      self.location = "een uitwedstrijd" 
      self.opponent = teams[0]
    start_time1 = self.get_state(self._entity,attribute = "start_time")
    if start_time1 == None:
      return
    actual_time = datetime.datetime.now()
    in_5_seconds = datetime.datetime.now() + datetime.timedelta(seconds=5)
    self.start_time = datetime.datetime.strptime(start_time1,"%Y-%m-%d %H:%M:%S")
    hour_before_start_time = self.start_time - datetime.timedelta(minutes=60)
    half_hour_before_start_time = self.start_time - datetime.timedelta(minutes=30)
    half_time = self.start_time + datetime.timedelta(minutes=50)
    game_ended = self.start_time + datetime.timedelta(minutes=130)

    if hour_before_start_time > actual_time:
      self.run_at(self.game_in_a_hour,hour_before_start_time)
    if half_hour_before_start_time > actual_time:
      self.run_at(self.game_in_a_half_hour,half_hour_before_start_time)
    if self.start_time > actual_time:
      self.run_at(self.game_now,self.start_time)
    if self.start_time < actual_time and actual_time<game_ended:
      self.log("live now")
      self.liveverslagactive = True
      self.run_at(self.run_liveverslag,in_5_seconds) 
    #self.log("next notify: " + self.start_time.strftime("%d-%m-%Y %H:%M"))

this should get you started.
the app also reads out a live report from a website, but that part isnt interesting for you so just ignore what has to do with that.

remember its not clean coding. its something i wrote 1,5 years ago and never cleaned it up.

1 Like