Could someone figure out what I am missing in the following code.
I converted my existing working automation and used example code for reference.
When dark sky cloud coverage (sensor) is more than 70 (as parameter) then turn on the lights (group.weather_related_switches) and run this from sunrise to two hours before sunset
apps.yaml:
Dark House:
class: DarkHouse
module: dark_house
#only called when the input_boolean is OFF
constrain_input_boolean: input_boolean.is_it_cloudy_with_chance_of_meatballs,off
entity_off: group.weather_related_switches
entity_on: group.weather_related_switches
start_time: sunrise
end_time: sunset - 02:00:01
sensor: sensor.dark_sky_cloud_coverage
skycoverage: 70
dark_house.py:
import appdaemon.appapi as appapi
import datetime
class DarkHouse(appapi.AppDaemon):
def initialize(self):
self.active = False
self.set_state("input_boolean.is_it_cloudy_with_chance_of_meatballs", state = 0)
self.log(str(self.get_scheduler_entries()))
darkskycloud = self.get_state(self.args["sensor"])
# Arguments: sensor.dark_sky_cloud_coverage - Value : 86.0
self.log("Arguments: {}".format(self.args["sensor"]) + " - Value : " + darkskycloud)
# Ex: Parameter : 70
self.log("Parameter : {}".format(self.args["skycoverage"]))
self.listen_state(self.light_event, self.args["sensor"])
def light_event(self, entity, attribute, old, new, kwargs):
self.log("Inside light_event function")
darkskycloud = self.get_state(self.args["sensor"])
self.log("Updated Sensor reading from : {}".format(self.args["sensor"]) + " - Value : " + darkskycloud)
self.log("Low light detected: turning {} ON".format(self.args["entity_on"]))
if self.now_is_between(self.args["start_time"], self.args["end_time"]):
if darkskycloud > int(self.args["skycoverage"]) and not self.active:
self.active = True
self.set_state("sensor.is_it_cloudy_with_chance_of_meatballs", state = 1)
if "entity_on" in self.args:
self.log("Turning {} on".format(self.args["entity_on"]))
self.turn_on(self.args["entity_on"])
msg="Attention: \nIt is getting a little dark inside the house. \nI will turn on some extra lights in the living room and kitchen. \n"
self.send_notification_msg(msg)
if darkskycloud < int(self.args["skycoverage"]) and self.active:
self.active = False
self.set_state("sensor.is_it_cloudy_with_chance_of_meatballs", state = 0)
if "entity_off" in self.args:
self.log("Turning {} off".format(self.args["entity_off"]))
self.turn_off(self.args["entity_off"])
msg="Attention: \n I will turn off lights in the living room and kitchen"
self.send_notification_msg(msg)
else:
# We are now dormant so set self.active false
self.active = False
self.set_state("sensor.is_it_cloudy_with_chance_of_meatballs", state = 0)
msg="Attention: This code is working. Please test this later"
self.send_notification_msg(msg)
def send_notification_msg(self, msg):
self.call_service('ifttt/trigger', event='ShreRam_Home', value1=msg)
self.call_service('tts/google_say', entity_id='media_player.living_room_home', message=msg)
Log file when I run appdaemon from command line :
pi@rpi3-aio:/home/homeassistant/.homeassistant/config/logs $ appdaemon -c /home/homeassistant/.homeassistant/config
2017-10-06 09:44:11.455468 INFO AppDaemon Version 2.1.9 starting
2017-10-06 09:44:11.455997 INFO Configuration read from: /home/homeassistant/.homeassistant/config/appdaemon.yaml
2017-10-06 09:44:11.862878 INFO Starting Apps
2017-10-06 09:44:12.290821 INFO Got initial state
2017-10-06 09:44:12.293046 INFO Loading Module: /home/homeassistant/.homeassistant/config/apps/hello.py
2017-10-06 09:44:12.294753 INFO Loading Object hello_world using class HelloWorld from module hello
2017-10-06 09:44:12.453059 INFO hello_world: Hello from AppDaemon
2017-10-06 09:44:12.455820 INFO hello_world: You are now ready to run Apps!
2017-10-06 09:44:12.456297 INFO Loading Module: /home/homeassistant/.homeassistant/config/apps/utils.py
2017-10-06 09:44:12.458020 INFO Loading Object utils using class utilities from module utils
2017-10-06 09:44:12.458718 INFO Loading Module: /home/homeassistant/.homeassistant/config/apps/sunsetlights.py
2017-10-06 09:44:12.460202 INFO Loading Object sunset_lights using class TurnOnLightsAtSunset from module sunsetlights
2017-10-06 09:44:12.467709 INFO sunset_lights: Next sunset at 2017-10-06 18:38:01
2017-10-06 09:44:12.470453 INFO sunset_lights: {}
2017-10-06 09:44:12.471322 INFO Loading Module: /home/homeassistant/.homeassistant/config/apps/bedtimelights.py
2017-10-06 09:44:12.474266 INFO Loading Object bedtime_lights using class TurnOffLightsAtBedtime from module bedtimelights
2017-10-06 09:44:12.481828 INFO bedtime_lights: Today date: 2017-10-06 - Time to trigger: 23:35:00
2017-10-06 09:44:12.482808 INFO Loading Module: /home/homeassistant/.homeassistant/config/apps/dark_house.py
2017-10-06 09:44:12.484493 INFO Loading Object Dark House using class DarkHouse from module dark_house
2017-10-06 09:44:12.703893 INFO Dark House: {'bedtime_lights': {UUID('561c31ba-b438-49e7-a309-0f21d4b27055'): {'type': None, 'kwargs': 1507350900, 'repeat': 1507350900, 'callback': <bound method TurnOffLightsAtBedtime.run_daily_callback of <bedtimelights.TurnOffLightsAtBedtime object at 0x7590da50>>, 'basetime': 1507350900, 'name': 'bedtime_lights', 'offset': 1507350900, 'interval': 1507350900, 'timestamp': 1507350900}}, 'sunset_lights': {UUID('86bcee05-3553-44a7-9e66-3ed99066ac0a'): {'type': 'next_setting', 'kwargs': 1507333081, 'repeat': 1507333081, 'callback': <bound method TurnOnLightsAtSunset.sunset_cb of <sunsetlights.TurnOnLightsAtSunset object at 0x758ddf70>>, 'basetime': 1507333081, 'name': 'sunset_lights', 'offset': 1507333081, 'interval': 1507333081, 'timestamp': 1507333681}}}
2017-10-06 09:44:12.706841 INFO Dark House: Arguments: sensor.dark_sky_cloud_coverage - Value : 84.0
2017-10-06 09:44:12.709485 INFO Dark House: Parameter : 70
2017-10-06 09:44:12.710105 INFO App initialization complete
2017-10-06 09:44:12.710764 INFO Dashboards are disabled
2017-10-06 09:44:12.711060 INFO API is disabled
2017-10-06 09:44:12.894201 INFO Connected to Home Assistant 0.54.0