Unwanted triggering of automation after reload Groups

Guys, I have a template sensor based on a group and I use the state of this sensor in several automations. My problem is unwanted firing of automation after clicking Reload Groups button in Developer tools > YAML. The template sensor temporarily changes the value to 0. In other words, it changes the value and triggers automation :frowning:

I thought there was some group_reloaded event, but apparently there isn’t or I didn’t look hard enough - that would help.

Here is an example where the automation is triggered even if the sensor state sensor.a_group_based_sensor is greater than 0.

group:
  example_people_group:
    entities:
      - person.john
      - person.janett

template:
- sensor:
  - unique_id: a_group_based_sensor
    name: A group based sensor
    state: >
      {% set people_at_home = expand('group.example_people_group') %}
      {{ people_at_home | selectattr('state','eq','home')|list|count }}

automation:
- id: example_of_some_automation
  alias: "An Example"
  mode: single
  initial_state: true
  trigger:
    - platform: state
      entity_id: sensor.a_group_based_sensor
  variables:
    people_at_home: >
      {{ states('sensor.a_group_based_sensor') | int(default=0) }}
  condition:
  - condition: template
    value_template: "{{ people_at_home == 0 }}"
  action:
  - service: tts.google_cloud_say
    target:
      entity_id:
        - media_player.google_nest_hub
    data_template:
      cache: false
      message: "You don't want to hear this message when you click the reload groups button."  

I’m looking for an elegant solution and would be glad if someone can help.

Well. I don’t think you needt hat sensor at all. The zone.home will report the number of people home if that works for you?

I understand your point, but people at home was just an example to demonstrate the problem.

My general point was about preventing automation from running when groups are reloaded and at the same time one of the groups is a source for a template sensor.

i see. This did get me thinking and I found this which was similar.

I was going to suggest availability_template on that sensor… It is a challenging problem. That wont solve your problem but thought it was a good read to spark thought around your issue.

@calisro Thanks for the tip ! Defining the availability for the template sensor helped! I’m still missing event like group_reloaded, however add availability to template sensor solves my problem. Thanks again!

template:
- sensor:
  - unique_id: a_group_based_sensor
    ...
    ...
    availability: >
      {{ states('group.example_people_group') not in ['unknown', 'unavailable'] }}

Here is my edit:

group:
  example_people_group:
    entities:
      - person.john
      - person.janett

template:
- sensor:
  - unique_id: a_group_based_sensor
    name: A group based sensor
    state: >
      {% set people_at_home = expand('group.example_people_group') %}
      {{ people_at_home | selectattr('state','eq','home')|list|count }}
    availability: >
      {{ states('group.example_people_group') not in ['unknown', 'unavailable'] }}

automation:
- id: example_of_some_automation
  alias: "An Example"
  mode: single
  initial_state: true
  trigger:
    - platform: state
      entity_id: sensor.a_group_based_sensor
  variables:
    people_at_home: >
      {{ states('sensor.a_group_based_sensor') | int(default=0) }}
    pass: >
      {{ states('sensor.a_group_based_sensor') not in ['unknown', 'unavailable'] }}
  condition:
  - condition: template
    value_template: "{{ pass and people_at_home == 0 }}"
  action:
    - ...