Average temprerature across rooms - What is the best way?

I currently have a template sensor to average the temperature across a group of rooms but I am not convinced this is the best (most efficient) approach.

Has anyone else done this and if so how do you do it?

Here is mine. It ignores readings less than 2 minutes old and also readings more than 10 minutes old as my temperature sensors can update several times a minute and also have been known to go offline :slight_smile:

   average_downstairs_temperature:
    friendly_name: Average Downstairs Temperature
    entity_id:
      - sensor.hall_temperature
      - sensor.kitchen_temperature
      - sensor.sitting_room_temperature
      - sensor.back_bedroom_temperature
    value_template: >
      {% set entities = ['sensor.hall_temperature',
                         'sensor.kitchen_temperature',
                         'sensor.sitting_room_temperature',
                         'sensor.back_bedroom_temperature'] %}
      {% set time = as_timestamp(now()) %}
      {% set ignore_recent = 120 %}
      {% set ignore_old = 600 %}
      {% set ns = namespace(count=0, value=0) %}
      {% for e in entities %}
        {% set domain, object_id = e.split('.') %}
        {% set s = states[domain][object_id] %}
        {% if s.state is defined and
              s.state is not none and
              s != 'unknown' and
              s != 'unavailable' and
              s != '' and
              ((time - as_timestamp(s.last_updated)) < ignore_recent or
               (time - as_timestamp(s.last_updated)) > ignore_old) %}
          {% set ns.count = ns.count + 1 %}
          {% set ns.value = ns.value + s.state | float %}
        {% endif %}
      {% endfor %}
      {% if ns.count == 0 %}
        'unavailable'
      {% else %}
        {{ (ns.value / ns.count) | round (1) }}
      {% endif %}
    unit_of_measurement: °C
1 Like

There’s a sensor that gives you the mean of other sensors:

1 Like

This is probably the best approach if you want to filter out states based on time. Otherwise using the normal statistics integration would work. That integration needs an update to specify a time range instead of max_age, like the history statistics integration.

1 Like

“Min/max Sensor” - hhhmm I wonder why I didn’t look at that when I was trying to get the mean :rofl:

Thanks for pointing that out. Because of the desire to filter on time as @petro says I am not sure it will work in this case but I’m glad I know about it for future.

I would suggest weighting the individual sensor reads with the average time spent in each room (something like 60% living/25% kitchen/15% bedroom in the daytime and 80% bedroom/20% living after sunset/midnight/etc).

That’s a good idea…

When I wanted to implement the average/wighting idea (use sensors form different rooms to control my combo boiler), my wife said “why average? I want the temperature to be NOT less than N degrees in any room” and that’s how I set it up.
Beware of the Min/Max sensor - it will give you the last calculated numeric value even if all of your sensors become unknown/unavailable (more details here). I ended up creating a custom multisource sensor that fixes that (and as a bonus I can switch every source on/off via input_boolean).

Yes, thanks! I’ve just recently discovered that. I expect I’ll go back to using some kind ‘roll-your-own’ method…

Is it something worth sharing, or is it too specific to your use case?

it’s not, I think it’s pretty versatile.

Actually, I have 2 use cases for it:

  1. I receive RF signals from my temperature sensors through 2 gateways: RFLink and OMG Pilight. For some reason if I rely upon only one of them, sometimes I lose a reading. So now I use my multisource sensor to have a ‘last’ version of Min/Max, but it behaves as an original sensor, i.e becomes unknown if all sources become unknown (which happens sometimes as my sensors are battery-powered).
  2. Then I use the same multisource sensor to create a ‘combined’ house temperature from all those multisensors applying min function (forgot to mention it, only min and last are implemented so far).

I can publish my code on Github and post a link here if you think it makes sense.

Yes I’d be interested to see it.
You can’t have too many sources of ideas!

Here it is :wink:

1 Like