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.
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:
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 }}
Good point about hubitat. I did confirm that that command was right by using the below to make sure I got output.
{{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.
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.
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.
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.