Base Class
All sensor entities inherit fromSensorEntity, which is defined in homeassistant.components.sensor.
Entity Description
Sensor entities can optionally useSensorEntityDescription 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.TEMPERATURE- Temperature sensorHUMIDITY- Humidity sensorPOWER- Power consumptionENERGY- Energy consumptionPRESSURE- PressureBATTERY- Battery levelTIMESTAMP- Date/time valueENUM- 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.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_valuerather than implementingstatedirectly - The
stateproperty is final and handles unit conversion automatically - For numeric sensors, always set a
device_classand/orstate_classto enable unit conversion - Use
native_unit_of_measurementfor the raw value’s unit; Home Assistant handles conversion - Sensors with
EntityCategory.CONFIGcannot be added (will raiseHomeAssistantError)