thomasloven / lovelace-auto-entities

🔹Automatically populate the entities-list of lovelace cards
MIT License
1.32k stars 119 forks source link

auto-entities

hacs_badge

Automatically populate lovelace cards with entities matching certain criteria.

For installation instructions see this guide.

Usage

type: custom:auto-entities
card: <card>
card_param: <card_param>
entities:
  - <entity>
  - <entity>
filter:
  template: <template>
  include:
    - <filter>
    - <filter>
  exclude:
    - <filter>
    - <filter>

show_empty: <show_empty>
unique: <unique>
sort: <sort_method>

Options

Filters

The two main filter sections include and exclude each takes a list of filters.

Filters have the following options, and will match any entity fulfilling ALL options:

Special options:

Template filter

The filter section template takes a jinja2 template which evaluates to a list of entities or entity objects.

How it works

auto-entities creates a list of entities by:

  1. Including every entity given in entities: (this allow nesting of auto-entitiesif you'd want to do that for some reason...)
  2. Include every entity listed in a filter.template evaluation
  3. Include all entities that matches ALL options of ANY filter in the filter.include section. The same entity may be included several times by different filters.
  4. Remove all entities that matches ALL options on ANY filter in the filter.exclude section.

It then creates a card based on the configuration given in card:, and fills in entities: of that card with the entities from above.

Matching rules

Wildcards

Any filter option can use * as a wildcard for string comparison. Note that strings must be quoted when doing this:

filter:
  include:
    - name: "Bedroom *"
    - entity_id: "sensor.temperature_*_max"

Regular expressions

Any filter option can use javascript Regular Expressions for string comparison. To do this, enclose the regex in /. Also make sure to quote the string:

filter:
  include:
    - name: "/^.* [Ll]ight$/"
    - entity_id: "/sensor.temperature_4[abd]/"

Numerical comparison

Any filter option dealing with numerical quantities can use comparison operators if specified as a string (must be quoted):

filter:
  include:
    - attributes:
        battery: "<= 50" # Attribute battery_level is 50 or less
    - state: "> 25" # State is greater than 25
    - attributes:
        count: "! 2" # Attribute count is not equal to 2
    - state: "= 12" # State is exactly 12 (also matches "12", "12.0" etc.)
    - state: 12 # State is exactly 12 but not "12"

Time since an event

Any filter option dealing with an event time can filter entities by time elapsed since that event:

filter:
  include:
    - attributes:
        last_seen: "> 1h ago" # Entity was seen more than 1 hour ago
    - last_updated: "< 20m ago" # Entity was updated less than 20 minutes ago
    - last_triggered: "> 1d ago" # Entity was triggered more than 1 day ago

All the numeric comparison operators are available.

Repeating options

Any option can be used more than once by appending a number or string to the option name:

filter:
  include:
    - state 1: "> 100"
      state 2: "< 200"

The filter above matches entities where the state is above 100 AND below 200. Compare to the following:

filter:
  include:
    - state: "< 100"
    - state: "> 200"

The two filters above together match entities where the state is below 100 OR above 200.

Object attributes

Some entity attributes actually contain several values. One example is hs_color for a light, which has one value for Hue and one for Saturation. Such values can be stepped into using keys or indexes separated by a colon (:):

filter:
  include:
    - attributes:
        hs_color:1: ">30"

The example above matches lights with a hs_color saturation value greater than 30.

Stringification

Some entity attributes are not text strings, but can be advanced structures. By starting the pattern to match with $$ auto-entities will convert the attribute to JSON before comparing:

filter:
  include:
    - attributes:
        entity_id: "$$*"

The example above matches any entity that has a entity_id attribute - i.e. all kinds of group entities.

Sorting entities

Entities can be sorted, either on a filter-by-filter basis by adding a sort: option to the filter, or all at once after all filters have been applied using the sort: option of auto-entities itself.

Sorting methods are specified as:

sort:
  method: <method>
  reverse: <reverse>
  ignore_case: <ignore_case>
  attribute: <attribute>
  first: <first>
  count: <count>
  numeric: <numeric>
  ip: <ip>

Entity options

In the options: option of the filters, the string this.entity_id will be replaced with the matched entity_id. Useful for service calls - see below.

Examples

Show all entities, except yahoo weather, groups and zones in a glance card:

type: custom:auto-entities
card:
  type: glance
filter:
  include: [{}]
  exclude:
    - entity_id: "*yweather*"
    - domain: group
    - domain: zone

Show all gps device_trackers with battery level less than 50:

type: custom:auto-entities
card:
  type: entities
  title: Battery warning
filter:
  include:
    - domain: device_tracker
      options:
        secondary_info: last-changed
      attributes:
        battery: "< 50"
        source_type: gps

Show all lights that are on:

type: custom:auto-entities
show_empty: false
card:
  type: glance
  title: Lights on
filter:
  include:
    - domain: light
      state: "on" # Remember that "on" and "off" are magic in yaml, and must always be quoted
      options:
        tap_action:
          action: toggle

Also show all lights that are on, except the hidden ones:

type: custom:auto-entities
show_empty: false
card:
  type: entities
  title: Lights on
  show_header_toggle: false
filter:
  include:
    - domain: light
  exclude:
    - state: "off"
    - state: "unavailable"
    - hidden_by: "user"

Show everything that has "light" in its name, but isn't a light, and all switches in the living room:

type: custom:auto-entities
card:
  type: entities
  title: Lights on
  show_header_toggle: false
filter:
  include:
    - name: /[Ll]ight/
      not:
        domain: light
    - type: section
    - domain: switch
      area: Living Room

List every sensor belonging to any iPhone:

type: custom:auto-entities
card:
  type: entities
  title: Phones
  show_header_toggle: false
filter:
  include:
    - device: /iPhone/

List the five last triggered motion sensors:

type: custom:auto-entities
card:
  type: entities
filter:
  include:
    - domain: binary_sensor
      attributes:
        device_class: motion
sort:
  method: last_changed
  reverse: true
  count: 5

Put all sensors in individual entity cards in a grid card:

type: custom:auto-entities
card:
  type: grid
card_param: cards
filter:
  include:
    - domain: sensor
      options:
        type: entity

Turn on scenes by clicking them:

type: custom:auto-entities
card:
  type: glance
filter:
  include:
    - domain: scene
      options:
        tap_action:
          action: call-service
          service: scene.turn_on
          service_data:
            # Note the magic value this.entity_id here
            entity_id: this.entity_id

Example using templates:

type: custom:auto-entities
card:
  type: entities
filter:
  template: |
    {% for light in states.light %}
      {% if light.state == "on" %}
        {{ light.entity_id}},
      {% endif %}
    {% endfor %}

Or:

template: "{{states.light | selectattr('state', '==', 'on') | map(attribute='entity_id') | list}}"

Buy Me A Coffee