gmlupatelli / blueprints_repo

GNU General Public License v3.0
21 stars 16 forks source link

Can't exclude all entities linked to an area or device #3

Open gmlupatelli opened 3 years ago

gmlupatelli commented 3 years ago

Can't exclude all the entities linked to an area or device when executing automation from the following blueprints: unavailable_entities_notification and low_battery_notification.

As a workaround, I can expand the areas and the devices into entities inside the target selector.

jgoguen commented 1 year ago

This is what I've done in another blueprint to expand a target selector to a list of entities. The only thing here is it doesn't attempt to deduplicate entries, so if you have a device in the bedroom with two lights and you add the two lights as entities, the bedroom as an area, and the device as a device, you'l end up with the two light entities each added three times. But deduplication shouldn't be hard to add.

At the end of this, all_entities is a list of entity IDs.

variables:
  target: !input target
  all_entities: |
    {% set res = namespace(r=[]) %}
    {% if target.entity_id is defined %}
      {% set ents = [target.entity_id] if target.entity_id is string else target.entity_id %}
      {% set res.r = res.r + ents %}
    {% endif %}
    {% if target.device_id is defined %}
      {% set devices = [target.device_id] if target.device_id is string else target.device_id %}
      {% for dev_id in devices %}
        {% set ents = device_entities(dev_id) %}
        {% set res.r = res.r + ents %}
      {% endfor %}
    {% endif %}
    {% if target.area_id is defined %}
      {% set areas = [target.area_id] if target.area_id is string else target.area_id %}
      {% for t in areas %}
        {% set area_ents = area_entities(t) %}
        {%
          set res.r = res.r + states.sensor
            | rejectattr('state', 'in', ['unavailable', 'unknown', None])
            | selectattr('entity_id', 'in', area_ents)
            | map(attribute='entity_id')
            | list
        %}
      {% endfor %}
    {% endif %}
    {{ res.r }}