Help to get working... multiple string exclusions from entity_id and name

Code for Single exclusion works fine. I tried multiple loop methods but cannot get the code to do multiple exclusions no matter how I split the list or if I make the list separate lines (preferred - easier to read and not fat finger) or comma delimited… sometimes it just ignores the exclusions, other times it excludes everything (output empty list)…

@petro thx for offering the help!

to get Links to…

v11 working, single string exclusion… Offline Devices Report v11 - workin — Codefile

v12 non-working, multi exclusions… Offline Devices Report v12 - test — Codefile

Post the code, excluding entities depends on the code in question and the selectors that are used in the blueprints. I mentioned this to you in another thread and you glossed over the response. If you want help with it, I can help you but I need that information.

if entities are a list, and that blueprint is what you’re going off of, all you need is:

    {% for switch in states.switch | rejectattr('entity_id', 'in', exclude_entities) %}

for each for loop that you do.

If exclude_entities is a list or a string, you need to account for that ahead of time with:

  {% set exclude_entities = exclude_entities if exclude_entities is list else [exclude_entities] %}

or

  {% set exclude_entities = [exclude_entities] if exclude_entities is string else exclude_entities %}

Ok, I see what you’re trying to do.

This is good

    {% set exclude_patterns = exclude_strings.split('\n') | map('trim') | reject('eq', '') | list if exclude_strings is defined and exclude_strings != "" else [] %}

But it will be hard to use. Change it to this instead:

    {% set exclude_regex = exclude_strings.split('\n') | map('trim') | reject('eq', '') | list | join('|') if exclude_strings is defined else 'abcdefghijklmnopqrstuvwxyz' %}
    

Then add this to each loop

    {% for binary_sensor in states.binary_sensor | rejectattr('entity_id', 'search', exclude_regex)

It will reject things contained in your entity_id’s, if it’s not provided it will default to reject the literal string abcdefghijklmnopqrstuvwxyz which is really unlikely to exist in any entity_id.

2 Likes

do you mean like this (inside switch & binary_sensor loops)? …

    {% for binary_sensor in states.binary_sensor 
    | selectattr('attributes.device_class', 'defined') 
    | selectattr('attributes.device_class', '==', 'battery') %}
    | rejectattr('entity_id', 'search', exclude_regex)

all the for loops you have, so that one and the rest. But put it first, if you have it last, make sure you move the terminator

still ignores the exclusion strings… Offline Devices Report v15-Petro — Codefile

post the trace showing it is

correction (I had my other test version still in there)… your version is not running at all, just Logs an error…

Logger: homeassistant.components.automation.offline_devices_report
Source: components/automation/__init__.py:663
integration: Automation (documentation, issues)
First occurred: 7:22:37 AM (9 occurrences)
Last logged: 1:39:05 PM

Error rendering variables: TemplateRuntimeError: No test named 'string_template_match' found.
Error rendering variables: TypeError: 'in <string>' requires string as left operand, not LoggingUndefined
Error rendering variables: PatternError: nothing to repeat at position 0

that is missing from the template completely, are you sure you’re running that version?

I need to see the latest error, and there will be a trace associated with it as well.

that is the exact code I’m running (see last link)

And I reloaded Automation’s YAML twice… no clue why that error is what it is .

it says 9 times, are you sure that’s the latest error? View the raw logs, not the pretty fancy logs.

Lastly, I’ve said trace 2 times now, post the trace. I’m not saying this for my own health. Post the automation trace.

there is no trace… it is not running

Then how are you certain it’s not working? You’d have different errors if the automation wasn’t loading.

I’ll try restarting HA completely… I’ve heavily mod’d a few blueprints without any issues. When I’ve tried to have multi-string (more than one) excludes or even single label excludes in any of them it always fails. I’ve seen others posting they were never able to get labels working.
This is what is frustrating… dead simple if I used VB, JS… but these templates are a nightmare for this stuff.

Look in your logs for errors. The raw text logs. It really isn’t hard.

You’ll have an error when you try to load if the configuration is wrong.

Or

You’ll have an error when the automation runs, which will have a trace & an error in the logs.

There are no other possibilities for errors. Just those 2 scenarios.

The hardest part about this is that you aren’t looking in the correct places. And that’s just learning where to look.

ok, fair, I had no clue you were talking about something else. ‘Raw Log’ shows this…

e[31m2025-05-09 13:13:26.586 ERROR (MainThread) [homeassistant.components.automation.offline_devices_report] Error rendering variables: PatternError: nothing to repeat at position 0e[0m

And can you show the automation configuration in yaml. Or download the trace and post the JSON here. I don’t see how an error like that could even be produced if it wasn’t running the automation.

Well, I loaded your blueprint. You’re mixing tabs and spaces, you need to choose one or the other not both. So replace all tabs with 4 spaces.

It also looks like you left a bunch of left over code in there that’s also causing exceptions left and right. I’m getting the following errors:

2025-05-09 16:20:00.335 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'exclude_patterns' is undefined when rendering '{% set exclude_entities = exclude_entities if exclude_entities is list else [exclude_entities] %} {% set exclude_regex = exclude_strings.split('\n') | map('trim') | reject('eq', '') | list | join('|') if exclude_strings is defined else 'abcdefghijklmnopqrstuvwxyz' %} {% set result = namespace(offline_devices=[]) %} {% for sensor in states.sensor  | rejectattr('entity_id', 'search', exclude_regex) | selectattr('attributes.device_class', 'defined')  | selectattr('attributes.device_class', '==', 'battery') %}

and

2025-05-09 16:20:00.338 ERROR (MainThread) [homeassistant.components.automation.offline_devices_report_v15_petro] Error rendering variables: TypeError: 'in <string>' requires string as left operand, not LoggingUndefined