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