Moving some automations from HASS to AD with google calendar

So I have an automation for my Holiday lights and its controlled via Google Calendar (I friggin love that integration piece)

So here is the deal I have the event setup in the calendar to span several days so it shows up in the calendar as an all day event.

The automation looks like this

#Holiday light automations
- 
  action: 
    - 
      entity_id: switch.outside_christmas_lights
      service: homeassistant.turn_on
  alias: "Holiday Lights on normal"
  condition: 
    condition: state
    entity_id: calendar.holiday_lights
    state: "on"
  id: holiday_lights
  trigger: 
    - 
      event: sunset
      offset: "00:20:00"
      platform: sun

So I’m working on changing this over to AD.

import appdaemon.plugins.hass.hassapi as hass

class holiday(hass.Hass):

    def initialize(self):

            self.listen_state(self.holiday_lights_function,calendar.holiday_lights)

Yeah I know made it real far didn’t I?

I’ve hit a hang up. If I set this to listen to the calendar which I think is the best possible method because then I’m not forcing AD to tie up a resource or something like hey every night at sunset run this and check if the calendar is on. I think that is just a waste of a thread.

listen to the calendar; calendar changes to on
then have a function to call another function at suset to do it or

In the initialize would you instead of doing the listen state. Would you make it an if statement and do

if calendar = on
self.sunset_cb(function goes here)

Just thoughts in my head as I try and figure out the best way to write this and keep it efficient.

Ok so typing all of that out above actually allowed me to think through it as well with my pseudo code and I’ve now come up with

import appdaemon.plugins.hass.hassapi as hass

class holiday(hass.Hass):

    def initialize(self):
        holiday_calendar = self.listen_state(calendar.holiday_lights)
        if holiday_calendar == on:
            self.run_at_sunset(self.holiday_light_function, offset=int (self.args["sunset_offset"]))

    def holiday_light_function( self, kwargs):
        self.log("Turning on the spot light")
        self.turn_on(switch.outside_christmas_lights)
        self.run_in(self.holiday_light_off_function, 21600)

    def holiday_light_off_function(self, kwargs):
        self.log("Turning off the spot light")
        self.turn_off(switch.outside_christmas_lights)

I think you are over thinking this :wink:

The simplest way by far would be to check every sunset and see if the calendar is on - it’s really no big deal to run a piece of code for 0.01 of a second once a day :wink: And the logic is also much simpler. Your code above won’t work as is but you could make it work, the approach is not incorrect. However, something like this would be a lot simpler:

import appdaemon.plugins.hass.hassapi as hass

class holiday(hass.Hass):

    def initialize(self):
        self.run_at_sunset(self.holiday_light_function, offset=int(self.args["sunset_offset"]))

    def holiday_light_function( self, kwargs):
        if self.get_state(calendar.holiday_lights) == "on":
            self.log("Turning on the spot light")
            self.turn_on(switch.outside_christmas_lights)
            self.run_in(self.holiday_light_off_function, 21600)

    def holiday_light_off_function(self, kwargs):
        self.turn_off(switch.outside_christmas_lights)
2 Likes

Yeah I have a problem with over thinking things.

i think you were on the right track but got yourselve tangled up:

import appdaemon.plugins.hass.hassapi as hass

class holiday(hass.Hass):

    def initialize(self):
            self.listen_state(self.holiday_lights_function,calendar.holiday_lights, new="on")

    def holiday_light_function( self, kwargs):
        self.log("Turning on the spot light")
        self.turn_on(switch.outside_christmas_lights)
        self.run_in(self.holiday_light_off_function, 21600)

    def holiday_light_off_function(self, kwargs):
        self.log("Turning off the spot light")
        self.turn_off(switch.outside_christmas_lights)

it saves you a get_state every day at sunset :wink:

Thank you @aimc and @ReneTode for your help with this its greatly appreciated.

Ok so in this scenario my Christmas lights are a whole new animal

import appdaemon.plugins.hass.hassapi as hass

class christmas(hass.Hass):

    def initialize(self):
        self.run_at_sunset(self.christmas_light_function, offset=int(self.args["sunset_offset"]))
        self.run_at_sunset(self.christmas_eve_light_function, offset=int(self.args["sunset_offset"]))
        self.listen_state(self.christmas_light_off_function, calendar.christmas_lights_off, new="on")

    def christmas_light_function( self, kwargs):
        if self.get_state(calendar.christmas_lights) == "on":
            self.log("Turning on the Christmas lights")
            self.turn_on(group.christmas_lights)
            self.run_in(self.christmas_light_off_function, 21600)      

    def christmas_eve_light_function( self, kwargs):
        if self.get_state(calendar.christmas_eve_lights) == "on":
            self.log("Turning on the Christmas lights")
            self.turn_on(group.christmas_lights)

    def christmas_light_off_function(self, kwargs):
        self.turn_off(group.christmas_lights)  

Normally they just turn on at 20 mins after sunset and then run for 6 hours and turn off. So using the holiday light code from above I copied it to this.

But then there is Christmas Eve. The Mrs wants the lights to turn on like normal but then they stay on until midnight on the 26th. This should work just fine right? Granted of course as long as my events are setup properly in the calendar.

dont forget that you do need to get christmas eve out of your christmaslight calendar. :wink:

but you now do everything twice.
you wont notice it, but your lights get turned on twice and turned of twice.

import appdaemon.plugins.hass.hassapi as hass

class holiday(hass.Hass):

    def initialize(self):
            self.listen_state(self.holiday_lights_function,calendar.holiday_lights, new="on")

    def holiday_light_function( self, kwargs):
        self.log("Turning on the spot light")
        self.turn_on(switch.outside_christmas_lights)
        if self.get_state(calendar.christmas_eve_lights) == "on":
            self.run_in(self.holiday_light_off_function, 21600 + thenight)
        else:
            self.run_in(self.holiday_light_off_function, 21600)

    def holiday_light_off_function(self, kwargs):
        self.log("Turning off the spot light")
        self.turn_off(switch.outside_christmas_lights)

this way you can let christmas eve in youre christmasevelights calendar and they only get turned on and off once.

I actually setup the Christmas Light stuff so that it was its very own calendar entry so I could put it on repeat every year and be done with it. My holiday lights are for all the other lights since I have one of those projector spot light things that we change out the cartridge for different holidays. I just let Christmas be its own animal and it also controls different lights at Christmas vs just 1 light for other holidays.

What is + thenight?

with + thenight i meant the time from the night that it should run longer :wink:

But if I leave my calendar events like this

Christmas Lights
11/26-12/23 then 12/26-12/31

Christmas Eve
12/24

Christmas Lights Off midnight 12/26

Then would my script be ok and not call the lights twice? I don’t do the holiday event title during Christmas

Maybe showing my calendar might help

i am sorry, i must correct myself.
i saw the sunset and listen_state and didnt look correctly.
that way it isnt turning on/off twice.
i think it should run as you want.

1 Like

Thank you very much for the proof read

1 Like