Getting the median temperature of a floor

Published on April 12, 2025 - Estimated reading time is 2 minutes
Home Assistant Template

Template sensors in Home Assistant allow you to calculate custom values by processing data from other entities. This note focuses on using a template sensor to calculate the median temperature of a specific floor, providing a practical example and explaining its components.

Screenshot of template sensor

Set up template sensors in the UI

  1. Navigate to Settings > Devices & Services > Helpers.
  2. Click Create Helper and select Template and the Template a sensor.
  3. Provide a name for your sensor, such as "First floor median temperature"
  4. Enter your Jinja2 template in the State template field (there is an example below).
  5. Save the helper, and the new template sensor will be created.

Example template

The following template calculates the median temperature of all temperature sensors on the "First floor," excluding hidden entities. It ensures that only sensor entities are considered, improving performance:

{{
  floor_entities("First floor")
  | select('match', 'sensor.*')
  | select('is_state_attr', 'device_class', 'temperature')
  | reject('is_hidden_entity')
  | map('states')
  | select('is_number')
  | map('float')
  | median
}}

Explanation

Why use template sensors?

Template sensors are ideal for:

For more details, refer to the Home Assistant Template sensor documentation.