Statistics sensor unavailable when source sensor is unavailable


I’m trying to create a statistics sensor in order to retrieve the max value of a pool water temperature sensor over the last 24 hours. My source sensor is available only when the water pump is running, but still I would like to get the max recorded value over these periods.

It looks like the statistics sensor should be able to do this, however this sensor reports itself as “unavailable” as soon as the source sensor is “unavailable”. I’ve found the following piece of code in homeassistant/components/statistics/ which seems to confirm what I’m seeing:

    def available(self) -> bool:
        """Return the availability of the sensor linked to the source sensor."""
        return self._available

However, I don’t understand why the availability of the source and computed sensors should be linked. Even if the source becomes unavailable the history still contains the relevant info for the statistics to be computed.

Is my understanding correct? Is this behavior intended to cover a different use case? Would it be possible to do what I’m trying to achieve differently?

I’ve been doing this using a direct SQL query for a while, however my request broke multiple times after HA updates so I would like to find a more reliable solution.


1 Like

I am seeing the exact same thing since a while. Sometime back (maybe half a year) this was not the case.
I calculate the daily maximum of my solar with this piece of yaml:

  - platform: statistics # use to extract maximum of the day
    name: PV Peak heute
    entity_id: sensor.power_ac_fronius
    state_characteristic: value_max
      hours: 20
    sampling_size: 10000

The result is the below (blue = daily peak, purple, actual power during the day)

I can’t see a reason why the statistics sensor should change to unavailable at all.

Does anybody know a reason?

Should this be reported as an issue to the HA team?