Skip to main content
The sensor platform allows integrations to expose read-only measurement values and state information to Home Assistant. Sensors are one of the most common entity types.

Base Class

All sensor entities inherit from SensorEntity, which is defined in homeassistant.components.sensor.

Entity Description

Sensor entities can optionally use SensorEntityDescription to define static metadata:

Required Properties

native_value

The current value of the sensor. Can be a number, string, date, or datetime.

Optional Properties

device_class

Indicates the type of sensor. This affects how the sensor is displayed and what unit conversions are available.
Common device classes:
  • TEMPERATURE - Temperature sensor
  • HUMIDITY - Humidity sensor
  • POWER - Power consumption
  • ENERGY - Energy consumption
  • PRESSURE - Pressure
  • BATTERY - Battery level
  • TIMESTAMP - Date/time value
  • ENUM - Enumerated value

native_unit_of_measurement

The unit of measurement for the sensor value.

state_class

Indicates how the state should be tracked over time. Required for long-term statistics.
Available state classes:
  • MEASUREMENT - Instantaneous value (temperature, power)
  • TOTAL - Monotonically increasing total (energy consumed)
  • TOTAL_INCREASING - Total that can decrease (e.g., water tank level)

suggested_display_precision

Number of decimal places to display in the UI.

suggested_unit_of_measurement

Override automatic unit conversion to suggest a specific display unit.

options

For enum sensors, the list of valid values.

last_reset

For total sensors, when the value was last reset.

Example Implementation

RestoreSensor

For sensors that should restore their state after restart:

Real-World Example

From the demo integration (homeassistant/components/demo/sensor.py):

Important Notes

  • Sensors should set _attr_native_value rather than implementing state directly
  • The state property is final and handles unit conversion automatically
  • For numeric sensors, always set a device_class and/or state_class to enable unit conversion
  • Use native_unit_of_measurement for the raw value’s unit; Home Assistant handles conversion
  • Sensors with EntityCategory.CONFIG cannot be added (will raise HomeAssistantError)

See Also