Help by sensor template

THANK YOU @ALL

Now everything Works Well. THANK YOU

EDIT

i found a problem, i generate with this template for windows

 {{ states|selectattr('entity_id', 'in', state_attr('group.open_windows',
    'entity_id'))|selectattr('state', 'equalto',
    'on')|map(attribute='entity_id')|join(', ')}}

when i check the template there is no entry on the right

here my group

open_windows:
    name: windows_open
    all: true
    icon: mdi:window-closed-variant
    entities:
       - binary_sensor.fenstersensor_badezimmer
       - binary_sensor.fenstersensor_toilette
       - binary_sensor.fenstersensor_kinderzimmer
       - binary_sensor.fenstersensor_schlafzimmer
       - binary_sensor.fenstersensor_wohnzimmer

now are all windows closed, but before the toilette window was open, and when i now check the generated group “group.devices_open_windows” there is the last entery still there. he doesnt delete the entry when the window is closed

here a example (i choose for the picture doors but its the same code above)
here you see he shows me the door is open, but its closed, here must be no entry

error

@petro

have you perhaps a idea?

Your open windows template filters only binary_sensors that state is 'on'. If all windows are closed the template editor shows the right result: nothing.

What triggers your dynamic group? Please post your automation.

hi,

  1. correct thats right, but why i had then a entity in my custom:button:card (dynamic group) when i closed the door? when close the window there should be no entry any more.

  2. of course here, my automation gets triggered when i OPEN and when i CLOSE the window.based on this :

- platform: template
    sensors:
      count_windows_open:
        friendly_name: "Anzahl geoeffneter Fenster"
        icon_template: mdi:window-closed-variant
        value_template: "{{ expand('group.open_windows') | selectattr('state', 'eq', 'on') | list | count }}"

the count of the windows that are open works well, when i open one window it goes to “1” when i close to “0”

automation :

alias: Update windows open  Group
description: ''
trigger:
  - platform: state
    entity_id: sensor.count_windows_open
condition: []
action:
  - service: group.set
    data:
      object_id: devices_open_windows
      name: Open windows
      entities: >
        {{ states|selectattr('entity_id', 'in', state_attr('group.open_windows',
        'entity_id'))|selectattr('state', 'equalto',
        'on')|map(attribute='entity_id')|join(', ')}}
mode: single

I’ve made the experience that auto-generated groups are only reliably triggered when trigger is platform: state followed by a hard-coded list:

  trigger:

  - platform: state
    entity_id:
    - light.…
    - media_player.…
    - binary_sensor.…

or


trigger:

  - platform: state
    entity_id:
    - group.automatic_generated_1
    - group.automatic_generated_2

Regarding to the second example, I created 2 auto-generated groups which member serve as trigger entities.

hi,

perhaps i missunderstand you, but he triggers the automation with the auto generated group perfekt everytime i open or close a window / door.

mean you this?

i think my problem is another, he saves the last opened window , here look, i opened the front door and closes it again.
I manually started the Automation, but the same behavior

but my auto generated group has this as entity :

entity_id:
  - binary_sensor.tursensor_haustuere
order: 3
auto: true
friendly_name: Open doors

he still have the binary_sensor.tursensor_haustuere saved, but this is wrong. because its closed again.

is it possible that my template code for the auto generated group is not correct?
that when he has no open entity he takes the last that opened?

perhaps he must reset the entity or group when no entity with the state ON is available?

here is my code for the auto generated groups :

{{ states|selectattr('entity_id', 'in', state_attr('group.open_windows',
    'entity_id'))|selectattr('state', 'equalto',
    'on')|map(attribute='entity_id')|join(', ')}}

i tried this version too

{{ expand('group.open_windows') | selectattr('state', 'eq', 'on') |
    map(attribute='entity_id') | list | join(', ') }}

I meant the dynamic changing of group entities. I also wanted only to see entities that currently are on and no as seen in your picture above. I only achieved this by using a state trigger.

okay i understand.

now i must change the trigger of the auto generated group to :

trigger:

  - platform: state
    entity_id:
    - binary_sensor.fenstersensor_badezimmer
    - binary_sensor.fenstersensor_toilette
    - binary_sensor.fenstersensor_kinderzimmer
    - binary_sensor.fenstersensor_schlafzimmer
    - binary_sensor.fenstersensor_wohnzimmer

correct or?

EDIT

I tested it, the same problem. He saves the last opened entity in the dynamic group and shows a closed door in the dynamic group.

I was afraid of that, because the automation was also triggered before that.

i think it’s because of the template of the automation how i display the open windows.

Is there no way that if there is no open window, that he deletes or resets the content of the group?

{{ states|selectattr('entity_id', 'in', state_attr('group.open_windows',
    'entity_id'))|selectattr('state', 'equalto',
    'on')|map(attribute='entity_id')|join(', ')}}

EDIT2

i found a problem, when i run the automation with the code above and NO window is opened he stops the automation with a error.

Entity ID is an invalid entity ID for dictionary value @ data[‘entities’]"

because he found no entity with state ON?
thats my problem i think.

EDIT3

I played a little bit with the code for the low batterys, and this is what i get, i dont know if gives a better way, but the problem when no window is open is gone

{% set ns = namespace(ret=[]) %}
        {% for s in states.binary_sensor
         | selectattr('entity_id', 'in', state_attr('group.open_doors',
    'entity_id'))|selectattr('state', 'equalto',
    'on') 
         %}
        {% set ns.ret = ns.ret + [ s.entity_id ] %}            
        {% endfor %}
        {{ ns.ret }}

What if you use only |list instead?

I just created a sensor for all the relevant devices I want to monitor.


  trigger:

  - platform: template
    value_template: |
          {{ expand(area_entities('Diele'), area_entities('Schlafzimmer'), area_entities('Küche'), area_entities('HWR'),  area_entities('Keller'),  area_entities('Büro'),  area_entities('Wohnzimmer'), area_entities('Loggia')), area_entities('garten'), area_entities('eingang'), 
            |selectattr('domain', 'in', ['binary_sensor', 'light', 'media_player', 'switch'])
            |selectattr('state', 'in', ['on', 'playing'])
            |rejectattr('attributes.is_hue_group', 'true')
            |rejectattr('attributes.entity_id', 'defined')
            |list
            |count
          }}

does not change dynamically the group members when they are turned off, but remain in the group with the status ‘off’.

However, if I make a sensor from the above template and use it as state trigger, I can confirm, that everything works as desired.

1 Like

You can’t update a group to contain 0 entities. This is the problem you’re running into.

1 Like

okay, but with this code it works well.
i checked it, when no entity is there the dynamic group is empty

{% set ns = namespace(ret=[]) %}
        {% for s in states.binary_sensor
         | selectattr('entity_id', 'in', state_attr('group.open_doors',
    'entity_id'))|selectattr('state', 'equalto',
    'on') 
         %}
        {% set ns.ret = ns.ret + [ s.entity_id ] %}            
        {% endfor %}
        {{ ns.ret }}

here the dynamic group with no open doors / windows

entity_id: []
order: 5
auto: true
friendly_name: Open windows

that template is alittle over compliciated

{{ expand('group.open_doors') | selectattr('state', 'equalto','on') | map(attribute='entity_id') | list }}

If that works, then maybe the service now works with an empty list. I was under the impression it didn’t.