Exclude entity from template sensor

How can I exclude the entity “lock.tesla_door_lock” from the following template sensor?

- platform: template
  sensors:
    unlocked_door_count:
      value_template: >-
        {{ states.lock | selectattr('state', 'eq', 'unlocked') |  list | count }}

|rejectattr('entity_id', 'eq', 'lock.tesla_door_lock')

2 Likes

Thank you! If I wanted to exclude multiple entities (in the future), could I list several?

For example:

|rejectattr('entity_id', 'eq', 'lock.tesla_door_lock', 'lock.tesla_door_lock_2')

You could use regex to match a pattern.

I’m pretty sure you can also do this:

| rejectattr('entity_id','in',('lock.tesla_door_lock','lock.tesla_door_lock2','lock.tesla_door_lock3','lock.tesla_door_lock4'))

1 Like

|rejectattr('entity_id', 'search', '(tesla_door_lock)')

3 Likes

search… Hadn’t seen that. Learn new things!

There’s also match which starts matching from the beginning of the string (search matches anywhere in the string unless you provide it with a regex directive to start at the beginning). It can provide fine-grained control when selecting/rejecting entities.

For example, this will not reject sensor.foo_tesla_door_lock because its object_id begins with foo_ as opposed to tesla_.

| rejectattr('object_id', 'match', 'tesla_door_lock')

However, it’s possible to do the same thing using search by simply enhancing the regex pattern:

| rejectattr('object_id', 'search', '^tesla_door_lock')

The ^ symbol means the following string must appear at the beginning.

Note that because it’s using object_id, instead of entity_id, it is acting on a broader range of entity domains (sensor, binary_sensor, lock, climate, etc).

2 Likes

Thank you so much for this! Was exactly what i was looking for! :smiley:

1 Like