diff --git a/custom_components/simple_pid_controller/number.py b/custom_components/simple_pid_controller/number.py index 6f5288b..bf44c7b 100644 --- a/custom_components/simple_pid_controller/number.py +++ b/custom_components/simple_pid_controller/number.py @@ -168,9 +168,9 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry, desc: dict) -> None: if self._key == "setpoint": min_val, max_val = range_min, range_max elif self._key == "output_min": - min_val, max_val = -abs(range_max), 0.0 + min_val, max_val = -abs(range_max), range_min elif self._key == "output_max": - min_val, max_val = 0.0, range_max + min_val, max_val = range_min, range_max else: _LOGGER.error("Unexpected PID parameter key: %s", self._key) min_val, max_val = DEFAULT_RANGE_MIN, DEFAULT_RANGE_MAX diff --git a/custom_components/simple_pid_controller/sensor.py b/custom_components/simple_pid_controller/sensor.py index 6537018..8137637 100644 --- a/custom_components/simple_pid_controller/sensor.py +++ b/custom_components/simple_pid_controller/sensor.py @@ -4,7 +4,7 @@ import logging -from homeassistant.components.sensor import SensorEntity +from homeassistant.components.sensor import SensorEntity, SensorStateClass from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -125,6 +125,7 @@ async def start_refresh(_: Any) -> None: PIDContributionSensor( hass, entry, "pid_d_contrib", "D contribution", coordinator ), + PIDContributionSensor(hass, entry, "error", "Error", coordinator), ] ) @@ -170,6 +171,7 @@ def __init__( BasePIDEntity.__init__(self, hass, entry, key, name) self._attr_native_unit_of_measurement = "%" + self._attr_state_class = SensorStateClass.MEASUREMENT @property def native_value(self) -> float | None: @@ -195,14 +197,24 @@ def __init__( self._attr_entity_category = EntityCategory.DIAGNOSTIC self._attr_entity_registry_enabled_default = False + self._attr_state_class = SensorStateClass.MEASUREMENT self._key = key @property def native_value(self): contributions = self._handle.last_contributions + input_value = self._handle.get_input_sensor_value() + setpoint = self._handle.get_number("setpoint") + + if input_value is None or setpoint is None: + error = 0 + else: + error = input_value - setpoint + value = { "p": contributions[0], "i": contributions[1], "d": contributions[2], + "error": error, }.get(self._key) return round(value, 2) if value is not None else None