Template sensor adds twice as much/correct in 2023.9.1 compared to 2023.8.x

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

But now with 2023.9.1 it adds 2x the values:

image

Is there anything changed, how trigger.from and .to is working now in 2023.9.x? Or a bug? Or (most probably) something wrong on my side?

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, …

Related: New behavior: self-referencing template is evaluated twice?

Thanks a lot for pointing to this. There it is as here self referencing, but shouldnt be the reason, because it is not in the trigger. There another issue is linked, where it is happening in adding attributes twice.

It seems that the only equal thing in all this issues is, that every trigger is now firing twice in trigger templates, which would be ofc a severe thing.

Alas I have no clue for you, it just seems that the Template integration has a bug.

I would report it as an Issue or add a comment to pedolsky’s Issue linked above. This new behavior is a serious problem because it corrupts computed values.

Already did it. And yes, the sensors got corrupted now so I have to fix them afterwards, what is a problem because of the total_increasing and the follow up dailies, etc.

Link, please?

Yesterday here and today again here (to seperate it if not related).

2 Likes

For all those that haven’t yet read arganto’s Issue, it helped emontnemery identify the cause of the error and correct it in this PR which has already been merged into the upcoming patch release (2023.9.2).

1 Like