Fix issue with temperature sensor handling in informationscollector
This commit is contained in:
@@ -62,7 +62,7 @@ class InformationsCollector(SmartObject):
|
||||
self.min_temp_yesterday_sensor = self.create_entity(output_sensors['min_temp_yesterday'])
|
||||
|
||||
current_min_temp, current_max_temp = self.compute_min_max_temp()
|
||||
if not self.max_temp_sensor.exists() or self.max_temp_sensor.get_state() == 'unavailable':
|
||||
if not self.max_temp_sensor.exists() or self.max_temp_sensor.get_state() == 'unavailable' or self.max_temp_sensor.get_state() == 'unknown':
|
||||
max_temp = current_max_temp
|
||||
try:
|
||||
if max_temp is None or self.dataset['max_temp'] > max_temp:
|
||||
@@ -70,7 +70,7 @@ class InformationsCollector(SmartObject):
|
||||
self.log_info(f"Restoring max_temp from dataset ({max_temp}°C)")
|
||||
except KeyError: pass
|
||||
self.set_max_temp_sensor(max_temp)
|
||||
if not self.min_temp_sensor.exists() or self.min_temp_sensor.get_state() == 'unavailable':
|
||||
if not self.min_temp_sensor.exists() or self.min_temp_sensor.get_state() == 'unavailable' or self.min_temp_sensor.get_state() == 'unknown':
|
||||
min_temp = current_min_temp
|
||||
try:
|
||||
if min_temp is None or self.dataset['min_temp'] < min_temp:
|
||||
@@ -199,8 +199,12 @@ class InformationsCollector(SmartObject):
|
||||
min_temp = None
|
||||
for sensor in self.args['min_max_temp_sensors']['temperature_sensors']:
|
||||
temp = self.get_state(sensor)
|
||||
if not max_temp or temp > max_temp: max_temp = temp
|
||||
if not min_temp or temp < min_temp: min_temp = temp
|
||||
try:
|
||||
temp = float(temp)
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
if max_temp is None or temp > max_temp: max_temp = temp
|
||||
if min_temp is None or temp < min_temp: min_temp = temp
|
||||
return min_temp,max_temp
|
||||
|
||||
def on_log_error(self, name, ts, level, type, message, kwargs):
|
||||
@@ -224,10 +228,17 @@ class InformationsCollector(SmartObject):
|
||||
|
||||
def on_temperature_change(self, entity, attribute, old, new, kwargs):
|
||||
if new != old:
|
||||
|
||||
try:
|
||||
new_value = float(new)
|
||||
except (TypeError, ValueError):
|
||||
self.log_warning(f"Invalid temperature value from {entity}: {new}")
|
||||
return
|
||||
|
||||
min_temp = self.min_temp_sensor.get_state()
|
||||
max_temp = self.max_temp_sensor.get_state()
|
||||
if max_temp is None or max_temp == 'unavailable' or new > max_temp: self.set_max_temp_sensor(new)
|
||||
if min_temp is None or min_temp == 'unavailable' or new < min_temp: self.set_min_temp_sensor(new)
|
||||
if max_temp is None or max_temp == 'unavailable' or new_value > float(max_temp): self.set_max_temp_sensor(new_value)
|
||||
if min_temp is None or min_temp == 'unavailable' or new_value < float(min_temp): self.set_min_temp_sensor(new_value)
|
||||
|
||||
def run_at_midnight(self, kwargs):
|
||||
self.max_temp_yesterday_sensor.set_state(state=self.max_temp_sensor.get_state(), attributes={'unit_of_measurement': '°C', 'device_class': 'temperature'})
|
||||
|
||||
Reference in New Issue
Block a user