keton / etrv2mqtt

Danfoss Eco BLE thermostat to MQTT bridge
MIT License
26 stars 10 forks source link

Newer versions of Home Assistant require timezone info in Last Updated field #11

Closed drzero42 closed 1 year ago

drzero42 commented 2 years ago

Since upgrading to Home Assistant 2021.12, I am receiving errors in my log, because the Last Updated values from etrv2mqtt do not contain timezone information. This means that in Home Assistant, my thermostats will have a value of "Unknown" in the Last Updated field.

This is one of the errors from my logs:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 37, in wrapper
    msg_callback(msg)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py", line 249, in message_received
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 505, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 538, in _async_write_ha_state
    state = self._stringify_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 511, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 411, in state
    raise ValueError(
ValueError: Invalid datetime: sensor.office_last_update provides state '2022-01-15 18:13:28.386640', which is missing timezone information

As far as I can tell, it should be enough to add timezone.utc to the datetime.now() call (timezone needs to be imported from the datetime module). That would be here: https://github.com/keton/etrv2mqtt/blob/626f6f5cf3b30f86bf9bb0d1e4608aee70aa4b00/etrv2mqtt/etrvutils.py#L34

Here is my small little test of it:

$ cat timezone-test.py 
from datetime import datetime, timezone

print("Without timezone info:", datetime.now())
print("With timezone info:", datetime.now(timezone.utc))

$ python3 timezone-test.py 
Without timezone info: 2022-01-15 18:34:39.682039
With timezone info: 2022-01-15 17:34:39.682100+00:00
drzero42 commented 2 years ago

I see that @mjenda already did a PR for this. Any chance we can get it merged soon?

Mikk36 commented 1 year ago

Same issue here.


Source: util/logging.py:168
First occurred: 11:05:48 (1 occurrences)
Last logged: 11:05:48

Exception in message_received when handling msg on 'etrv/Elutuba suur radiaator/state': '{"name": "Elutuba", "battery": 88, "room_temp": 24.0, "set_point": 23.0, "last_update": "2022-10-09 11:05:48.319576"}' Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 44, in wrapper msg_callback(msg) File "/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py", line 303, in message_received self.async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 533, in async_write_ha_state self._async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 571, in _async_write_ha_state state = self._stringify_state(available) File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 539, in _stringify_state if (state := self.state) is None: File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 414, in state raise ValueError( ValueError: Invalid datetime: sensor.elutuba_suur_radiaator_last_update provides state '2022-10-09 11:05:48.319576', which is missing timezone information```
Mikk36 commented 1 year ago

As a temporary solution I just created a few tempate sensors to grab the values from the MQTT sensors to have LTS (long term statistics) support.


    - name: Elutuba suur radiaator temperatuur
      unique_id: living_room_large_radiator_temperature
      device_class: temperature
      state_class: measurement
      unit_of_measurement: "°C"
      icon: "mdi:radiator"
      state: >
        {{ state_attr('climate.elutuba_suur_radiaator_thermostat', 'current_temperature') | float }}
      availability: >
        {{ states('climate.elutuba_suur_radiaator_thermostat') not in ['unknown', 'unavailable'] }}
    - name: Elutuba suur radiaator power
      unique_id: living_room_large_radiator_power
      device_class: battery
      state_class: measurement
      unit_of_measurement: "%"
      icon: "mdi:radiator"
      state: >
        {{ states('sensor.elutuba_suur_radiaator_battery') | int }}
      availability: >
        {{ states('sensor.elutuba_suur_radiaator_battery') not in ['unknown', 'unavailable'] }}```
Mikk36 commented 1 year ago

So much for that, it looks like 2022.10.4 killed that.

keton commented 1 year ago

fixed