Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List entries within an attribute are processed twice #99972

Closed
pedolsky opened this issue Sep 9, 2023 · 6 comments · Fixed by #100119
Closed

List entries within an attribute are processed twice #99972

pedolsky opened this issue Sep 9, 2023 · 6 comments · Fixed by #100119

Comments

@pedolsky
Copy link

pedolsky commented Sep 9, 2023

The problem

I'm using several trigger based template sensors with a list within an attribute, based on examples like

here or here.

Since 2023.9, the list entries are duplicated.

IMG_0194

IMG_0191

What version of Home Assistant Core has the issue?

core-2023.9.1

What was the last working version of Home Assistant Core?

core.2023.8

What type of installation are you running?

Home Assistant Supervised

Integration causing the issue

Template

Link to integration documentation on our website

https://www.home-assistant.io/integrations/template/

Diagnostics information

No response

Example YAML snippet

template:
  - trigger:
      - platform: state
        entity_id:
          - input_boolean.test
          - binary_sensor.bewegung_ankleide
          - binary_sensor.bewegung_flur
          - binary_sensor.bewegung_garderobe
          - binary_sensor.bewegung_hg06335
          - binary_sensor.bewegung_kuche
          - binary_sensor.bewegung_trinkbrunnen
          - binary_sensor.eingangstur
          - binary_sensor.kamera_loggia
          - binary_sensor.loggiatur
        from: 'off'
        to: 'on'
    sensor:
      - name: Recent
        state: "{{ now().timestamp() | timestamp_custom() }}"
        attributes:
          history: >
            {% set current = this.attributes.get('history', []) %}
            {% set new = [{
              "name": trigger.to_state.name,
              "time": now().isoformat() }] %}
            {{ (new + current)[:10] }}


### Anything in the logs that might be useful for us?

```txt
no

Additional information

No response

@home-assistant
Copy link

home-assistant bot commented Sep 9, 2023

Hey there @PhracturedBlue, @tetienne, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of template can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign template Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


template documentation
template source
(message by IssueLinks)

@joel-bourquard
Copy link

Hi @pedolsky ,
Thanks for pinging me. I'm having the same issue - for reference: https://community.home-assistant.io/t/new-behavior-self-referencing-template-is-evaluated-twice/612296

Maybe this will help: adding a random() statement to the template confirms that the value is fully evaluated twice (with 2 different values of random).

@Mariusthvdb
Copy link
Contributor

Sorry I didn't spot this is the community post. Checking the attributes now reveals the issue here too.

Notifications themselves are Not listed twice (that's what I figured to mention to be about at first ), attributes are:

image

Btw the Frontend has issues of its own ;-)

@tdejneka
Copy link

tdejneka commented Sep 9, 2023

Until the bug is fixed, enhance the template to discard duplicates using the unique filter. In your example, duplicates are any list items with the same time value.

Change this:

{{ (new + current)[:10] }}

To this:

 {{ ((new + current) | unique(attribute='time') | list)[:10] }}

In Mariusthvdb's example, duplicates would be list items with the same created_at value.

@emufan
Copy link
Contributor

emufan commented Sep 10, 2023

Until the bug is fixed

This bug is not related in special to attributes, but that every trigger is now fired twice?

So this could be same reason as in my case, described in the forum:
https://community.home-assistant.io/t/template-sensor-adds-twice-as-much-correct-in-2023-9-1-compared-to-2023-8-x/612822/3

@emontnemery
Copy link
Contributor

Fixed by #100119

@github-actions github-actions bot locked and limited conversation to collaborators Oct 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants