Here is the code. If any of the entities were turned on during the day time then they will turn off after the timer and also if the deck light is turned at night then after five minutes it gets turned off. This code worked before and I think I need to fine tune this and remove redundant code.
watch_dog_deck:
module: watchdog
class: WatchDog
constrain_input_boolean: input_boolean.ad_watch_dog
# in mins
limit: 5
# Deck light
entities:
- switch.ge_12722_onoff_relay_switch_switch
import appdaemon.plugins.hass.hassapi as hass
from datetime import datetime as dt
# from sane_app_logging import SaneLoggingApp
# LOG_DEBUG = False
__version__ = "1.2"
class WatchDog(hass.Hass):
# class WatchDog(hass.Hass, SaneLoggingApp):
def initialize(self):
# self._setup_logging(self.__class__.__name__, LOG_DEBUG)
# self._log.info("Initializing WatchDog...")
self.log("Initializing WatchDog...")
self.handle = None
self.utils = self.get_app('utils')
if "entities" in self.args:
for entity in self.args["entities"]:
# self._log.debug("Entity : " + self.get_state(entity, attribute="friendly_name"))
self.log("Entity : " + self.get_state(entity, attribute="friendly_name"))
if self.name == 'watch_dog_hallway_light':
if self.now_is_between("12:30:00", "sunrise") and self.get_state("sensor.house_mode") == "Night":
self.listen_state(self.light_event, entity)
else:
# self._log.debug("Do not monitor entity : " + self.get_state(entity, attribute='friendly_name') + ". Right now it is within scheduled time or not in night mode.")
self.log("Do not monitor entity : " + self.get_state(entity, attribute='friendly_name') + ". Right now it is within scheduled time or not in night mode.")
else:
self.listen_state(self.light_event, entity)
# if the light is turned on by motion detector then add addl logic to set brightness
if self.name == 'watch_dog_hallway_light':
self.listen_state(self.listenstate_hallway_light, "light.hallway_light_bulb_one")
# self._log.info('Done initializing WatchDog.')
self.log('Done initializing WatchDog.')
def light_event(self, entity, attribute, old, new, kwargs):
self.log('WatchDog: light_event Function triggered ')
msg= "Watchdog : " + self.friendly_name(entity) + " turned : " + str(new)
# self._log.debug(msg)
self.log(msg)
if "limit" in self.args:
delay = self.args["limit"]
else:
# self._log.critical("Check the apps.yaml file for missing parameter.")
self.log("Check the apps.yaml file for missing parameter.")
return
# start the timer only when the entity is turned on
if self.get_state(entity) == "on":
#self.log("Turn off entity - " + self.get_state(entity, attribute='friendly_name') + " - after " + str(delay) + " minutes")
# self._log.debug(self.get_state(entity, attribute='friendly_name') + " will be turned OFF after " + str(delay) + " minutes")
self.log(self.get_state(entity, attribute='friendly_name') + " will be turned OFF after " + str(delay) + " minutes")
self.cancel_timer(self.handle)
self.handle = self.run_in(self.runin_light_off, delay*60)
else:
# self._log.warning("Ignore. This entity : " + self.get_state(entity, attribute='friendly_name') + " is not turned on")
self.log("Ignore. This entity : " + self.get_state(entity, attribute='friendly_name') + " is not turned on")
def runin_light_off(self, kwargs):
self.log('WatchDog: runin_light_off Function triggered ')
# self._log.debug(self.args)
# self.log(self.args)
#2018-10-04 14:49:33.087437 INFO watch_dog_deck:
#{'module': 'watchdog', 'class': 'WatchDog', 'constrain_input_boolean': 'input_boolean.ad_watch_dog', 'limit': 5, 'entities': ['switch.ge_12722_onoff_relay_switch_switch']}
# turn off only if the entity is ON state
for entity in self.args["entities"]:
if self.get_state(entity) == "on":
self.turn_off(entity)
if (self.sun_up()):
msg= self.get_state(entity, attribute='friendly_name') + " is ON during the day time. Saving power by turning it off!\n"
self.set_state("sensor.appd_notify_message", state=msg)
else:
msg= self.get_state(entity, attribute='friendly_name') + " turned OFF "
self.set_state("sensor.appd_notify_message", state=msg)
else:
# self._log.info("IGNORE - Entity : {} - state : {}".format(self.friendly_name(entity), self.get_state(entity)))
self.log("IGNORE - Entity : {} - state : {}".format(self.friendly_name(entity), self.get_state(entity)))
def listenstate_hallway_light(self, entity, attribute, old, new, kwargs):
self.log('WatchDog: listenstate_hallway_light Function triggered ')
# if "limit" in self.args:
# delay = self.args["limit"]
# watch_dog_steps_light: Hallway light one - attribute: state - changed from on to off
# self._log.info("{} - attribute: {} - changed from {} to {}".format(self.friendly_name(entity), attribute, old, new))
self.log("{} - attribute: {} - changed from {} to {}".format(self.friendly_name(entity), attribute, old, new))
if new == "on":
# at night time if this light is turned on then set brightness to 50%
#brightness is in 0 - 255, constrain to 255 to get percentage-of
if self.get_state("sensor.house_mode") == "Night":
blevel = round(int(float(50)) * 255 / 100)
# self._log.info("light.{} set to {}.".format(entity, blevel))
self.log("light.{} set to {}.".format(entity, blevel))
self.turn_on(entity, brightness=blevel)
else:
# self._log.debug("It's not night time. Set the brightness level to default")
self.log("It's not night time. Set the brightness level to default")
# when light brightness was set to 50% from if logic
# and tried to set brightness to full via Wink app then logic comes here ..Old = On and New = On
# because just brightness was changed
blevel = round(int(float(100)) * 255 / 100)
self.turn_on(entity, brightness=blevel)
else:
# self.log(msg)
# self._log.debug("This entity turned off. Nothing to do here")
self.log("This entity turned off. Nothing to do here")