Help getting list of sensor entities for script loop template

Hello!

I am really struggling with being able to get the list of entities to use with my Tornado Warning automation. Currently it works by me defining each ledeffect sensor manually in the automation. I want to make this smarter because I still have about 20 light switches to add to my house and I would like the automation to simply recognize them as they are onboarded into HA.

If I run the below I get the list of sensors I want. But it is messy and I am sure there is a cleaner way to do it. I am also not sure how to get this list available for use in a script for each loop.

{% set data = namespace(matches=[]) %}
{% for item in integration_entities('hubitat') | unique | list %}
  {% if item | regex_match('(?:sensor).+(?:ledeffect)') == true %}
    {% set data.matches = data.matches + [item] %}
  {% endif -%} 
{%- endfor%}
{{ data.matches }}

Running the below returns a list of ALL sensors, not just the ledeffect ones. I am really confused as why the regex works with regex_match, but it is not working with regex_findall.

 {{integration_entities('hubitat') | regex_findall('(?:sensor).+(?:ledeffect)') | unique | list }}

Does anyone have any pointers, tips, or tricks to point me in the right direction? I’ve struggled with this for a few days now and I am throwing in the towel.

Thank you!

try this format:

{{ integration_entities('hubitat') | expand | selectattr('entity_id', 'search', '(?:sensor).+(?:ledeffect)') | map(attribute='entity_id') | list }}

i didn’t test your regex, but the point is that selectattr with ‘search’ does the regexp. then you use map to distill that to entity_id names that you put into a list and can use in loops, or entity_id: fields, or whatever.

i’m not totally clear on what your regex is trying to do for the ledeffect, but if you’re trying to find all sensors with ledeffect in the entity name (e.g. sensor.foo_ledeffect), perhaps the regex should be this instead:

'sensor\..*ledeffect'

if you meant something else, holler

Thank you for the quick reply!

The Regex worked the way I had it posted and also using what you posted in your reply.

Your format with selectattr worked as well. I ended up needed to use states instead of integration_entities(‘hubitat’) to get the initial list of entities though. I think that might have been what was throwing me off from the start. I really appreciate the help! I was hitting my head on the table over this one.

{{ states | selectattr('entity_id', 'search', '(?:sensor).+(?:ledeffect)') | map(attribute='entity_id') | list }}

using integration_entities('hubitat') can also work, but need to expand (added above)

i don’t have hubitat, so i’m presuming it’s the right name for that integration.

Good point about hubitat. I did confirm that that command was right by using the below to make sure I got output. :slight_smile:

{{integration_entities('hubitat') | unique | list }}

Anyway, adding expand to the command did what I was expecting it to do. I have marked your original reply as the solution.

Is there documentation that explains how the various commands calls behave? I can’t find any explicit documentation using google. I can learn about the individual pieces if I google the words, but I am hoping for a more complete document so I can be more self sufficient.

Thank you for your help this evening!

unfortunately i don’t know of one document that is really comprehensive. in fact a very large % of what i know has come from participating in this forum. and often seeing how other people solve the same problem in different ways.

i presume you know this one though:
https://jinja.palletsprojects.com/en/3.1.x/

but it doesn’t include all the home assistant nuances/specifics.

playing around with your original question to help answer the “why it didn’t work” (since i never answered that and i just gave you a different solution)…

here’s what i’m discovering.

this:

{{integration_entities('hubitat') | regex_findall('(?:sensor).+(?:ledeffect)') | unique | list }}

didn’t actually match all of them… what i think it ended up doing was saying "yes, i see a match in that whole array, then returning it as a 1 element string that looked like there was an array inside that had all entities.

ie, the result i got was
[" [ ‘entity.entity1’, 'entity.entity2"]

look really carefully, it is actually a 1 element array with a long string that matched up to the end of your regexp match. it is not an array of entities.

also if you had used states instead of integration_entities(‘hubitat’), you would not be able to use regex_findall on it. since regex_findall work on strings, and states returns a template of all entities, not the string name of entities… you could do add map(attribute=‘entity_id’) and then try to do regex_findall on it… but i think you end up hitting the problem above where it stringifies the entire array and matches it as one long string.

anyways… fyi in case you care… dorked around primarily for my own learning. cheers.

The Templating doc does that:

and both are linked from Developer Tools / Template.

But what that doesn’t include is all of the Python methods and properties that you can apply to objects — that often comes down to a bit of Python knowledge, searching for ways to achieve individual steps of your algorithm in Python, and trial & error in the template editor. Not all methods are available, for safety reasons.

1 Like