Exclude specific entity in data template to prevent binary_sensor.remote_ui in list of unavailable entities

I am using the data_template below to create a notification with a list of entites with status ‘unavailable’ to notify myself if there could be a problem. This works very well, but the entity ‘binary_sensor.remote_ui’ is always unavailable but I do not need a notification of that one. Could anyone help me to exclude a specific entity in the data_template below? Or, any change to remove the ‘binary_sensor.remote_ui’ entity?

data_template:
  title: '*De volgende nodes zitten in de problemen*'
  message: "{% macro offline_state() %} {% set domains = ['light', 'vacuum', 'switch',\
    \ 'sensor', 'binary_sensor', 'zwave', 'lock'] %} {% for domain in domains\
    \ -%} {% for item in states[domain] if ((item.state | lower == \"unavailable\"\
    )) -%} {{ item.domain }} {{ item.name }} {%- if not loop.last %}, {% endif\
    \ -%} {% endfor %} {%- endfor %} {% endmacro %} {{ offline_state() }}  \n"

first, I’d personally use multi line templating so you don’t have to escape your message. Instead of encapsulating everything in quotes, just use the > to indicate multi-line templating.

Second, just add an if statement in your macro

data_template:
  title: '*De volgende nodes zitten in de problemen*'
  message: >
    {% macro offline_state() %}
    {% set domains = ['light', 'vacuum', 'switch',  'sensor', 'binary_sensor', 'zwave', 'lock'] %}
    {% for domain in domains -%}
      {% for item in states[domain] if ((item.state | lower == "unavailable")) -%}
        {% if item.entity_id not in ['binary_sensor.remote_ui'] %}
          {{ item.domain }} {{ item.name }}
        {% endif %}
        {%- if not loop.last %}, {% endif -%} 
      {% endfor %} 
    {%- endfor %} 
    {% endmacro %}
    {{ offline_state() }}

Also, if you don’t need to output the domain name you can shorten this significantly.

data_template:
  title: '*De volgende nodes zitten in de problemen*'
  message: >
    {% set domains = ['light', 'vacuum', 'switch',  'sensor', 'binary_sensor', 'zwave', 'lock'] %}
    {% set filter = ['binary_sensor.remote_ui'] %}
    {{ states | selectattr('domain', in, domains) | rejectattr('entity_id', in, filter) | selectattr('state','eq','unavailable') | map(attribute='name') | list | join(', ') }}
1 Like

Thanks for your help. That version of the data_template looks a lot less ccomplicated then the version I use, nice!

However, there seems to be a issue with the data_template for the one without domain names. Config check of HA accepted it as no problem, but the notification is not send, even if i remote the line with the filter.

The one with domain name works.

My mistake, typos

{% set domains = ['light', 'vacuum', 'switch',  'sensor', 'binary_sensor', 'zwave', 'lock'] %}
{% set filter = ['binary_sensor.remote_ui'] %}
{{ states | selectattr('domain', 'in', domains) | rejectattr('entity_id', 'in', filter) | selectattr('state','eq','unavailable') | map(attribute='name') | list | join(', ') }}
2 Likes

Thanks @petro!

@petro I am not sure what, but something with my new YAML code is not right, specific the message part (all automations in the YAML file after the YAML code below are invalid). Do you have any clue what’s wrong?

  action:
  - service: persistent_notification.create
    data_template:
      title: Entiteiten in de problemen
      notification_id: offline-devices-alert
      message: >
        {% set domains = ['light', 'vacuum', 'switch',  'sensor', 'binary_sensor', 'zwave', 'lock'] %}
        {% set filter = ['binary_sensor.remote_ui'] %}
        {{ states | selectattr('domain', 'in', domains) | rejectattr('entity_id', 'in', filter) | selectattr('state','eq','unavailable') | map(attribute='name') | list | join(', ') }}

can you post the error that you are getting and the following lines?

I was under the assumption that there was a problem because the Visual Studio Code add-on did not show action, services, etc in blue in the automations below the discussed automation anymore:

When deleting the message part, displaying some parts in blue of the following automations where there again:

I guess Visual Studio Code can not handle the message structure fine or something like that, but the automation with the new message structure is working.

