So I started using AppDaemon (version 2.1.10) to automate things, but I have a hard time figuring out the correct syntax.
What I want:
- if we are not at home
- if the sun is up
- if the lights are on
- if the override button is off
- then switch off all lights.
How I implemented this:
- event listeners for specific devices leaving the house (bluetooth and gps)
- event listener for the sun <-- this is problematic
Either the sun listener keeps every minute shouting that it is above the horizon or I get some positional arguments error. Do I have the correct approach here?
My code:
import appdaemon.appapi as appapi
#
# Automation 5 - Away for the day.
#
# Switch off all the lights
#
class Auto5(appapi.AppDaemon):
def initialize(self):
self.log("Automation 5 reloaded. Waiting for people leaving the house.")
self.listen_state(self.check_away, "device_tracker.user2_iphone", old="home")
self.listen_state(self.check_away, "device_tracker.user1_bluetooth", old="home")
self.listen_state(self.check_away, "device_tracker.user1_iphone", old="home")
# this keeps on firing each minute
# self.listen_state(self.check_away, "sun.sun", new="above_horizon")
# this raises an error:
# TypeError: check_away() missing 4 required positional arguments: 'attribute', 'old', 'new', and 'kwargs'
# self.run_at_sunrise(self.check_away)
def check_away(self, entity, attribute, old, new, kwargs):
self.log("A device left home, checking for other conditions")
# check actual state of devices and time conditions
if (self.get_tracker_state("device_tracker.user2_iphone") != "home"
and self.get_tracker_state("device_tracker.user1_bluetooth") != "home"
and self.get_tracker_state("device_tracker.user1_iphone") != "home"
and self.get_tracker_state("input_boolean.override") == "off"
and self.now_is_between("sunrise", "sunset")):
self.log("Everyone is away for the day, and there is no override. Switching off all lights.")
self.call_service("light/turn_off",
entity_id = "group.all_lights")
# and send a message
self.call_service("notify/ios_iphone_1",
message = "All lights have been switched off")