Heads up: 2023.6 longer has persistent notifications in states: what to do?

please see Allow listing of notification_id in persistent_notification trigger and vote up?

request for allowing some more flexibility in the persistent_notification platform trigger Automation Trigger - Home Assistant

new challenge, and it must be a templating error I make. I would like to records some history, so added below history attribute, in an effort to ad the last 4 items to that history attribute. This seems to work alright when a notification is added, but, when dismissing them, a longer list of attributes is shown.

dont think it is the trigger, because that fire correctly:

      - platform: persistent_notification
        update_type:
          - added
          - removed

please have a look with me what I am not seeing?

      - unique_id: persistent_notifications_overview
        device_class: timestamp
        state: >
          {{now()}}
        attributes:
          notifications: >
            {% set msgs = this.attributes.get('notifications', []) %}
            {% if trigger.id == 'start' %}
              {{[]}}
            {% elif trigger.update_type == 'added' %}
              {% set new = [{
                  "id": trigger.notification.notification_id,
                  "title": trigger.notification.title,
                  "message": trigger.notification.message,
                  "created_at": trigger.notification.created_at.isoformat() }] %}
              {{msgs + new}}
            {% else %}
              {{msgs|rejectattr('id','eq',trigger.notification.notification_id)|list}}
            {% endif %}
          history: >
            {%- set previous = this.attributes.history|default([]) %}
            {%- set new = [this.attributes.notifications|default('no notification')] %}
            {{previous[-4:] + new}}

this helps:

          history: >
            {%- set previous = this.attributes.history|default([]) %}
            {%- set new = this.attributes.notifications|default('no notification') %}
            {% if trigger.update_type == 'added' %}
            {{previous[-4:] + new}}
            {% else %} {{previous[-4:]}}
            {% endif %}

but still has some quirks. mainly it wont populate history if is a single notification was cleared

the default is adding a list with a string, which is a no-no. make your default a list.

I believe Ive done that now, but also, and that was the reason for the nested, Iā€™ve isolated the single notification

          history: >
            {%- set previous = this.attributes.history|default(['no history']) %}
            {% set new = [{
                "id": trigger.notification.notification_id,
                "title": trigger.notification.title,
                "message": trigger.notification.message,
                "created_at": trigger.notification.created_at.isoformat()}] %}
            {% if trigger.update_type == 'added' %} {{previous[-3:] + new}}
            {% else %} {{previous[-4:]}}
            {% endif %}

because in the notifications attribute, that was already creating a listā€¦

now seems to fare a lot better, though I can find a way to have the un-triggered entity show the default upon restart, of empty lists

thats ok though, because when I add a ā€˜no notificationsā€™ in that list as default, the list isnt empty, and the counter goes off :wink:

Ill leave it at

      - unique_id: persistent_notifications_overview
        device_class: timestamp
        state: >
          {{now()}}
        attributes:
          notifications: >
            {% set msgs = this.attributes.get('notifications',[]) %}
            {% if trigger.id == 'start' %}
              {{[]}}
            {% elif trigger.update_type == 'added' %}
              {% set new =
               [ {"id": trigger.notification.notification_id,
                  "title": trigger.notification.title,
                  "message": trigger.notification.message,
                  "created_at": trigger.notification.created_at.isoformat()} ]%}
              {{msgs + new}}
            {% else %}
              {{msgs|rejectattr('id','eq',trigger.notification.notification_id)|list}}
            {% endif %}
          history: >
            {%- set previous = this.attributes.history|default([]) %}
            {% set new = [{
                "id": trigger.notification.notification_id,
                "title": trigger.notification.title,
                "message": trigger.notification.message,
                "created_at": trigger.notification.created_at.isoformat()}] %}
            {% if trigger.update_type == 'added' %} {{previous[-3:] + new}}
            {% else %} {{previous[-4:]}}
            {% endif %}

for now, and need to experience if this history list is ok.
I then have an attribute with the active notifications, and a history attribute with the last 4.

well, it all seems to go fine, except upon restartā€¦cI dont get that, because retriggering the template by reload templates works just fine. so why does homeassistant start cause trouble:

Template variable error: 'dict object' has no attribute 'notification' when rendering '{%- set previous = this.attributes.history|default([]) %} {% set new = [{ "id": trigger.notification.notification_id, "title": trigger.notification.title, "message": trigger.notification.message, "created_at": trigger.notification.created_at.isoformat()}] %} {% if trigger.update_type == 'added' %} {{previous[-3:] + new}} {% else %} {{previous[-4:]}} {% endif %}'

aarrgh, I need to filter out that just like in the other attributeā€¦ which I thought I had already done tbh, using the

{% if trigger.update_type == 'added' %}

but it was in the wrong placeā€¦ fixed:

          history: >
            {%- set previous = this.attributes.history|default([]) %}
            {% if trigger.update_type == 'added' %}
            {% set new = [{
                "id": trigger.notification.notification_id,
                "title": trigger.notification.title,
                "message": trigger.notification.message,
                "created_at": trigger.notification.created_at.isoformat()}] %}
            {{previous[-3:] + new}}
            {% else %} {{previous[-4:]}}
            {% endif %}

