Help with basics of converting and presenting router up time in days

I am a total newbie with HA so apologies in advance (and I have read posts/info but i’m lost)!

HA setup and running fine. Have added an integration for my router (UPnP integration)
Added an Entity Card to the dashboard that shows IP and up time for the router
Up time is shown in seconds so I thought it would be easy to change this to days

But it goes down hill from here…
From what I understand I need to create a ‘template’ do achieve ths
I have made a start in dev tools - template and managed to return the value - see below

{% set total_seconds = states('sensor.rt_ax86s_uptime') %}
The Router uptime is {{total_seconds}} in seconds (but the value is a string not number)

but when I try to divide total_seconds to get to days I get an error. I think it is that the attribute/state is not a number?

But even if I can get this to work, I just don’t understand where / how I actually make the changes so that it displays correctly in the dashboard.

I think there must be a dummies guide that I am missing :slight_smile:

Well I hope you folks can help me. HA looks powerful but possibly a bit technical for me.

You can convert a string to a number with |int or |float

For the template editor:

This is a string: {{ states('sensor.rt_ax86s_uptime') }} seconds
This is a number: {{ states('sensor.rt_ax86s_uptime')|int(0) }} seconds
So: {{ (states('sensor.rt_ax86s_uptime')|int(0)) / 86400 }} is days
And: {{ ((states('sensor.rt_ax86s_uptime')|int(0)) / 86400)|round(2) }} is a bit neater!

Note where the brackets go: on the last one, for example, you need to round the result, not just the 86400.

The (0) on the int filter is a default value: if, for some reason, your sensor isn’t working, the template will return 0 rather than throwing an error. This is discussed here:

Thanks. Making progress.

I can now create a variable with the up time in the correct format in the template editor but how do I get this into the dashboard view - e.g. within a Entities Card Configuration?

Do I need to add the code inside the Card configuration? I was hoping that I could create a new Entity that I could select to present the data.

Good if someone could show me how I get this into a dashboard view and then I think I have the basics

That’s the way to do it, by creating a template sensor:

So, for example:

template:
  - sensor:
      - name: "RT AX86S uptime in days"
        unit_of_measurement: "days"
        state: "{{ ((states('sensor.rt_ax86s_uptime')|int(0)) / 86400)|round(2) }}"

That will give you sensor.rt_ax86s_uptime_in_days that you can use in the dashboard.

I must be so close or stupid as I have been trying this but I can’t work out where i need to do this. Is it in the configuration.yaml file or somehow in the UI?

The documentation and examples I have found are not that clear on how/where template code goes

That goes in configuration.yaml if you haven’t yet split that file out. Then you need to reload template entities (Developer Tools / YAML) or restart the whole system to get it working.

Thank you. I was trying to write the code inside the dashboard element for a looong time. Lol

Is is good practice to split the configuration.yaml or can I do this later if config gets complicated?

You can do it directly in a card if you use this

Thanks everyone - I have this working. I can see how configuration can get complicated and why it needs to be backed up or maintained in github…

image

@nickrout - thanks for the tip. Will it work when using a docker container install for HA?

installed template-entitiy-row via HACS and this working too. So now I have 2 ways to make this work.

Thanks again all

Yes, why wouldn’t it?

Just very new to HA. I was thinking there were some limitations with the docker install of HA but have managed to install HACS and get this all working.

Thanks again for your help