Visual studio code is messing up because of your message with the star i believe. I could be wrong there.

Also, that condition is using the wrong quotes in alot of places. Does that condition work?

Also after deleting the star, Visual studio is confused.

Yes, that condition work. Did not created it myself, just copy pasted it so not sure what could be better.

I don’t where people got the idea to use two consecutive single-quote characters '' instead of a single double-quote character " to serve as a delimiter. It does work but in an oddball way.

I have some automations @dale3h helped me with that uses that. It’s funny, if I paste that template in the dev-tools template tool it doesn’t work but it works fine in my automation. I probably should go and edit them for single " but if it ain’t broke…

It’s like someone was using a keyboard without " so they improvised with '' and were pleasantly surprised when it worked. However, it’s not equivalent to " because each ' in '' continues to be handled just like the usual single-quote.

In yaml, '' escapes a single quote. So ', '', /', ", and /" are all the same meaning. Only one that doesn’t work is "".

2 Likes

I’m trying to do nearly the same thing, but I am using the Alexa Custom Component and it creates a two switches for each alexa device that are usually offline. I have several alexa devices, so I end up with 10-12 offline devices that I dont care about.

I was using this and it was fine for me

         {% set offline = states| selectattr('state','eq','unavailable') | map(attribute='name') | join(', ') %}
          The following devices are offline: {{ offline }}

How could I exclude devices with “shuffle” or “repeat” in the name. I dont think I’ll ever use shuffle or repeat for any other switches, so it would be safe to just exclude entities with that word in their names.

Can I set a filter with wildcard where the binary_sensor.remote_ui is below?

        {% set domains = ['light', 'vacuum', 'switch',  'sensor', 'binary_sensor', 'zwave', 'lock'] %}
        {% set filter = ['binary_sensor.remote_ui'] %}
        {{ states | selectattr('domain', 'in', domains) | rejectattr('entity_id', 'in', filter) | selectattr('state','eq','unavailable') | map(attribute='name') | list | join(', ') }}

Thanks

Unfortunately, you can’t do this with filters. You’ll have to make a macro that filters using a for loop. filtering with rejectattr or selectattr doesn’t allow you to compare a partial string inside another string.

{%- macro wildcard(entity_ids, reject) %}
{%- for entity_id in entity_ids %}
{%- if reject not in entity_id %}
{{- entity_id }}{{ '' if loop.last else ',' }}
{%- endif %}
{%- endfor %}
{%- endmacro %}

then to use:

{% set entity_ids = states | selectattr('domain', 'in', domains) | selectattr('state','eq','unavailable') | map(attribute='name') | list %}
{% set entity_ids = wildcard(entity_ids, 'shuffle').split(',') | reject('eq','') %}
{{ wildcard(entity_ids, 'repeat').split(',') | reject('eq','') | join(', ') }}
1 Like

So I put this in the template editor

{%- macro wildcard(entity_ids, reject) %}
{%- for entity_id in entity_ids %}
{%- if reject not in entity_id %}
{{- entity_id }}{{ '' if loop.last else ',' }}
{%- endif %}
{%- endfor %}
{%- endmacro %}
{% set entity_ids = states | selectattr('domain', 'in', domains) | selectattr('state','eq','unavailable') | map(attribute='name') | list %}
{% set entity_ids = wildcard(entity_ids, 'shuffle').split(',') | reject('eq','') %}
{{ wildcard(entity_ids, 'repeat').split(',') | reject('eq','') | join(', ') }}

Is that right? I assume maybe not, haha. Since I do have one device that should have showed as unavailable, if it was working correctly. I’m trying to follow the logic,but have to admit macros and loops get a bit confusing.

If I understand, the macro is going to be used for the last line, whereever “wildcard” is listed? I think the end would be that you’d have a list of all entity ID’s that you could then filter?

Thanks for your time, no rush

edit: I found that if I removed

selectattr('domain', 'in', domains) 

that it works perfectly, but it does make me wonder why?

Because domains isn’t declared. If you made a domains filter like your post, then it would work.

        {% set domains = ['light', 'vacuum', 'switch',  'sensor', 'binary_sensor', 'zwave', 'lock'] %}
2 Likes