I have taken the work by jazzyisj and messed about with it a bit.
I wanted to be able to include ‘child’ entities without having to hard code them and I took an approach which works but I suspect now relies on a bug in the group
services.
The way I do it is to include in the ‘Ignored Entities’ group a dummy ‘pattern’ entity such as:
pattern.heating
So that all entities containing the string ‘heating’ get added to the Child Group by an automation
like this:
- service: group.set
data:
object_id: unavailable_entities_children
entities: >
{% set ns = namespace(child_entities = '') %}
{% set ignored = state_attr('group.unavailable_entities_ignored', 'entity_id') | list %}
{%- for item in ignored %}
{% if item.split('.')[0] == 'pattern' %}
{%- set child_pattern = item.split('.')[1] %}
{%- set ns.child_entities = ns.child_entities ~ states | selectattr('entity_id', 'search', child_pattern) | map(attribute='entity_id') | join(', ') %}
{%- if not loop.last %}
{%- set ns.child_entities = ns.child_entities ~ ', ' %}
{%- endif %}
{% endif %}
{% endfor %}
{{ ns.child_entities }}
So far so good, but then I wanted to be able to dynamically add entities to the 'Ignored Entites` group based on the state of another entity.
For example, if I had a master control input_boolean for my irrigation and that boolean is turned to ‘off’ then all entities with the string ‘irrigation’ should be added to the ‘Ignored Entities’ group. Again, so far so good, I do that this way:
- service: group.set
data:
object_id: unavailable_entities_ignored
add_entities: >
{% if is_state('input_boolean.irrigation_master_control_switch', 'off') and
'esphome_irrigation_controller' not in state_attr('group.unavailable_entities_ignored', 'entity_id') %}
{{ states | selectattr('entity_id', 'search', 'esphome_irrigation_controller') | map(attribute='entity_id') | join(', ') }}
{% endif %}
If you’re still with me, here comes the interesting bit.
I then wanted to remove those entities if the boolean is turned back on. The following should work (and kind of does):
{%- set entities = state_attr('group.unavailable_entities_ignored', 'entity_id') | list %}
{%- for entity in entities if 'esphome_irrigation_controller' not in entity %}
{{ entity }}{%- if not loop.last%},{% endif %}
{%- endfor %}
It returns exactly the list I am looking for.
However whilst the dummy pattern.some_string
entitiy_ids are added to the group, whenever HA looks at that group it seems to throw an error for invalid entity_ids. Even though the dummy patterns which are hard coded do not cause a problem.
I’m not sure I expect anyone to actually have read this far as I realise this is all very esoteric and probably not at all easy to follow but I thought it might interest some of the usual suspects (e.g. petro, 123, Mariusthvdb and of course jazzyisj ) when it come to (more advanced?) templating.
And yeah… in all likelihood what I am doing could be done better… I am all ears if that is the case.