A sensor for the uptime of home assistant and the underlying os would be could.
If you add this into your sensors.yaml, you’ll have an OS uptime sensor
- platform: command_line
name: Uptime OS
command: uptime | sed 's/.*up \([^,]*\), .*/\1/'
You can also use the systemmonitor component which will give you last boot date and time since last boot.
I use this along with Glances for a pretty robust monitoring system:
You are right the system monitor shows the system uptime. But there is no uptime for the hass process itself.
That’s why I said ‘also’.
@runningman84: I have a sensor that reports the last start time of HA. It basically just looks at the last_triggered attribute of a notifcation automation that runs at HA start.
sensors:
ha_last_restart:
value_template: '{% if states.automation.startup_notification and states.automation.startup_notification.attributes.last_triggered %}
{{ as_timestamp(states.automation.startup_notification.attributes.last_triggered) | timestamp_custom("%b %d %X") }}
{% else %}
Never
{% endif %}'
friendly_name: HA Last Restart
A bit of a hack, but it works well.
Can someone please explain why this sensor FAILS to update. It appears to work ONCE - on startup - but the value / state never changes:
- platform: template
sensors:
ha_uptime_sec:
friendly_name: 'Uptime'
unit_of_measurement: 'sec'
value_template: >
{{ (as_timestamp(now()) - as_timestamp(states.input_boolean.ha_uptime.last_changed)) | round(0) }}
- platform: template
sensors:
ha_uptime_days:
friendly_name: 'Raspberry Pi2 Hassbian Uptime'
unit_of_measurement: 'days'
value_template: >
{{ (states.sensor.ha_uptime_sec.state | float / 86400) | round(0) }}
I got the basic idea from everybody’s favorite my favorite HASS guru Ben of @bruhautomation , and it seems to work fine as a trigger condition but not as a sensor.
… oh and I forgot to mention, input_boolean.ha_uptime is just a hidden/dummy input boolean which is never touched.
ha_uptime:
name: Home Assistant Uptime
initial: on
@tggman: You need to add sensor.time
to your sensor config, like this:
- platform: template
sensors:
ha_uptime_sec:
friendly_name: 'Uptime'
unit_of_measurement: 'sec'
value_template: >
{{ (as_timestamp(now()) - as_timestamp(states.input_boolean.ha_uptime.last_changed)) | round(0) }}
entity_id: sensor.time
- platform: time_date
It worked! Thanks so much. I had to add both the entity_id to the uptime sensor AND the sensor platform time_date.
Questions:
(1) I really would like to understand what the entity_id sensor.time is and why it was necessary to add it to this particular sensor in order to get the Uptime sensor to UPDATE properly. Doesn’t the event bus service/update all the sensors at a regular time interval?
(2) Why was adding the sensor platform time_date necessary? Without it, the Uptime sensor failed to update. In short, why does this inter-dependence exist?
Apparently I lack a basic understanding of how sensors are updated and would like to learn. So any help you can give me would be greatly appreciated.
Can I use this command somehow to get the uptime from a remote machine?
yeah , wondering the same thing, how can we use that command for a remote PI for example? did you ever figured this out?
Thanks to all you guys for helping me to figure out how jinja works and how to write scripts. This is my 1st attempt. I also wanted create a sensor to detect the uptime after a restart of the server control so that my automations can run after a few minutes to give HA time to process all the switches and update itself and stabilise.
1st thing is put this in the sensor.yaml file as discussed:-
- platform: uptime
name: Time Online
I gave it a friendly name of Time Online.
And now for the script:-
uptime_after_restart:
friendly_name: "UpTime"
value_template: >-
{% set startdate = states('sensor.time_online')[0:10]+'T00:00:00.000000+02:00' %}
{% set nowdate = now().strftime('%Y-%m-%d')+' 00:00:00.000000+02:00' %}
{% set daydifference = ((as_timestamp(nowdate)-as_timestamp(startdate))/86400 )|round(0) %}
{% set startsecs = (states('sensor.time_online')[17:19]|int)+
(states('sensor.time_online')[14:16]|int)*60+
(states('sensor.time_online')[11:13]|int)*3600 %}
{% set nowdate = now().strftime('%Y-%m-%d %H:%M:%S')+'.000000+02:00' %}
{% set nowsecs = (nowdate[17:19]|int)+
(nowdate[14:16]|int)*60+
(nowdate[11:13]|int)*3600
+ (86400*daydifference) %}
{% set secondsdifference = nowsecs - startsecs %}
The variable are used as follows:-
- startdate -> The date/time when the HAS has restarted in, stamped in the sensor time_online only using year, month and day.
- nowdate -> The date retrieved from the now() which will be your current time only using year, month and day.
- daydifference -> calculating how many days have passwed between startdate and nowdate
- startsecs -> calculates amount of seconds from midnight up till the time recorded in time_online
- nowsecs -> calculates amount of seconds from midnight up till the time recorded in time_online as well as adding amount of seconds per day [86400 for every day difference there is between the 2 dates.
- secondsdifference -> difference between the 2 time periods in seconds
In the developer Tools under states, you will see a new entity [sensor.time_online] which now updates itself every 30 sec.