Help to filter entities in data_template

I get two variables (room and appliance) from DialogFlow. They are specified in template only for testing. Template works fine, but I wonder if there is any other way of filtering. I don’t like the output of {{entities}} is string, not an array. But I do not know how to do otherwise.

data_template:
  entity_id: >
    {% set room = ['kitchen','bedroom'] %}
    {% set appliance = 'livolo' %}
    {% set type = { 'livolo':'switch', 'neoclimaartvogue': 'climate', 'broadlinksp3sboiler':'switch' } %}
    {% set entities = type[appliance]+'.'+appliance+'_' +room | join(type[appliance]+'.'+appliance+'_')  %}
    {{ states | selectattr('entity_id', 'in', entities)
              | map(attribute='entity_id')
              | join(', ') }}

Do you know how to code in Python? You could use the python_script integration. Much more flexible in handling data.

Not enough yet…

How can I do something like that with an array?

{% set room = 'livingroom' %}
{{ room.replace(room, 'switch.livolo_'+room)}}

It’s not very clear what it is you’re exactly trying to do. However, if you’re trying to create entities as a list that is made up of synthesized entity_id’s based on the room and appliance variables, then maybe this will work for you:

data_template:
  entity_id: >
    {% set room = ['kitchen','bedroom'] %}
    {% set appliance = 'livolo' %}
    {% set type = { 'livolo':'switch', 'neoclimaartvogue': 'climate', 'broadlinksp3sboiler':'switch' } %}
    {% set ns = namespace(entities=[]) %}
    {% for rm in room %}
      {% set ns.entities = ns.entities + [type[appliance]~'.'~appliance~'_'~rm] %}
    {% endfor %}
    {{ states | selectattr('entity_id', 'in', ns.entities)
              | map(attribute='entity_id')
              | join(', ') }}

Of course, the last statement doesn’t seem quite what you might ultimately want. I’m guessing it was just an interim debugging test???

BTW, the implementation above uses a Jinja namespace. You can find more details here under “Scoping Behavior.”

1 Like

Thanks. That’s exactly what I wanted to do…
What yo mean by the last statement? This code block?

{{ states | selectattr('entity_id', 'in', ns.entities)
              | map(attribute='entity_id')
              | join(', ') }}

It seems to be a long way to just output the list of entity_id’s that the previous statements created. (I.e., it’s not outputting the states of the entities, just their entity_id’s, which is what ns.entities already contains.)

When user say “Turn off kitchen and bedroom lights” or “Turn off livingroom AC” DialogFlow sends some variables (action, appliance, room). In outpoot I need only list of entity_id’s wich have to bu turned_on or turned_off. Output of ns.entities looks like: [‘switch.livolo_kitchen’, ‘switch.livolo_bedroom’]. This does not fit data_template format. Or I am wrong? After {{ states | selectattr(…. the output looks like switch.livolo_bedroom, switch.livolo_kitchen

Why not just:

{{ ns.entities | join(', ') }}

You’re right. The only advantage of the above method is the ability to check if the appliance is currently on or off. And filter entities according to the desired state