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.
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
}}
floor_entities("First floor")
: Retrieves all entities associated with the "First floor."select('match', 'sensor.*')
: Filters entities to include only those matching the sensor.*
pattern.select('is_state_attr', 'device_class', 'temperature')
: Filters entities to include only those with the device_class
attribute set to temperature
.reject('is_hidden_entity')
: Excludes hidden entities from the list.map('states')
: Extracts the state of each remaining entity.select('is_number')
: Filters out non-numeric states.map('float')
: Converts the numeric states to floating-point numbers.median
: Calculates the median of the resulting list of temperatures.Template sensors are ideal for:
For more details, refer to the Home Assistant Template sensor documentation.