Skip to content

Unit of Measurement of Output Sensor is wrong #124

Description

@ajvdw

Since the output sensor can be freely defined. The percentage does not make sense as a unit of measurement.

class PIDOutputSensor(
    CoordinatorEntity[PIDDataCoordinator], RestoreEntity, SensorEntity
):
    """Sensor representing the PID output."""

    def __init__(
        self, hass: HomeAssistant, entry: ConfigEntry, coordinator: PIDDataCoordinator
    ):
        super().__init__(coordinator)

        name = "PID Output"
        key = "pid_output"

        BasePIDEntity.__init__(self, hass, entry, key, name)

        **self._attr_native_unit_of_measurement = "%".**  
        self._attr_state_class = SensorStateClass.MEASUREMENT

    async def async_added_to_hass(self):
        await super().async_added_to_hass()
        if (state := await self.async_get_last_state()) is not None:
            try:
                value = float(state.state)
                self._handle.last_known_output = value
            except (ValueError, TypeError):
                self._handle.last_known_output = 0.0

    @property
    def native_value(self) -> float | None:
        if self.coordinator.data is None:
            return None
        return round(self.coordinator.data, 2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions