AppDaemon Q&A

Thanks i will have a look.

No worries - sorry for the short reply, they were just about to turn the WiFi off on the plane :slight_smile:

Please forgive my ignorance as I begin to figure out rule and automations with Home Assistant.

But is a basic If this then that type rule available?

ie… If myitem.value = 1, do this, if not do that?

And can you then do that with combinations of OR and AND conditions. ie… myitem.value = 1 AND myOtherItem.value = 2, then do this, and if not met, do something else.

Or does it require separate rules and automations? I think this is where AppDaemon can help?

it is basic python.

so options like you want is just like it is.
OR and AND sind like in normal python programming available.
and IF ELSE also.

just like

for (every switchname witch start with A):
if switch not on:
turn switch on.

you can also start other automations if you like.

Hi,

I’ve been considering going from openHAB to HA a while now but I was in doubt when I saw how the yaml/configuration/rules system worked. However, now with this Appdaemon I really think I can manage to port my openHAB rules without too much effort! Fantastic work on this :slight_smile:

There’s only one thing I’m a little concerned about, concurreny issues. In openHAB I saw some times z-wave sensors send duplicate temperature reports (I’m not sure if this was due to z-wave radio issues or the z-wave binding/handler). This meant that if I tried to catch a temperature state event I suddenly had 2 “rules” running simultaneousy, which of course caused problems. I solved this after a while with locking when I figured out how (in the horrible world of java).
Anywho, I noticed you mention this problem in the API docs, do you have an example on how to solve this problem without locking ? global vars? :slight_smile:

I am following back to my finding from the above post AppDaemon Q&A

So, I moved AppDaemon to the same computer as Home Assistant and so far I don’t see any issue that I saw of not getting called my callback for status change event. It proves some how my intranet was causing something so it will not work.
Thanks all for the help

1 Like

I am also noticing similar issue with my Ecobee thermostats, which sends/fires multiple times state change events. So far I have not found the solution but added code to keep track of old state my own using AppDaemon app to prevent processing again for the same event.
By the way in Home Assistant trigger I have already tried putting “from” and “to” but didn’t help to stop multiple calls.
To add more complexity I have templated sensor, so may be original sensor and then template processing causes another status change?

I am trying to find a page with release details for AppDaemon like Home Assistant has one at: https://github.com/home-assistant/home-assistant/releases

I remember seeing such page for AppDaemon but I couldn’t find it today, someone could point me where it is?
Thanks

Never mind, I found it here https://github.com/acockburn/appdaemon/blob/master/HISTORY.md

It would pretty much be locking although it’s a lot easier in Python. I haven’t yet seen a need for this in app level code but you have a valid use case.

Thank you so much for this! A great tool for HASS to make some more advanced automations :smiley:

Would it be possible to make a thread for more examples? Specifically, I’d like to make my sunrise scripts run at a time based on an input_select, and I have basically non-existant python experience (but I sense that’s about to change ;))

If anyone has an alarm script like this, please post it for inspiration!

I think its a great idea to start a new thread specifically asking the question and let others chime in - lets see if we can crowdsouce an answer :slight_smile:

Hi.

How do i set an timer to execute every 15min?

You can use the run_every() scheduler call:

https://github.com/acockburn/appdaemon/blob/master/API.md#run_every

Here is that i tried. None of them works.
I’m new to python so mabye the fault i obvious for you.
I want my script to run 00, 15 30 and 45.

import datetime
time = datetime.datetime.now() + datetime.timedelta(hours=0)
repeat = datetime.timedelta(minutes=15)
self.run_every(self.run_every_c, time, repeat)

time = datetime.datetime.now()
repeat = datetime.timedelta(minutes=15)
self.run_every(self.run_every_c, time, repeat)

Thanks for your help

what do you have in the function run_every_c??

I have a script that is setting states of all my lights depending on light level, time and scene.
I want to run it every 15 mins because i have the RFXTRX and want to sync some lights in case it has missed the signal.I aslo have some listeners that triggers the samt script. Currently i run it every minute witch is way to frequent.

it is easier to see what is going wrong if you post the complete app and tell us if you see anything in the error.log

Agree with Rene - what does “not working” mean? If you get us some more info I am sure we will be able to figure it out.

1 Like
timer = datetime.datetime.now() + datetime.timedelta(hours=0)
repeat = datetime.timedelta(minutes=15)
self.run_every(self.event, timer, repeat)