Is anyone getting notifications doubling up since the latest update?

You replied to my post containing an example of how to create ā€œmessagesā€ that are independent of Home Assistantā€™s persistent notifications. Are you saying the example I posted now produces duplicates of each message?

and to answer the question: No all notifications are single at this end.

Sort of, I used janes modification of your post: Setting up a Message Box in Home Assistant ā€“ RJT-Services

Iā€™m ending up with it getting duplicates which is odd as it worked initially.

Ask RJT Services.

Appears to be due to a bug in how version 2023.9.X processes a Template entityā€™s attributes. For more information, see link in pedolskyā€™s post below.

2 Likes

@Krispkiwi

Can confirm the issue. It happens to all of my template sensors that contain a list within an attribute. Iā€™ve raised an issue.

Until the bug is fixed, you can modify the template so that it excludes duplicates. To do that, use the unique filter.

In the following example, the unique filter is used to ensure all messages have a unique value of id. In other words, any message with a duplicate id value is discarded. This only needs to be done when a new message is added (not needed when a message is deleted).

template:
  - trigger:
      - platform: event
        event_type:
          - message_create
          - message_delete
          - message_delete_all
    sensor:
      - name: Messages
        state: "{{ now().timestamp() | timestamp_custom() }}"
        attributes:
          messages: >
            {% set msgs = this.attributes.get('messages', []) %}
            {% if trigger.event.event_type == 'message_create' %}
              {% set new = [{
                  "id": trigger.event.data.id | default('TS' ~ now().timestamp()),
                  "title": trigger.event.data.title | default(''),
                  "message": trigger.event.data.message | default(''),
                  "time": now().isoformat() }] %}
              {{ (msgs + new) | unique(attribute='id') | list }}
            {% elif trigger.event.event_type == 'message_delete' %}
              {% if trigger.event.data.id is defined %}
                {% set msgs = msgs | rejectattr('id', 'eq', trigger.event.data.id) | list %}
              {% elif trigger.event.data.index is defined and trigger.event.data.index | is_number %}
                {% set t = trigger.event.data.index | int(0) - 1 %}
                {% if 0 <= t < msgs|count %}
                  {% set msgs = msgs | rejectattr('id', 'eq', msgs[t].id) | list %}
                {% endif %}
              {% endif %}
              {{ msgs }}
            {% else %}
              {{ [] }}
            {% endif %}

tl;dr
This discards duplicates:

{{ (msgs + new) | unique(attribute='id') | list }}
3 Likes

Iā€™ve added some info in the issue : can see what was meant, didnā€™t see double notifications but a yes on the double attributes .

1 Like

Thanks for the updates everyone, was worried I had done something wrong.

its a bit more involved than the notifications, or list within an attribute, and seems to have to do with the trigger template itself? The state is incorrect because if that too, it actually counts (adds in this case) double. also a new GitHub issue was opened: Trigger templates are executed twice since 2023.9.1 Ā· Issue #100096 Ā· home-assistant/core Ā· GitHub

this

template:

  - trigger:

      - platform: state
        entity_id: automation.reload_tradfri_integration #counter.
        attribute: last_triggered
        id: count

    sensor:

      - unique_id: count_reload_tradfri_integration
        state: >
          {% if trigger.id == 'count' %} {{this.state|default(0)|int(0)+ 1}}
          {% else %} 0
          {% endif %}

        attributes:
          last: >
            {{now().strftime('%D %X')}}
          <<: &history
            history_1: >
              {{this.attributes.last|default('Not yet set')}}
            history_2: >
              {{this.attributes.history_1|default('Not yet set')}}
            history_3: >
              {{this.attributes.history_2|default('Not yet set')}}
            history_4: >
              {{this.attributes.history_3|default('Not yet set')}
etc

also suffers the issue:

the single attribute list version I have of the same:

     - unique_id: count_reload_tradfri_integration_single_attribute
       state: >
         {% if trigger.id == 'count' %} {{this.state|default(0)|int(0)+ 1}}
         {% else %} 0
         {% endif %}
       attributes:
         herlaad_overzicht: >
           {% set op = now().strftime('%D %X') %}
           {% set current = this.attributes.get('herlaad_overzicht',[]) %}
           {% set new = [{'op': op }] %}
           {{(new + current)[:9]}}
         last_triggered: >
           {{now()}}

shows this:

FYI, emontnemery has corrected the problem. The fix is in the next patch release (2023.9.2).

1 Like

I use also the messages system (Setting up a Message Box in Home Assistant ā€“ RJT-Services). But i want to have from each message an seperate entity on my dashboard. I have looked to the auto entities card but have no clue how to do that with attributes. Anyone any clue how i can build that ?

Well yes but that is not the topic here really. Please open a new?
Also Maybe ask RJT services since they copied our info they might know :wink:

Why those people post what they do and take credit is beyond me.

RJT clearly says that the idea comes from 123 by linking to his post.

@rolfberkenbosch , what do you mean by separate entities? Several Markdown Cards?

Indeed several markdown cards, each card with their own title.

Thatā€™s @jane-t (a Padawan) on discord FYI. She properly sites her resources in that blog.