Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hassapi as hass;
- import time;
- """
- Monitor cameras for motion, and turn lights on if after dark, then turn them off after no more motion is detected
- """
- class MotionLights(hass.Hass):
- def initialize(self):
- self.log("MotionLights.initialize()")
- last_motion_event = { }
- light_delay = 30
- cameras = { }
- cameras[ "c01_motion" ] = [ "switch.exterior_front_floods" ]
- cameras[ "c03_motion" ] = [ "switch.exterior_front_floods" ]
- cameras[ "c09_motion" ] = [ "switch.exterior_front_floods" ]
- cameras[ "c11_motion" ] = [ "switch.exterior_front_floods" ]
- cameras[ "c12_motion" ] = [ "switch.exterior_front_floods" ]
- cameras[ "c19_motion" ] = [ "switch.exterior_driveway_floods" ]
- cameras[ "c20_motion" ] = [ "switch.exterior_driveway_floods" ]
- cameras[ "doorbird_front_door_motion_detected" ] = [ "switch.front_porch_overhead_light", "switch.exterior_front_floods", "switch.exterior_driveway_floods" ]
- for camera in cameras.keys():
- self.listen_state(self.motion, "binary_sensor." + camera, new="on")
- def motion(self, entity, attribute, old, new, kwargs):
- last_motion_event[ entity ] = time.time()
- if self.sun_down():
- for light in cameras[entity]:
- self.turn_on(light)
- self.run_in(self.light_off, light_delay, camera = entity)
- def light_off(self, kwargs):
- last_motion = 0
- if camera in last_motion_event:
- last_motion = last_motion_event[camera]
- for light in cameras[camera]:
- for othercamera in cameras.keys():
- if othercamera in last_motion_event:
- if light in othercamera:
- if last_motion_event[othercamera] > last_motion:
- last_motion = last_motion_event[othercamera];
- now = time.time()
- if now - last_motion > light_delay:
- for light in cameras[camera]:
- self.turn_off(light)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement