Wrong time format in Mqtt sensor

Hi.
I created a Mqtt sensor that queries the Router and returns the Uptime value in seconds.
This is the sensor with the template:

mqtt:
  sensor:
#ROUTER TELTONIKA UPTIME
    - name: "Teltonika RUT Uptime"
      unique_id: "teltonika_rut_uptime"
      icon: mdi:clock-check-outline
      value_template: "{{ value | int(0) | timestamp_custom('%-d d, %-H h, %-M m', false) }}"
      state_topic: "router/11188xxxxx/uptime"
      

The problem is that it always adds 1 more day.
For example if the time is 22h, 10m, on the dashboard I see 1d, 22h, 10m.
If the real time is 2d, 10h, 5m on the dashboard I see 3d, 10h, 5m.
It always adds 1 more day…how can I fix it?

Thanks

Welcome, I’m Captain Obvious :smiley:
Could you show actual examples of the MQTT state and the resulting sensor, please.

This?

And value in seconds without template

A timestamp is not just a plain number of seconds, it’s a date/time expressed as the number of seconds since 01/01/1970 00:00.
“%d” is not a number of days, it’s the day of the month of the timestamp.

406356, as a timestamp, is the 5th of January 1970, 16:52

How can I convert that value expressed in seconds to d,h,m format?

One possibility:

{{ "%d d, %d h, %d m" | format(value // (60*60*24), value // (60*60) % 24, value // 60 % 60) }}

If days are not that important, you could also set the “duration” device class on the sensor, so that HA knows to display the number as a duration, e.g.

  - name: "Teltonika RUT Uptime"
    unique_id: "teltonika_rut_uptime"
    state_topic: "router/11188xxxxx/uptime"   
    device_class: duration 
    unit_of_measurement: s

image

    - name: "Teltonika RUT Uptime"
      unique_id: "teltonika_rut_uptime"
      icon: mdi:clock-check-outline
      value_template: "{{ '%d d, %d h, %d m' | format(value // (60*60*24), value // (60*60) % 24, value // 60 % 60) }}"
      state_topic: "router/1118876940/uptime"

Not working.
I would also need the days, exactly d-h-m

Help yourself and check the HA log for errors.

. I also tried other templates found on the forum, none work. I really don’t know how to do it.

Blockquote
Logger: homeassistant.util.logging
Source: util/logging.py:168
First occurred: 12:16:00 (1 occurrences)
Last logged: 12:16:00
Exception in message_received when handling msg on ‘router/1118876940/uptime’: ‘411876’ 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 292, in message_received _update_state(msg) File “/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py”, line 243, in _update_state payload = self._template(msg.payload, PayloadSentinel.DEFAULT) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “/usr/src/homeassistant/homeassistant/components/mqtt/models.py”, line 251, in async_render_with_possible_json_value rendered_payload = self._value_template.async_render_with_possible_json_value( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 735, in async_render_with_possible_json_value return _render_with_context(self.template, compiled, **variables).strip() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 2179, in _render_with_context return template.render(**kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File “/usr/local/lib/python3.11/site-packages/jinja2/environment.py”, line 1301, in render self.environment.handle_exception() File “/usr/local/lib/python3.11/site-packages/jinja2/environment.py”, line 936, in handle_exception raise rewrite_traceback_stack(source=source) File “”, line 1, in top-level template code TypeError: unsupported operand type(s) for //: ‘str’ and ‘int’

Not going to able to help you with anything specific. But maybe point you in the right direction.

A lot of the time these offsets in time are due to timezone differences because there is a difference between your local timezone and the timezone that it thinks it should be.

Have you set your timezone in settings?

Yes, the time zone is set correctly. But I can’t find the right template to convert seconds to days-hours-minutes

Could anyone help me? A template that converts seconds into hours would also be fine (to be inserted directly into the mqtt sensor value_template)

It’s an uptime. Just output the raw value, then add unit_of_measurement: s and device_class: duration. The front end will convert the time into d hh:mm:ss

mqtt:
  sensor:
#ROUTER TELTONIKA UPTIME
    - name: "Teltonika RUT Uptime"
      unique_id: "teltonika_rut_uptime"
      unit_of_measurement: s
      device_class: duration
      icon: mdi:clock-check-outline
      value_template: "{{ value }}"
      state_topic: "router/11188xxxxx/uptime"