MQTT sensor invalid timestamp format

Hi awesome folks.

I’ve had HomeAssistant monitoring my oil tank for a few years now, but in the last few releases I’m seeing log errors due to the rtl433 sensor timestamps not being properly formatted.

I have the rtl_433 add-on pulling data from the 433MHz receiver into MQTT, and then an MQTT sensor reading those messages.

My sensor is defined as:

    name: "Oil tank depth"
    state_topic: "rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664"
    value_template: "{{ value_json.depth_cm | int  }}"
    unit_of_measurement: "cm"

The MQTT payload includes:

% mosquitto_sub -h 172.21.230.50 -u [redacted] -P [redacted] -t "rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/#" -v
rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/depth_cm 58
rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/time 2022-05-08 20:18:35
rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/id 148218664
rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/flags 160
rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/maybetemp 17
rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/temperature_C 20.0
rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/binding_countdown 0

The exceptions look like this:

Exception in message_received when handling msg on 'rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/time': '2022-05-08 19:45:43' Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 47, in wrapper msg_callback(msg) File "/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py", line 283, 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 398, in state raise ValueError( ValueError: **Invalid datetime: sensor.oil_sonicsmart_148218664_utc provides state '2022-05-08 19:45:43', which is missing timezone information**

Is there a way for me to reformat the timestamp from the rtl_433 before it gets pushed to MQTT?
Is there a way to reformat the timestamp in the sensor template?

I couldn’t see anything on the templating page that made me understand how to access one field in my value template but alter the timestamp associated with that message before processing continues… Am I missing the bleeding’ obvious?

Cheers
Mike

Hi @Taras - this is my confusion. I don’t have a sensor handling the timestamp - Perhaps it’s being manipulated inside the RTL_433 add-on as it passes the message to MQTT?

Reading between the lines are you suggesting I could/should create a sensor to handle the timestamp topic, and reformat that with | as_timestamp or similar?

Thanks @Taras. Was the MQTT payload I showed in the original post not what you’re after? I’m happy to try to get more granularity but not sure how best to get what you’re after, if it’s not as displayed by mosquito_sub?
E.g.

% mosquitto_sub -h 172.21.230.50 -u [redacted] -P [redacted] -t "rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/#" -v
rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/depth_cm 58
rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/time 2022-05-08 20:18:35
rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/id 148218664
rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/flags 160
rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/maybetemp 17
rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/temperature_C 20.0
rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/binding_countdown 0

I’m trying that now, but I may have oversimplified my question and misled you…
I have two sensors defined, one reads out the depth in cm, but the other really does want to use a template as I’m converting from the depth to a volume:

  - platform: mqtt
    name: "Oil tank depth"
    state_topic: "rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/depth_cm"
    unit_of_measurement: "cm"

  - platform: mqtt
    name: "Oil tank contents"
    state_topic: "rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664"
    unit_of_measurement: "litres"
    value_template: "{{ (1200 * 0.5 * ( 1 + cos ( 3.141 * ( ( value_json.depth_cm | int - 10 ) / 140 ) ) ) ) | int  }}"

It may well be the latter sensor that’s triggering the error?

Yup - as I suspected I’m still seeing the same ValueError. I’ll try commenting out the templated sensor and see if that clears it (with only the Oil tank depth sensor enabled).

Hmm. I looked again at the exception, which boils down to:
sensor.oil_sonicsmart_148218664_utc provides state '2022-05-08 19:45:43', which is missing timezone information
But there’s no topic called utc in the MQTT payloads ,only time
I wondered if this was a side-effect of the RTL_433 MQTT Auto Discovery add-on, but the script that this wraps explicitly ignores time as a field.
I can see the entity in my Dev Tools, distinct from the ones I’ve created in configuration.yaml:

Yup - errors are still being generated so this isn’t a templating problem, it’s an issue with rtl_433 / MQTT I think… although I’m no closer to understanding how to address it, do I disable the auto discovery and remove its entities (then report an issue to the maintainers)?

Exception in message_received when handling msg on 'rtl_433/74930c0d-rtl433/devices/CurrentCost-TX/3350/time': '2022-02-08 16:33:11'
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 47, in wrapper msg_callback(msg)
File "/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py", line 283, 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 398, in state raise 
ValueError( ValueError: Invalid datetime: sensor.currentcost_tx_3350_utc 
provides state '2022-02-08 16:33:11', 
which is missing timezone information

Exception in message_received when handling msg on 'rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/148218664/time': '2022-05-09 16:19:14'
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 47, in wrapper msg_callback(msg)
File "/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py", line 283, 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 398, in state raise
ValueError( ValueError: Invalid datetime: sensor.oil_sonicsmart_148218664_utc
provides state '2022-05-09 16:19:14',
which is missing timezone information

Success! Disabling those other entities removed the error from the logs. Sorry for my misunderstanding / mis-statement of the problem, but thanks for the guidance @123.

I’ll see if I can report this in a more sane fashion to the RTL_433 folks.

1 Like

I was too slow. Issue has been raised to the upstream project rtl_433 and incorporated into the Home Assistant add-ons including the RTL_433 MQTT Auto Discovery one. See HASS bridge emits incorrectly formatted time · Issue #1904 · merbanan/rtl_433 · GitHub and Datetime without timezone exceptions with HA 12.0 · Issue #35 · pbkhrv/rtl_433-hass-addons · GitHub