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)
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 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)
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.
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.
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.