I have just started studying appdaemon and python for a couple of days and I managed to make a small app already, but I can’t understand how to create an equlivalent of the following code:
{% set device_alarm = ['door', 'garage_door', 'lock', 'opening', 'window'] %}
{% for item in expand(states['binary_sensor']) if (item.attributes['device_class'] in device_alarm) %}
"{{ item.name }}",
{% endfor %}
"Nessuno"
I currently use it successfully on a rest command but I have no idea how to do it via the command:
I don’t understand. The code on top gives you a list of each binary sensor that has one of the specified device classes. The code on the bottom sets the value of an input_select to “lista”. Could you please explain what you want to do?
I would like to rewrite the template that retrieves the binary sensors in appdaemon to be able to use it with the command that populates the input select, but I have no idea how to do it
I don’t know if you can loop through all entities in AppDaemon, will check. As a workaround you can put all binary sensors in a group and loop through this group.
Thanks again for the umpteenth answer, the problem I had already solved by using the code that had proposed @Burningstone. but both with what and with this new posted by you I’m running into a new problem:
2020-01-11 20:19:01.929352 WARNING AppDaemon: Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/appdaemon/appdaemon.py", line 600, in worker
funcref(args["event"], data, args["kwargs"])
File "/config/appdaemon/apps/prova/prova.py", line 34, in appd_event
device_class = self.get_state(entity, attribute = "all")["attributes"]["device_class"]
KeyError: 'device_class'
it seems you also got binary sensors without a device class.
so you need to check if device class is in the dict.
state = self.get_state(entity, attribute = "all")
if "attributes" in state and "device_class" in state["attributes"]:
device_class = state["attributes"]["device_class"]
else:
device_class = ""
alarm_classes = {'door', 'garage_door', 'lock', 'opening', 'window'}
alarm_sensors = [ent for ent in self.get_state("binary_sensor")
if self.get_state(ent, attribute="device_class") in alarm_classes]