How to get the average difference between two sensors

I’m trying to automatically update the offset of my Tado TRV’s, which are near the windows and therefore always off. Tado has a service ‘set_climate_temperature_offset’ for this. But how do I pass the right offset into it? My plan was to do this once per day, since the TRV re-adjusts the valve every time you change the offset, which costs some battery.

The pseudocode would look something like this:
new_offset = average(room_sensor, 24h) - (average(trv_sensor, 24h) - current_offset)

Would there be a way to do this without creating new template sensors for the two averages. I’m trying to avoid template sensors for as long as possible, simply because it adds multiple new entities per room.

You can’t get history from template sensors. You’d have to use the statistics integration to get a 24hr average.

Once complete, you’d then create a template sensor from the differences.

Don’t add the helpers to a room. Hide them.

Thanks for the quick reply. I understand. Is there a way to have such a statistic only update when I need it, or once per x hours? Having multiple sensors calculate a new average/difference every time the temperature sensor updates (every few seconds) seems wasteful. That’s another reason why I hoped for a template solution that I could compute only when I want to update the TRV offset.

You can use the SQL integration to do that, but not statistics which makes it easy. Stat’s runs every new datapoint, but it’s not really a lot of overhead.

In that case I’ll probably just have to fix te sensor updating that often, that was a bug with the flashed firmware anyway… Thanks!

Edit: I prevented the statistics calculation from occurring too often by first calculating the difference with a template, rounding that, and then taking the average of that difference, instead of the difference of averages.