SOLVED: Exclude entities from a variable

Hi all,

I have a automation that is working but want exclude some things:

{% set ns = namespace(battery=[]) %}
{% for s in states.sensor if 'battery_level' in s.entity_id %}
  {% set ns.battery = ns.battery + [s] %}
{% endfor %}
{{ ns.battery | map(attribute='entity_id') | list }}

Result:
['sensor.bathroom2_battery_level', 'sensor.bathroom_battery_level', 'sensor.bedroom_battery_level', 'sensor.bedroom_door_battery_level', 'sensor.diningroom_battery_level', 'sensor.diningroom_door_battery_level', 'sensor.frondoor_door_battery_level', 'sensor.galaxy_tab_a_battery_level', 'sensor.hallway_battery_level', 'sensor.imac_door_battery_level', 'sensor.kitchen_battery_level', 'sensor.livingroom_battery_level', 'sensor.martine_mobile_battery_level', 'sensor.nest_protect_bedroom_battery_level', 'sensor.nest_protect_dining_room_battery_level', 'sensor.nest_protect_living_room_battery_level', 'sensor.ns_telefoon_battery_level', 'sensor.peter_iphone_battery_level', 'sensor.sm_t510_battery_level', 'sensor.toilet_battery_level']

I want exclude in the automation (whole automation below):

sensor.ns_telefoon_battery_level
sensor.sm_t510_battery_level
sensor.martine_mobile_battery_level
value_template: "{{ states(battrery_current) not in ['martine_mobile','galaxy_tab','sm_t510','ns_telefoon'] }}"

But seems its not exclude the entities. Can someone assist me?

- id: "f152dde2-643e-4269-bd7e-d5a11ba98a68"
  alias: "Notification - Low battery nodes"
  trigger:
    - platform: time
      at: "17:00:00"

  variables:
    min_battery_level: 50
    battery_ids: ""
    battrery_current: ""

  condition: []
  
  action:
    - variables:
        battery_ids: >
          {% set ns = namespace(battery=[]) %}
          {% for s in states.sensor if 'battery_level' in s.entity_id %}
            {% set ns.battery = ns.battery + [s] %}
          {% endfor %}
          {{ ns.battery | map(attribute='entity_id') | list }}

    - alias: Repeat

      repeat:
        count: "{{ battery_ids | length }}"

        sequence:
          - variables:
              battrery_current: "{{ battery_ids[repeat.index - 1] }}"
          - condition: template
            value_template: "{{ states(battrery_current) not in ['iphone','ipad','martine_mobile','galaxy_tab','sm_t510','ns_telefoon'] }}"

          - condition: template
            value_template: "{{ 0 < (states(battrery_current) | float) < (min_battery_level | float ) }}"

          - service: script.mobile_notify_no_actionable
            data:
              title: "Battery level low"
              message: "Battery level of {{ state_attr(battrery_current,'friendly_name') }}: {{ states(battrery_current) }}%!"
              thread_id: "system_notification"

Copy-paste the following template into the Template Editor and confirm it reports a list containing only the entity_ids you want.

{{ states.sensor
  | selectattr('object_id', 'search', 'battery_level')
  | rejectattr('object_id', 'match', '(martine_mobile|galaxy_tab|sm_t510|ns_telefoon)')
  | map(attribute='entity_id') | list }}

NOTE

Both the search and match filters accept regular expressions (regex).

  | rejectattr('object_id', 'match', '(martine_mobile|galaxy_tab|sm_t510|ns_telefoon)')
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                                   this is a regex pattern
1 Like

Damn thats a great part of code!

found this site. is that a good one to learn some more options?:
https://jinja.palletsprojects.com/en/3.0.x/templates/#list-of-builtin-filters

1 Like

I guess this will be the automation:

- id: "f152dde2-643e-4269-bd7e-d5a11ba98a68"
  alias: "Notification - Low battery nodes"
  trigger:
    - platform: time
      at: "17:00:00"

  variables:
    min_battery_level: 50
    battery_ids: ""
    battrery_current: ""

  condition: []
  
  action:
    - variables:
        battery_ids: >
          {{ states.sensor
           | selectattr('object_id', 'search', 'battery_level')
           | rejectattr('object_id', 'match', '(martine_mobile|galaxy_tab|sm_t510|ns_telefoon)')
           | map(attribute='entity_id') | list }}

    - alias: Repeat

      repeat:
        count: "{{ battery_ids | length }}"

        sequence:
          - variables:
              battrery_current: "{{ battery_ids[repeat.index - 1] }}"

          - condition: template
            value_template: "{{ 0 < (states(battrery_current) | float) < (min_battery_level | float ) }}"

          - service: script.mobile_notify_no_actionable
            data:
              title: "Battery level low"
              message: "Battery level of {{ state_attr(battrery_current,'friendly_name') }}: {{ states(battrery_current) }}%!"
              thread_id: "system_notification"
1 Like