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

Trigger templates are executed twice since 2023.9.1 #100096

Closed
emufan opened this issue Sep 11, 2023 · 2 comments · Fixed by #100119
Closed

Trigger templates are executed twice since 2023.9.1 #100096

emufan opened this issue Sep 11, 2023 · 2 comments · Fixed by #100119
Milestone

Comments

@emufan
Copy link
Contributor

emufan commented Sep 11, 2023

The problem

I have a helper trigger template sensor (as backup for a Riemann sum)

  - trigger:
      - platform: state
        entity_id: sensor.myenergi_wallbox_charge_added_session
    sensor:
      - name: Wallbox other ENERGY_COUNTER
        unique_id: unique_id_template_sensor_wallbox_other_energy_counter
        state: >
          {% if trigger.from_state and trigger.to_state %}
            {% if is_number(trigger.from_state.state)
                and is_number(trigger.to_state.state)
                and (float(trigger.to_state.state) > float(trigger.from_state.state)) %}
              {{ ( states("sensor.wallbox_other_energy_counter")|float
              + float(trigger.to_state.state)*1000 
              - float(trigger.from_state.state)*1000 )|round(1) }}
            {% else %}
              {{ states("sensor.wallbox_other_energy_counter") }}
            {% endif %}
          {% else %}
            {{ states("sensor.wallbox_other_energy_counter") }}
          {% endif %}
        unit_of_measurement: "Wh"
        device_class: energy
        state_class: total_increasing

This takes the delta of each change of source and adds it to the counter. The source is resetted after each loading session.

This works with <= 2023.8.x

source added

image

counter added

image

But now with 2023.9.1 it adds 2x the values:

image

image

Compared the values in more detail. from and to in an automation are correct. Delta is always 180Wh each minute in the source.

Chart of the source is showing this as well.

But the template sensor adds as shown now 360Wh per minute.

I cannot check if it is now called whyever twice in each trigger or uses other tigger or is using wrong from or to, e.g. from from one trigger before, …

Sees to be related to #99972 or https://community.home-assistant.io/t/new-behavior-self-referencing-template-is-evaluated-twice/612296 There it is not sum but lists and attributes but the bassis seem to be that always the trigger triggers twice.

In my example it corrupts now the values and the follow up values (daily, weekly, ...) as well as the home energy, etc. For me a serious/severe bug.

What version of Home Assistant Core has the issue?

2023.9.1

What was the last working version of Home Assistant Core?

2023.8.x

What type of installation are you running?

Home Assistant OS

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

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

@Mariusthvdb
Copy link
Contributor

Mariusthvdb commented Sep 11, 2023

as noticed in the heads-up Persistent notifications thread https://community.home-assistant.io/t/heads-up-2023-6-longer-has-persistent-notifications-in-states-what-to-do/578654/138?u=mariusthvdb

the nested list attributes suffer, but indeed also regular trigger templates. my example for that topic in the community:

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')}}
            history_5: >
              {{this.attributes.history_4|default('Not yet set')}}
            history_6: >
              {{this.attributes.history_5|default('Not yet set')}}
            history_7: >
              {{this.attributes.history_6|default('Not yet set')}}
            history_8: >
              {{this.attributes.history_7|default('Not yet set')}}
            history_9: >
              {{this.attributes.history_8|default('Not yet set')}}
            last_triggered: >
              {{now()}}
Scherm­afbeelding 2023-09-11 om 07 46 24

and a nested attributes version of that, on the same trigger:

      - 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()}}
Scherm­afbeelding 2023-09-11 om 07 49 42

@emontnemery emontnemery added this to the 2023.9.2 milestone Sep 11, 2023
@emontnemery
Copy link
Contributor

emontnemery commented Sep 11, 2023

I can reproduce the problem on 2023.9, but not on 2023.8. The root cause is a regression introduced by #97400

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants