Change update frequency of a template that converts seconds to 'ymhms', possible?

Hello!

I have a sensor that displays the uptime of my router, it displays it in seconds. So i used a template to display it in ymhms using this code:

{% set uptime = states.sensor.rt_ac57u_v3_6548_uptime.state | int %}
  {% set years = uptime // 31536000 %}
  {% set months = (uptime % 31536000) // 2592000 %}
  {% set days = (uptime % 2592000) // 86400 %}
  {% set hours = (uptime % 86400) // 3600 %}
  {% set minutes = (uptime % 3600) // 60 %}
  {{ '%dy ' % years if years else '' }}{{ '%dm ' % months if months else '' }}{{ '%dd ' % days if days else '' }}{{ '%dh ' % hours if hours else '' }}{{ '%dm' % minutes if minutes else '' }}

It updates every second and is flooding my logbook, is there a way to make it update every xx minute?

Thanks in advanced!

Does it mean that this source sensor is updated every 1 sec?

change your template to…

{{ now() - timedelta(seconds=states('sensor.rt_ac57u_v3_6548_uptime') | int(0)) }}

if you’re using yaml, ensure the entity has device_class: timestamp

If you’re using the UI, just select timestamp for the ‘show as’. Don’t add a unit of measurement or state class.

In the UI, it should show a relative time. It will still update every second, but the result should always be the same if your clock is in sync with the sensor.

Then exclude sensor.rt_ac57u_v3_6548_uptime from history.

Lastly, if you can control the state of sensor.rt_ac57u_v3_6548_uptime, I recommend altering it to output a time in seconds when it started. It will be a static number and then you can just let HA do the calculation for you if you set the device_class to timestamp. No template needed.

It does, I can’t really see/find an option to change it

That template worked, but it still shows up in logbook

Should it be like this in the config yaml file?

logbook:
  exclude:
    entities:
      - sensor.rt_ac57u_v3_6548_uptime

just

logbook:
  exclude:
    entities:
      - sensor.rt_ac57u_v3_6548_uptime

should work.

check your config in developer tools → yaml page. If that is successful, then restart for the changes to take effect.

You can convert your existing Template Sensor to a Trigger-based Template Sensor and use a Time Pattern Trigger set to whatever time interval you prefer. The following example will update every 5 minutes.

template:
  - trigger:
      - platform: time_pattern
        minutes: '/5'
    sensor:
      - name: Router Uptime
        unique_id: abc123xyz456
        state: > 
          {% set uptime = states('sensor.rt_ac57u_v3_6548_uptime') | int(0) %}
          {% set years = uptime // 31536000 %}
          {% set months = (uptime % 31536000) // 2592000 %}
          {% set days = (uptime % 2592000) // 86400 %}
          {% set hours = (uptime % 86400) // 3600 %}
          {% set minutes = (uptime % 3600) // 60 %}
          {{ '%dy ' % years if years else '' }}{{ '%dm ' % months if months else '' }}{{ '%dd ' % days if days else '' }}{{ '%dh ' % hours if hours else '' }}{{ '%dm' % minutes if minutes else '' }}

NOTE

Currently, a Trigger-based Template Sensors can only be defined via YAML. So if your existing Template Sensor was defined via the UI (as a Template Sensor helper) you’ll need to delete it and create the Trigger-based version shown in the example above.

Pretty sure his issue is that sensor.rt_ac57u_v3_6548_uptime is updating once a second and flooding his logs. sensor.rt_ac57u_v3_6548_uptime is the source sensor, so it just needs to be excluded from logbook

Agreed; that sensor still needs to be excluded from logbook.