This gives me this error:
File “/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py”, line 381, in do_every_second
exec_schedule(name, entry, conf.schedule[name][entry])
File “/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py”, line 298, in exec_schedule
args[“basetime”] += args[“interval”]
TypeError: unsupported operand type(s) for +=: ‘int’ and ‘datetime.timedelta’

timer = datetime.datetime.now()
repeat = datetime.timedelta(minutes=15)
self.run_every(self.event, timer, repeat)

This gives me this error:
File “/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py”, line 381, in do_every_second
exec_schedule(name, entry, conf.schedule[name][entry])
File “/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py”, line 298, in exec_schedule
args[“basetime”] += args[“interval”]
TypeError: unsupported operand type(s) for +=: ‘int’ and ‘datetime.timedelta’

This is all my code (probaby looks dirty but it works with self.run_minutely. Im new to python so i have much to learn.)

import appdaemon.appapi as appapi
import xml.etree.ElementTree
import datetime

class Schedule(appapi.AppDaemon):

def initialize(self):
self.listen_state(self.state_event, ‘group.harmony’)
self.listen_state(self.state_event, ‘group.all_devices’)
self.listen_state(self.state_event, ‘switch.sleep_button’)
self.listen_state(self.state_event, ‘input_boolean.manual_lights’)

timer = datetime.datetime.now()
repeat = datetime.timedelta(minutes=15)
self.run_every(self.event, timer, repeat)

def event(self, kwargs):
self.log(‘Running light control’)
if self.get_state(‘input_boolean.manual_lights’) == ‘off’:
if self.get_state(‘switch.sleep_button’) == ‘on’:
scheme = ‘night’
elif self.get_state(‘switch.holiday’) == ‘on’ and self.get_state(‘group.all_devices’) == ‘home’:
scheme = ‘holiday’
elif self.get_state(‘switch.holiday’) == ‘off’ and self.get_state(‘group.all_devices’) == ‘home’:
scheme = ‘weekday’
elif self.get_state(‘group.all_devices’) == ‘not_home’:
scheme = ‘away’
self.log(‘Scheme: ’ + scheme)
today = str(datetime.date.today())
e = xml.etree.ElementTree.parse(’/home/hass/.homeassistant/schedule.xml’).getroot()
light = float(self.get_state(“sensor.light”))
for entities in e.findall(‘entity’):
name = entities.find(‘name’)
if entities.find(scheme + ‘/lux’) != None:
lux = entities.find(scheme + ‘/lux’)
else:
lux = entities.find(‘lux’)
self.log('Entity: ’ + name.text + ‘, Lux: ’ + lux.text + ‘(current:’ + str(light) + ‘)’)
if float(lux.text) > light:
for schedule in entities.findall(scheme + ‘/schedule’):
time = schedule.attrib.get(‘time’)
sync = schedule.attrib.get(‘sync’)
sel_str = str(today + ’ ’ + time + ‘:00’)
sel_datetime = datetime.datetime.strptime(sel_str, ‘%Y-%m-%d %H:%M:%S’)
if datetime.datetime.now() >= sel_datetime:
if self.get_state(‘group.harmony’) == ‘on’ and schedule.attrib.get(‘scene’) == ‘movie’:
event = sel_datetime
new_state = schedule.attrib.get(‘state’)
new_brightness = schedule.attrib.get(‘brightness’)
elif schedule.attrib.get(‘scene’) == None:
event = sel_datetime
new_state = schedule.attrib.get(‘state’)
new_brightness = schedule.attrib.get(‘brightness’)
self.log(‘Settings: time=’ + str(event) + ’ sync=’ + sync)
if sync == ‘true’ or (sync == ‘false’ and (self.get_state(name.text) != new_state or (new_brightness and self.get_state(name.text, “brightness”) != new_brightness))):
self.log('Old state: ’ + self.get_state(name.text) + ', Old brightness: ’ + str(self.get_state(name.text, “brightness”)))
self.log('New state: ’ + new_state + ', New brightness: ’ + str(new_brightness))
self.set_state(name.text, state = new_state, attributes = {“brightness”: new_brightness})
new_state = None
new_brightness = None
else:
if sync == ‘true’ or (sync == ‘false’ and (self.get_state(name.text) != ‘off’)):
self.log('Old state: ’ + self.get_state(name.text) + ‘, New state: off’)
self.set_state(name.text, state = “off”)

def state_event(self, entity, attribute, old, new, kwargs):
self.event(kwargs)

Thanks for your efforts!