elad-bar / ha-edgeos

Integration with EdgeOS (Ubiquiti)
133 stars 24 forks source link

Calculating UpTime from Last Restart #72

Closed jsp196 closed 2 years ago

jsp196 commented 2 years ago

I can't seem to find the "uptime" information in the new version of the HA integration. Is there a way to find this? Or a way to calculate up time from the Last Restart date/time? I have a sensor in HA that shows "Internet has been online for:" and then the uptime of the system. Any guidance would be great.

elad-bar commented 2 years ago

you should have {EdgeOS name} Last Restart sensor with the last restart date

jsp196 commented 2 years ago

I saw that, but there used to be an UPTIME sensor that showed the number of seconds the system had been online. How can I convert "Last Restart Date" to the duration the system has been online?

elad-bar commented 2 years ago

Changing the number of seconds every second can increase the db size and HA dev team recommended change all sensors with uptime to start time so in graphs (grafana or any other tool) you can use it as time since

doublepedaldylan commented 2 years ago

HACS documentation is outdated on that front then, fyi.

elad-bar commented 2 years ago

That's right will update the documentation, thanks

jsp196 commented 2 years ago

For those who still want "elapsed time" instead of boot time, I was able to still use this template with slight modification and still get the right result. Just make sure to turn off logging for this particular sensor.

  - platform: template
    sensors:
      edgeos_uptime:
        friendly_name: "EdgeOS Network Uptime"
        value_template: >-
          {%- set uptime  = now().timestamp() - states('sensor.edgerouter_5_poe_last_restart')|as_timestamp | round -%}
          {%- set sep     = ', ' -%}
          {%- set TIME_MAP = {
              'week': (uptime / 604800) % 10080,
              'day': (uptime / 86400) % 7,
              'hr': (uptime / 3600) % 24,
              'min': (uptime / 60) % 60
          }
          -%}

          {%- for unit, duration in TIME_MAP.items() if duration >= 1 -%}
            {%- if not loop.first -%}
              {{ sep }}
            {%- endif -%}

            {{ (duration | string).split('.')[0] }} {{ unit }}

            {%- if duration >= 2 -%}
              s
            {%- endif -%}
          {%- endfor -%}

          {%- if uptime < 1 -%}
            just now
          {%- endif -%}