How to prevent this automation triggering at shutdown

automation:

  - alias: Create broadcast available group
    id: Create broadcast available group
    mode: restart
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: *broadcast
    action:
      service: group.set
      data:
        object_id: broadcast_available
        name: Broadcast available
        entities: >
          {{expand('group.broadcast')|rejectattr('state','in',['unknown','unavailable'])
            |map(attribute='entity_id')|join(',')}}

throws an odd error at shutdown, apparently the state of any of the *broadcast entities changes.

Error while executing automation automation.create_broadcast_available_group: Entity ID  is an invalid entity ID for dictionary value @ data['entities']

How can I prevent this from happening at shutdown?

    condition:
      >
        {{{trigger.event_type != 'homeassistant_close'}}}}

to give an idea of what I am after. Cant find any hints on the syntax though, so please help me out? thanks!

btw, I did think of adding it as a trigger and then exclude the trigger.id in the conditions, but there seems to be no ‘not’ condition in the trigger id department either?

Is it actually causing an issue, or are you just targeting zero errors, even at shutdown?
Generally speaking, you might add a condition to the automation that group.broadcast meets some requirements…

tbh, I dont like Errors. at all. even at shutdown/restart, they would indicate an issue in the config.
Added to that, I dont understand what’s happening. Ive tried to keep a log viewer window open in another browser, but not much info was visible.
Upon startup, one can nicely see the group grow with available media_players

why does it say Entity ID is an invalid entity ID …? apparently the template gets evaluated to that, or this is something in the group.set service maybe.

what do you think of? I already have the |rejectattr('state','in',['unknown','unavailable'] . should I also exclude an empty group maybe.

condition:
  >
   {{expand('group.broadcast')|rejectattr('state','in',['unknown','unavailable'])
            |map(attribute='entity_id')|list|count != 0}}

thing is, I am not sure why this is triggering in the first place

No clue.
This is a general thought, not specific to that use case (not idea what is in group.broadcast at any given time), nor to the fact that it only happens on shutdown.

I have something like that in mind, for example (syntax not checked)

...
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: *broadcast
    condition: "{{ expand('group.broadcast') | length > 0 }}"
    action:
      service: group.set
...

(just an example; idk if calling group.set with an empty entities: is valid or not)

yes, Ive just restarted with my template above, and that seems to do the trick. The group.broadcast contains all of my media_players. They can be powered ‘off’ and are unavailable at that time. If I use the group to play announcements and some are unavailable, I get errors… so I made this group.broadcast_available, automatically updating the entities. Works perfectly.

apparently, when calling the homeassistant restart, these players turn unavailable, and trigger the automation (which is what is supposed to happen). But since they all turn unavailable the service group.set returns the error it does.
I need to exclude the unavailable entities from the group, otherwise it will still trigger :wink:

thanks for pointing me to another direction!