enkama / hass-variables

Home Assistant variables component
89 stars 12 forks source link

Error declaring and initializing Timestamp variables. #102

Closed sermayoral closed 9 months ago

sermayoral commented 9 months ago

After settings a date of a timestamp variable in the date window dialog:

image

homeassistant show the following error:

Logger: homeassistant.components.sensor
Source: helpers/entity_platform.py:507
Integration: Sensor (documentation, issues)
First occurred: 13:40:14 (1 occurrences)
Last logged: 13:40:14

Error adding entities for domain sensor with platform variable
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 507, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 752, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1023, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 743, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 843, in _async_write_ha_state
    state, attr = self._async_generate_attributes()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 784, in _async_generate_attributes
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 749, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 554, in state
    raise ValueError(
ValueError: Invalid datetime: sensor.date_test_sergio provides state '2023-11-20 00:00:00', which is missing timezone information

The format is not right. Documentation says the following:

If your template is returning a timestamp that should be displayed in the frontend (e.g., as a sensor entity with device_class: timestamp), you have to ensure that it is the ISO 8601 format (meaning it has the “T” separator between the date and time portion). Otherwise, frontend rendering on macOS and iOS devices will show an error. The following value template would result in such an error:

{{ states.sun.sun.last_changed }} => 2023-07-30 20:03:49.253717+00:00 (missing “T” separator)

To fix it, enforce the ISO conversion via isoformat():

{{ states.sun.sun.last_changed.isoformat() }} => 2023-07-30T20:03:49.253717+00:00 (contains “T” separator)

Source: https://www.home-assistant.io/docs/configuration/templating/

Thank you very much 😊

Snuffy2 commented 9 months ago

You are correct, it is not working. I really thought it was working previously. I'm either mistaken or something in HA changed. Either way, I'll work to get this fixed. Thanks for letting us know.

sermayoral commented 9 months ago

It's a pleasure. Thank you for your time maintaining this component. I love it 😊