Median temperature on a floor

The new floors thing is cool. I want to know the median temperature reported by all temperature sensor entities on each floor. I have this so far:

{{ floor_areas("first") 
  | map('area_entities')
  | list 
  | join 
}}

This gives me a list of entity IDs on that floor (The join filter appears to flatten arrays which is fun). How would I turn that into a list of state objects? And how do I get the median from a list of numeric values? Ideally i want to do something like this:

{{ floor_areas("first") 
  | map('area_entities')
  | list 
  | join
  | map('states')  # Doesn't seem to work
  | selectattr('attributes.device_class', 'eq', 'temperature')
  | map('state')   # Doesn't seem to exist
  | median   # Doesn't exist
}}

Any ideas? I also considered approaching it from the other direction and finding all temperature sensors and then filtering for floor, but I can’t figure out how to use the floor functions in a selectattr, and I am not sure the floor is yet an attribute of a state.

OK now I have this:

{% set temps = namespace(values = [], midpoint = 0) %}
{% for entity in states %}
  {% if entity.attributes.device_class in ['temperature'] and floor_id(entity.entity_id) == "ground" and entity.state | float > 5 and entity.state | float < 40 %}
    {% set temps.values = temps.values + [entity.state | float] %}
  {% endif %}
{% endfor %}
{% set temps.midpoint = ((temps.values | length) / 2) | round(0, 'ceil') %}

{{(temps.values | sort)[temps.midpoint]}}

Not awful but I am sad I can’t figure out how to do it functionally.

average

{{ floor_areas("first") | map('area_entities')  | sum(start=[]) | select('is_state_attr', 'device_class', 'temperature') | map('states') | select('is_number') | map('float') | average }}

median

{{ floor_areas("first") | map('area_entities')  | sum(start=[]) | select('is_state_attr', 'device_class', 'temperature') | map('states') | select('is_number') | map('float') | median }}
2 Likes

I love it.

they key is taking the list of lists after mapping area_entities and flattening it to a list