fribse
(Kenneths Teknik)
June 12, 2019, 2:59pm
1
Hi All
I just went completely overboard with displaying my UPS in HA with a picture elements card
The numbers and manufacturer etc. is of course fetched with the NUT component.
But as you can see the runtime is shown i seconds, and I would like to show it in mins.
So I tried creating a sensor, first the UPS itself:
- platform: nut
name: UPS Battery backup
host: 1.2.3.4
resources:
- ups.status.display
- ups.load
- ups.realpower.nominal
- ups.mfr
- ups.model
- input.voltage
- battery.charge
- battery.runtime
I’m not sure if I can calculate directly in that NUT platform, so I thought I would just create a new sensor for that:
- platform: template
sensors:
upsruntimemins:
friendly_name: "UPS Runtime"
value_template: "{{ states('sensor.ups_battery_backup_battery_runtime') |int/60|round(0) }}"
And that does give me mins, but with a ton of decimals.
tom_l
June 12, 2019, 3:05pm
2
That’s because the order of operations evaluates | filters
first. So you are rounding the constant 60. Try adding some brackets so that |round()
applies to the whole template:
“{{ ( states('sensor.ups_battery_backup_battery_runtime') | int / 60 ) | round(0) }}”
Also in future please format your pasted code correctly.
Edit: fixed fancy single quotes (stupid mobile phone!).
2 Likes
fribse
(Kenneths Teknik)
June 12, 2019, 3:57pm
3
Ahh, ok, so that binds stronger, good to know. I corrected the format, forgot about it.
It displays correcetly now, thankyou!
petro
(Petro)
June 12, 2019, 4:03pm
4
I use this:
- platform: snmp
host: !secret unifi_bonus_switch
name: Bonus Room Switch Uptime
baseoid: 1.3.6.1.2.1.1.3.0
value_template: >
{%- set time = value | int // 100 %}
{%- set minutes = ((time % 3600) // 60) %}
{%- set minutes = '{}min'.format(minutes) if minutes > 0 else '' %}
{%- set hours = ((time % 86400) // 3600) %}
{%- set hours = '{}hr '.format(hours) if hours > 0 else '' %}
{%- set days = (time // 86400) %}
{%- set days = '{}d '.format(days) if days > 0 else '' %}
{{ 'Less than 1 min' if time < 60 else days + hours + minutes }}
Looks like this:
EDIT: Sorry, thought you were snmp.
This is the same thing that I use for uptime on my APC. It’ll have the same look.
sensor:
- platform: template
sensors:
ups_uptime:
entity_id:
- sensor.time
value_template: >
{%- set time = states('sensor.ups_battery_backup_battery_runtime') | int %}
{%- set minutes = ((time % 3600) // 60) %}
{%- set minutes = '{}min'.format(minutes) if minutes > 0 else '' %}
{%- set hours = ((time % 86400) // 3600) %}
{%- set hours = '{}hr '.format(hours) if hours > 0 else '' %}
{%- set days = (time // 86400) %}
{%- set days = '{}d '.format(days) if days > 0 else '' %}
{{ 'Less than 1 min' if time < 60 else days + hours + minutes }}
6 Likes
This worked for me perfectly after trying different solutions.