So here is today’s scripting attempt as you can tell I’m getting a little more brave now trying to combine 2 things into one script.
Here is the use case. Use it for both scheduled times as well as presence
Here is my appdaemon.yaml
water_heater_off:
module: turn_on_off_at_time
class: daily_turn_on_off_time
timeon: "04:30:00"
timeoff: "00:00:00"
switchID: switch.water_heater_switch
constrain_presence: anyone
water_heater_off_nobody_home:
module: turn_on_off_at_time
class: daily_turn_on_off_time
switchID: switch.water_heater_switch
The app itself
import appdaemon.appapi as appapi
import datetime
class daily_turn_on_off_time(appapi.AppDaemon):
#
#
#
# Args
# timeon = what time you want this to turn on
# timeoff = what time you want this to turn off
# switchID = what do you want to turn on/off
#
def initialize(self):
# So first we'll take the time that is passed into us and parse it out
timeon = self.args["timeon"]
timeoff = self.args["timeoff"]
if timeon is not None:
time_on_parsed = self.parse_time(timeon)
if timeoff is not None:
time_off_parsed = self.parse_time(timeoff)
# Now its time for the fun stuff
if time_on_parsed is not None:
self.run_daily(self.turn_on_function, time_on_parsed)
if time_off_parsed is not None:
self.run_daily(self.turn_off_function, time_off_parsed)
self.listen_state(self.turn_off_function, "group.all_devices", new="not_home")
def turn_on_function(self, entity, attribute, old, new, kwargs):
self.turn_on(self.args["switchID"])
def turn_off_function(self, entity, attribute, old, new, kwargs):
self.turn_off(self.args["switchID"])
Problem is this
2018-03-06 15:08:48.029320 WARNING Unexpected error during loading of water_heater_off_nobody_home:
2018-03-06 15:08:48.030129 WARNING ------------------------------------------------------------
2018-03-06 15:08:48.032972 WARNING Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 921, in read_app
init_object(name, class_name, module_name, config[name])
File "/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 595, in init_object
conf.objects[name]["object"].initialize()
File "/config/hadaemon/apps/turn_on_off_at_time.py", line 16, in initialize
timeon = self.args["timeon"]
KeyError: 'timeon'
2018-03-06 15:08:48.033943 WARNING ------------------------------------------------------------
I was thinking that I"ll just check to make sure the value isn’t null before we proceed but I guess I just don’t understand how to properly do that. I tried also doing
if self.args[“timeon”] is not None:
but even that has issues so I think that I just don’t know how to properly do that.