Home Assistant Uptime

I’m looking for a way to monitor the uptime of a supervised Home Assistant. The current ‘- platform: uptime’ component appears to only return how long since the underlying operating system was restarted. However, Home Assistant itself could have restarted many times since the OS was last rebooted. I’m trying to figure out if anyone has created a sensor and/or template to return the amount of time since the Home Assistant process or docker container was restarted.

The following solution does not appear to work

  - platform: command_line
    name: "HA runtime"
    command: echo "$(($(date +%s) - $(date --date="`systemctl show home-assistant.service -p ActiveEnterTimestamp | awk -F'=' '{print $2}'`" "+%s")))"
    scan_interval: 60
    value_template: >-
      {% set uptime = value | int %}
      {% set minutes = ((uptime % 3600) / 60) | int %}
      {% set hours = ((uptime % 86400) / 3600) | int %}
      {% set days = (uptime / 86400) | int %}

      {%- if uptime < 60 -%}
        Less than a minute
      {%- else -%}
        {%- if days > 0 -%}
          {%- if days == 1 -%}
            1 day
          {%- else -%}
            {{ days }} days
          {%- endif -%}
        {%- endif -%}
        {%- if hours > 0 -%}
          {%- if days > 0 -%}
            {{ ', ' }}
          {%- endif -%}
          {%- if hours == 1 -%}
            1 hour
          {%- else -%}
            {{ hours }} hours
          {%- endif -%}
        {%- endif -%}
        {%- if minutes > 0 -%}
          {%- if days > 0 or hours > 0 -%}
            {{ ', ' }}
          {%- endif -%}
          {%- if minutes == 1 -%}
            1 minute
          {%- else -%}
            {{ minutes }} minutes
          {%- endif -%}
        {%- endif -%}
      {%- endif -%}

It returns the following error in the logs.

2020-12-29 11:41:58 ERROR (SyncWorker_43) [homeassistant.components.command_line] Command failed: echo “$(($(date +%s) - $(date --date=“systemctl show home-assistant.service -p ActiveEnterTimestamp | awk -F'=' '{print $2}'” “+%s”)))”

Running the command ‘echo “$(($(date +%s) - $(date --date=“systemctl show home-assistant.service -p ActiveEnterTimestamp | awk -F'=' '{print $2}'” “+%s”)))”’ from the host operating system works without issue.

Any suggestions?

It reports the time when Home Assistant restarted (which can be more recent than when the OS restarted).