Add force_update support to template sensor

This still works in 2023 and is needed for certain uses of the statistics sensor. For example, if your statistics sensor is monitoring another source sensor it can result in the “unknown” value for the statistics sensor. What triggers this “unknown” state is if the source sensor value does not change for lengthy periods. The HA database is not updated until the value changes, meaning the statistics sensor has no values to work from within the required time period.
The workaround is simple, you just add a time-based attribute to your source sensor. The syntax has changed slightly since the earlier comments in this thread, here is an example that works today on HA 2023.2.5:

 - sensor:
      - name: "Desired Charge Current"
        unit_of_measurement: "A"
        state_class: "measurement"
        state: >
            {{ charge_current }}
        attributes:
            minute_counter: "{{ now().minute }}"

This Github issue was raised for the statistics problem I mention: Statistics sensor doesn't handle zero values correctly · Issue #67627 · home-assistant/core · GitHub

4 Likes