Appdaemon Input_select/set_options help

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:

self.call_service("input_select/set_options", entity_id="input_select.prova",
            options= "lista")

can anyone help me?

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 for the reply
with something like this?

groupitem = self.get_state("group.binary_sensors","all");
entity_list = groupitem['attributes']['device_class']

meanwhile I try, but I was hoping for something more direct

@ReneTode showed me how to loop through all entities.

This should create a list of all binary sensors with one of your specified device classes:

self.entity_list = []
    entities = self.get_state()
    alarm_device_class = ['door', 'garage_door', 'lock', 'opening', 'window']

    for entity, state in entities.items():
        domain = entity.split(".")[0]
        device_class = state["attributes"]["device_class"]
        if domain == "binary_sensor" and device_class in alarm_device_class:
            self.entity_list.append(entity)

You can then pass self.entity_list to your input_select.

in that case you could even better use:

self.entity_list = []
entities = self.get_state("binary_sensor", attribute = "all")
alarm_device_class = ['door', 'garage_door', 'lock', 'opening', 'window']

for entity, state in entities.items():
    device_class = state["attributes"]["device_class"]
    if  device_class in alarm_device_class:
        self.entity_list.append(entity)
1 Like

thank you all for your help, I’m learning a lot. I tried the two codes, but it gives me an error. surely I’m wrong because of my limited knowledge.

    def ha_event(self, entity_id, event_name, data, **kwargs):
        self.entity_list = []
        entities = self.get_state("binary_sensor", attribute = "all")
        alarm_device_class = ['door', 'garage_door', 'lock', 'opening', 'window']

        for entity, state in entities.items():
            device_class = state["attributes"]["device_class"]
            if  device_class in alarm_device_class:
                self.entity_list.append(entity)
                self.call_service("input_select/set_options", entity_id="input_select.prova",
                    options= self.entity_list)
2020-01-11 19:13:56.903238 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 15, in ha_event
    entities = self.get_state("binary_sensor", attribute = "all")
  File "/usr/lib/python3.8/site-packages/appdaemon/plugins/hass/hassapi.py", line 97, in get_state
    return super(Hass, self).get_state(namespace, entity, **kwargs)
  File "/usr/lib/python3.8/site-packages/appdaemon/appapi.py", line 221, in get_state
    raise ValueError(
ValueError: our_automation_name: Invalid entity ID: None

sorry, my mistake.
it seems that you cant combine “all” attributes, with a group of entities in AD 3

so in that case you need to change to:

    def ha_event(self, entity_id, event_name, data, **kwargs):
        self.entity_list = []
        entities = self.get_state("binary_sensor")
        alarm_device_class = ['door', 'garage_door', 'lock', 'opening', 'window']

        for entity in entities:
            device_class = self.get_state(entity, attribute = "all")["attributes"]["device_class"]
            if  device_class in alarm_device_class:
                self.entity_list.append(entity)
                self.call_service("input_select/set_options", entity_id="input_select.prova",
                    options= self.entity_list)

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'

yet the “device_class” attribute exists

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 = ""

thank you all, solved using:

device_class = self.get_state(entity, attribute = "device_class")
1 Like

A more compact way to write this would be

    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]
1 Like