SOLVED: Just went overboard on my UPS in HA, how do I get the seconds converted to mins

Hi All

I just went completely overboard with displaying my UPS in HA with a picture elements card
ups-monitoring
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.

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

Ahh, ok, so that binds stronger, good to know. I corrected the format, forgot about it.
It displays correcetly now, thankyou!

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:

image

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.