Possible to monitor homeassistant uptime?

I know that it’s possible to monitor the uptime for the system on which homeassistant is running (using the system monitor template sensor), but I have homeassistant running in a Docker on a Synology device – restarting the homeassistant Docker instance doesn’t change that sensor’s value (which monitors the duration since the last Synology reboot). I’d like to be able to monitor the time since homeassistant started, so I can use that as a condition on some automations.

For example, I have an automation which sets the initial values for some thermostat temperature input_sliders on event ‘homeassistant_start’. I also have persistent notifiers that pop up when those sliders are changed, but I don’t want the persistent notifier to pop up on every restart. I’d like to make the time since restart a condition for the automation that sets the notifier, but I can’t figure out how. Any help would be appreciated.

Not sure if this would work since I haven’t tested it, but you could set an automation to trigger on HA boot to say switch an input boolean on.

Then you could compare the time that input boolean was changed with the current time with a sensor template.

This is not complete code, by something like this:

{{ as_timestamp(now()) - as_timestamp(state.input_boolean.ha_on.last_updated) }}

This will return to your config a total number of seconds that HA has been running.

You’d just need to set an automation to do something to trigger input_boolean.ha_on to on if it was off (when HA first boots)

That’s a great idea, thanks! I wasn’t aware of the .last_updated attribute. I’ll give it a try and report back.

any luck with this @joshlawless ?

Another idea is to just trigger an event on homeassistant.start and then as your first action do a delay for XX:XX:XX and then execute whatever it is you needed to execute. If you reset HomeAssistant prior to the delay finishing then the automation should never complete.

- alias: On HA Start
  trigger:
  - platform: event
    event_type: homeassistant_start
  action:
  - delay: 00:05:00
  - service: script.turn_on
    entity_id: script.on_start

Trying this out, and I have automation I made to just turn off one of my automations I don’t want to use right now on start.

Then in a template sensor I used:

{{ as_timestamp(now()) - as_timestamp(states.automation.hastart.attributes.last_triggered) }}

which renders a sensor and works in the template dev screen but only appears to update once when Ha first starts. I tried adding the scan interval option to the sensor but it doesn’t appear to update with that either.

I use a command_line sensor to achieve this:

- platform: command_line
  name: "HASS uptime"
  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 seconds = uptime % 60 %}
    {% set minutes = ((uptime % 3600) / 60) | int %}
    {% set hours = ((uptime % 86400) / 3600) | int %}
    {% set days = (uptime / 86400) | int %}

    {%- 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 -%}
    {%- if seconds > 0 -%}
      {%- if days > 0 or hours > 0 or minutes > 0 -%}
        {{ ', ' }}
      {%- endif -%}
      {%- if seconds == 1 -%}
        1 second
      {%- else -%}
        {{ seconds }} seconds
      {%- endif -%}
    {%- endif -%}

The output of the command is the uptime in seconds. Using the value_template, it looks like this:

Come to think of it, I should probably edit it to leave out the seconds. The sensor is only refreshed once every 60 seconds anyway.

2 Likes

I tried this, but It will not go back to zero after I reboot the HASS or the entire rPi. Is it suposed to do that?

Mine always goes back to zero (= no more than a few minutes in reality) in both cases.

Dumb question from a HA noob, but how did you get the uptime sensor to display like this in HA?

Thank you for your help!

Look up a few posts where I shared the code. I have changed it a little since then to remove the seconds: I use a scan_interval of 60 seconds, so including seconds is pointless. Anyway, my code now looks like this:

- 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 -%}

And the result:

5 Likes

Got it now - thank you for sharing this. I was just getting the round sensor until I realized I needed to use the sensor in a group. Working perfectly now.

Thanks again for your help!

1 Like

When I copy and past the code it says “23 hours, 24 minutes” when the clock is 23:24, it says the clock and will att midnight start from 0

I’ve never encountered anything like it so I wouldn’t quite know where to start troubleshooting. Maybe it has to do with how you installed Home Assistant, so the command could require changes to suit your install. In my case, I installed Home Assistant using the All-in-One installer a bit over a year ago.

1 Like

Yes it is strange! I installed the hassbian image so there is an different for sure.

Works for me, you are the man. Thanks for sharing.

2 Likes

I am having the same error as @Stimo with this script. The issue seems to be that systemctl show home-assistant.service -p ActiveEnterTimestamp on my Pi running hassbian returns ActiveEnterTimestamp= (perhaps not unreasonably, since ActiveEnterTimestamp is not returned by that systemctl command!)

Has anyone explored any other routes to determine the number of seconds that HA has been up?

Here is another solution.
BTW, i think there will be an uptime sensor in the next release.

1 Like

I gavs up this script, couldn’t get it to work. And as @VDRainer said, there is a uptime sensor in a release soon anyways.