mirror of
https://github.com/home-assistant/developers.home-assistant.git
synced 2025-07-14 12:56:30 +00:00
Document + blog post sensor presentation rounding (#1673)
This commit is contained in:
parent
2afea1546a
commit
428318d797
@ -4,6 +4,9 @@ authorURL: https://github.com/emontnemery
|
|||||||
title: "Sensor entity can now do rounding of numerical values"
|
title: "Sensor entity can now do rounding of numerical values"
|
||||||
---
|
---
|
||||||
|
|
||||||
|
Note:
|
||||||
|
The changes described here have been reverted and replaced with rounding for presentation, more details can be found in [this blog post](blog/2023/02/08/sensor_presentation_rounding).
|
||||||
|
|
||||||
`SensorEntity` can now do rounding of a numerical `native_value` when it's converted to the sensor state. This is implemented as a part of [core PR #86074](https://github.com/home-assistant/core/pull/86074). The rounding is opt-in by integrations setting the `native_precision` property. It is recommended that this property is set by integrations because it ensures the number of decimals is reasonable also after unit conversion.
|
`SensorEntity` can now do rounding of a numerical `native_value` when it's converted to the sensor state. This is implemented as a part of [core PR #86074](https://github.com/home-assistant/core/pull/86074). The rounding is opt-in by integrations setting the `native_precision` property. It is recommended that this property is set by integrations because it ensures the number of decimals is reasonable also after unit conversion.
|
||||||
|
|
||||||
A summary of the changes, copied from the PR description:
|
A summary of the changes, copied from the PR description:
|
||||||
|
20
blog/2023-02-08-sensor_presentation_rounding.md
Normal file
20
blog/2023-02-08-sensor_presentation_rounding.md
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
---
|
||||||
|
author: Erik Montnemery
|
||||||
|
authorURL: https://github.com/emontnemery
|
||||||
|
title: "The number of decimals used when displaying a sensor state is now configurable"
|
||||||
|
---
|
||||||
|
|
||||||
|
The number of decimal digits shown when displaying a sensor state is now configurable by the user. Integrations can suggest the number of
|
||||||
|
decimal digits by setting the property `suggested_display_precision`. Integrations are encouraged to remove rounding for display and instead set property `suggested_display_precision`.
|
||||||
|
|
||||||
|
Round for presentation is done by the frontend, as well as by new template functions introduced in [core PR #87619](https://github.com/home-assistant/core/pull/87619).
|
||||||
|
|
||||||
|
The number of displayed decimal digits is influenced by unit conversion:
|
||||||
|
- Converting from a smaller to a larger unit increases the display precision
|
||||||
|
- Converting from a larger to a smaller unit decreases the display precision if the integration has set `suggested_display_precision`
|
||||||
|
- Minimum precision when converting from a larger to a smaller unit is 0, i.e. there's no rounding to tens, hundreds etc.
|
||||||
|
|
||||||
|
The number of displayed decimal digits is not influenced by unit conversion if the user has set the display precision themselves.
|
||||||
|
|
||||||
|
Note:
|
||||||
|
A similar concept where the sensor's state was rounded, detailed in [an earlier blog post](blog/2023/01/25/sensor_rounding), has been reverted.
|
@ -15,11 +15,11 @@ Properties should always only return information from memory and not do I/O (lik
|
|||||||
| ---- | ---- | ------- | -----------
|
| ---- | ---- | ------- | -----------
|
||||||
| device_class | string | `None` | Type of sensor.
|
| device_class | string | `None` | Type of sensor.
|
||||||
| last_reset | `datetime.datetime` | `None` | The time when an accumulating sensor such as an electricity usage meter, gas meter, water meter etc. was initialized. If the time of initialization is unknown, set it to `None`. Note that the `datetime.datetime` returned by the `last_reset` property will be converted to an ISO 8601-formatted string when the entity's state attributes are updated. When changing `last_reset`, the `state` must be a valid number.
|
| last_reset | `datetime.datetime` | `None` | The time when an accumulating sensor such as an electricity usage meter, gas meter, water meter etc. was initialized. If the time of initialization is unknown, set it to `None`. Note that the `datetime.datetime` returned by the `last_reset` property will be converted to an ISO 8601-formatted string when the entity's state attributes are updated. When changing `last_reset`, the `state` must be a valid number.
|
||||||
| native_precision | int | `None` | The number of decimals which should be used in the sensor's state after rounding.
|
|
||||||
| native_unit_of_measurement | string | `None` | The unit of measurement that the sensor's value is expressed in. If the `native_unit_of_measurement` is °C or °F, and its `device_class` is temperature, the sensor's `unit_of_measurement` will be the preferred temperature unit configured by the user and the sensor's `state` will be the `native_value` after an optional unit conversion.
|
| native_unit_of_measurement | string | `None` | The unit of measurement that the sensor's value is expressed in. If the `native_unit_of_measurement` is °C or °F, and its `device_class` is temperature, the sensor's `unit_of_measurement` will be the preferred temperature unit configured by the user and the sensor's `state` will be the `native_value` after an optional unit conversion.
|
||||||
| native_value | `None`, `datetime.date`, `datetime.datetime`, `decimal.Decimal`, float, int, string | **Required** | The value of the sensor in the sensor's `native_unit_of_measurement`. Using a `device_class` may restrict the types that can be returned by this property.
|
| native_value | `None`, `datetime.date`, `datetime.datetime`, `decimal.Decimal`, float, int, string | **Required** | The value of the sensor in the sensor's `native_unit_of_measurement`. Using a `device_class` may restrict the types that can be returned by this property.
|
||||||
| options | list | `None` | In case this sensor provides a textual state, this property can be used to provide a list of possible states. Requires the `enum` device class to be set. Cannot be combined with `state_class` or `native_unit_of_measurement`.
|
| options | list | `None` | In case this sensor provides a textual state, this property can be used to provide a list of possible states. Requires the `enum` device class to be set. Cannot be combined with `state_class` or `native_unit_of_measurement`.
|
||||||
| state_class | string | `None` | Type of state. If not `None`, the sensor is assumed to be numerical and will be displayed as a line-chart in the frontend instead of as discrete values.
|
| state_class | string | `None` | Type of state. If not `None`, the sensor is assumed to be numerical and will be displayed as a line-chart in the frontend instead of as discrete values.
|
||||||
|
| suggested_display_precision | int | `None` | The number of decimals which should be used in the sensor's state when it's displayed.
|
||||||
| suggested_unit_of_measurement | string | `None` | The unit of measurement to be used for the sensor's state. For sensors with a `unique_id`, this will be used as the initial unit of measurement, which users can then override. For sensors without a `unique_id`, this will be the unit of measurement for the sensor's state. This property is intended to be used by integrations to override automatic unit conversion rules, for example, to make a temperature sensor always display in `°C` regardless of whether the configured unit system prefers `°C` or `°F`, or to make a distance sensor always display in miles even if the configured unit system is metric.
|
| suggested_unit_of_measurement | string | `None` | The unit of measurement to be used for the sensor's state. For sensors with a `unique_id`, this will be used as the initial unit of measurement, which users can then override. For sensors without a `unique_id`, this will be the unit of measurement for the sensor's state. This property is intended to be used by integrations to override automatic unit conversion rules, for example, to make a temperature sensor always display in `°C` regardless of whether the configured unit system prefers `°C` or `°F`, or to make a distance sensor always display in miles even if the configured unit system is metric.
|
||||||
|
|
||||||
:::tip
|
:::tip
|
||||||
|
Loading…
x
Reference in New Issue
Block a user