tomaae / homeassistant-mikrotik_router

Mikrotik router integration for Home Assistant
Apache License 2.0
301 stars 50 forks source link

[Feature] UPS runtime_left time format #245

Open clau-bucur opened 2 years ago

clau-bucur commented 2 years ago

Is your feature request related to a problem? Please describe.

Currently the attribute "runtime_left" is not eaily used in templates because it's format is not a standard time/timedelta.

Describe the solution you'd like

Would it be possible to format the runtime_left attribute in such a way what it's compatible with the as_timedelta filter ? (taken from docs: Expects data in the format DD HH:MM:SS.uuuuuu, DD HH:MM:SS,uuuuuu, or as specified by ISO 8601 (e.g. P4DT1H15M20S which is equivalent to 4 1:15:20) or PostgreSQL’s day-time interval format (e.g. 3 days 04:05:06))

This way this attribute is easier to be used in automations and scripts that rely on the time left on battery.

Describe alternatives you've considered

Converting the current attribute value inside the template before filtering it with _astimedelta.

tomaae commented 2 years ago

it is in iso8601, just missing pt mark. but I dont know if HA has an element that supports time format without being full datetime.

clau-bucur commented 2 years ago

It has, called timedelta, and this attribute fits perfectly.

tomaae commented 2 years ago

you are talking about python there, not HA. I just had a look and HA knows date, timestamp and duration. Duration would be only one that is in such format, but it is defined as "fixed duration", so not sure. I will have to ask abou that.

defaultsecurity commented 11 months ago

you are talking about python there, not HA. I just had a look and HA knows date, timestamp and duration. Duration would be only one that is in such format, but it is defined as "fixed duration", so not sure. I will have to ask abou that.

I agree with the duration solution. I believe runtime should be passed in integer seconds. Then if the template sensor's "device_class" is "duration", most home assistant cards will display the duration in HH:MM:SS format automatically. History graphs are working as well. I tested it and it works, the card below gets its runtime data in seconds.

ups3

ups2

For those who cannot wait here is an example template script that converts the 1h30m30s format to seconds.

template:
  - sensor:
      - name: "UPS Runtime"
        unique_id: "5cfb7f04-d286-415e-a8bc-35c9007a4db8"
        device_class: "duration"
        unit_of_measurement: "s"
        state_class: "measurement"
        state: >
          {% set time = state_attr('binary_sensor.mikrotik_hex_ups', 'runtime_left') %}
          {% set seconds = 0 | int %}
          {% if 'd' in time %} {% set seconds = seconds + (time.split("d")[0]|int)*86400 %} {% set time = time.split("d")[1] %} {% endif %}
          {% if 'h' in time %} {% set seconds = seconds + (time.split("h")[0]|int)*3600 %} {% set time = time.split("h")[1] %} {% endif %}
          {% if 'm' in time %} {% set seconds = seconds + (time.split("m")[0]|int)*60 %} {% set time = time.split("m")[1] %} {% endif %}
          {% if 's' in time %} {% set seconds = seconds + (time.split("s")[0]|int) %} {% endif %}
          {{seconds}}