Hi,
I’m trying to do some basic automation, I have 1 light and 1 sensor.
My requirements are
Light comes on at sunset at dim level.
Between sunset and midnight if someone triggers the sensor the light will go to maximum for 10 minutes.
After midnight the light will remain at dim level until at sunset it switches off.
The script seems to work fine for a few days but after a while it seems to switch off around midnight instead of just dimming.
I’ve littered the script with debug but still can’t work it out, my Python skills are very basic and I’m obviously doing something incorrectly.
Would someone be able to cast an eye over the script and see if they can spot anything wrong, it’s driving me up the wall now.
My config file is
hallwaylight:
module: hallway
class: Hallway
sensor: binary_sensor.sensor_hallway_occupancy
target: light.hallway_light
timeout: 600
lightmapping:
Dim:
brightness: 1
color: white
rgb_color: [255,153,0]
color_temp: 3500
Full:
brightness: 255
color: white
rgb_color: [255,192,141]
color_temp: 3500
sunsetoffsetmins: 30
dimlightstime: "23:00:00"
This is my script
import appdaemon.plugins.hass.hassapi as hass
import datetime
class Hallway(hass.Hass):
def initialize(self):
self.sensor = self.args.get("sensor")
self.target = self.args["target"]
self.timeout = self.args["timeout"]
self.sunset_offset = self.args["sunsetoffsetmins"]
self.light_mappings = self.args['lightmapping']
self.dim_light_time = self.args['dimlightstime']
self.current_light_setting = None
self.timer = None
## Turn light off at sunrise - On at sunset (with offset)
self.run_at_sunrise(self.sunrise_cb)
self.run_at_sunset(self.before_sunset_cb, offset=-(self.sunset_offset*60))
## Dim lights at specified time
self.run_daily(self.dim_lights_at_time, self.dim_light_time)
## When app runs figure out what state, i.e. light on/off/ full/dim etc
self.run_in(self.initial_state, 0)
## Listen to sensor
self.listen_state(self.motion_callback, self.sensor, new = "on")
def initial_state(self, kwargs):
sunset_str = "sunset - 00:{}:00".format(self.sunset_offset)
if self.now_is_between(sunset_str, "sunrise"):
self.dim_lights(kwargs)
else:
self.log("Turning off 1")
self.turn_off(self.target)
def sunrise_cb(self, kwargs):
self.log("Sunrise")
self.current_light_setting = None;
self.log("Turning off 2")
self.turn_off(self.target)
self.log('Next sunset at ' + str(self.sunset()))
def before_sunset_cb(self, kwargs):
self.log("Sunset")
self.dim_lights(kwargs)
def light_operating_times(self):
sunset_str = "sunset - 00:{}:00".format(self.sunset_offset)
return self.now_is_between(sunset_str, "sunrise")
def full_light_operating_times(self):
sunset_str = "sunset - 00:{}:00".format(self.sunset_offset)
return self.now_is_between(sunset_str, "00:00:00")
def dim_lights(self, kwargs):
if self.light_operating_times():
self.log("turn_on: Dim")
self.current_light_setting = 'Dim'
self.turn_on(self.target, brightness = self.light_mappings['Dim']['brightness'], transition = 2)
def dim_lights_at_time(self, kwargs):
self.log("turn_on: Dim at time")
self.dim_lights(kwargs)
def full_lights(self, kwargs):
if self.light_operating_times():
self.log("turn_on: Full")
self.current_light_setting = 'Full'
self.turn_on(self.target, brightness = self.light_mappings['Full']['brightness'], transition = 2)
def motion_callback(self, entity, attribute, old, new, kwargs):
if self.current_light_setting == 'Dim' and self.full_light_operating_times():
self.full_lights(kwargs)
self.set_timer()
def set_timer(self):
self.log("setting timer");
if self.timer is not None:
self.cancel_timer(self.timer)
self.timer = self.run_in(self.timeout_callback, self.timeout)
def timeout_callback(self, kwargs):
self.log("turn_on: Last")
self.timer = None
if self.light_operating_times():
self.dim_lights(kwargs)
Any help would be much appreciated, please let me know if I can provide any further details.
Best regards,
Marvin