jcallaghan / home-assistant-config

My Home Assistant configuration & documentation.
https://www.jcallaghan.com/
MIT License
175 stars 8 forks source link

Home Assistant start time and time running sensor β±πŸ“ˆπŸš¦ #178

Closed jcallaghan closed 4 years ago

jcallaghan commented 4 years ago

A few times I've wondered how long Home Assistant has been running or if I'm testing something when it last started (my memory sucks and I forget if I've restarted it or not).

My plan is to trigger an automation when Home Assistant starts. This will send me a notification and update an input_datetime sensor. And just for fun, I'll create a template sensor to calculating the uptime (days:hours:minutes).

adonno commented 4 years ago

I will ’borrowβ€˜ that idea

jcallaghan commented 4 years ago

Templating fun

While relative_time filter exists, it only displays the highest value i.e. 2 days. I wanted to know my uptime in days, hours and minutes.

Date time: {{ states('sensor.date_time') }}
Home Assistant started: {{ states('input_datetime.home_assistant_started') }}

Date time timestamp: {{ as_timestamp(now()) }}
Started timestamp: {{ as_timestamp(states('input_datetime.home_assistant_started')) }}

{% set totalminutes = (as_timestamp(now()) - as_timestamp(states('input_datetime.home_assistant_started'))) / 60 %}
{% set hours = (totalminutes / 60) | round(0,'floor') %}
{% set minutes = (totalminutes % 60) | round(0,'floor') %}
{% set days = (hours / 24) | round(0,'floor') %}
{% set weeks = (days / 7) | round(0,'floor') %}
{% set todayhours = hours - (days * 24) | round(0,'floor') %}

Total Minutes: {{ totalminutes }}
Weeks: {{ weeks }}
Days: {{ days }}
Hours: {{ hours }}
Minutes: {{ minutes }}
Today hours: {{ todayhours }}

{% if totalminutes <= 59 %}
00:00:{% if minutes <= 9 %}0{{ minutes }}{% else %}{{ minutes }}{% endif %}
{% elif totalminutes >= 60 and totalminutes <= 1400 %}
00:{% if hours <= 9 %}0{{ hours }}{% else %}{{ hours }}{% endif %}:{% if minutes <= 9 %}0{{ minutes }}{% else %}{{ minutes }}{% endif %}
{% elif totalminutes >= 1401 and totalminutes <= 9800 %}
{% if days <= 9 %}0{{ days }}{% else %}{{ days }}{% endif %}:{% if todayhours <= 9 %}0{{ todayhours }}{% else %}{{ todayhours }}{% endif %}:{% if minutes <= 9 %}0{{ minutes }}{% else %}{{ minutes }}{% endif %}
{% endif %}

image

jcallaghan commented 4 years ago

Relates to #179.

jcallaghan commented 4 years ago

Friendly attribute

To replicate relative_time filter I've created a friendly attribute that displays the uptime in a more readable way. I can then use this attribute in alerts. I think this would be great for @CCOSTAN 's briefing and Tweets.

Just minutes... image

Hours and minutes... image

Days, hours and minutes... image

jcallaghan commented 4 years ago

HTML5 notification example

Raw sensor value (DD:HH:MM)

image

Or with the latest commit which uses the friendly attribute that I've added to the uptime sensor.

image

jcallaghan commented 4 years ago

Restart time idea

I guess I could also add another date time to track the restart time when the restart is fired. Then on start calculate the time difference to work out restart time. I don't need this right now but it would be a helpful statistic to track. I know this is something that is reported in the logs if I ever need it.

jcallaghan commented 4 years ago

Shared on Twitter.

image

CCOSTAN commented 4 years ago

Friendly attribute

To replicate relative_time filter I've created a friendly attribute that displays the uptime in a more readable way. I can then use this attribute in alerts. I think this would be great for @CCOSTAN 's briefing and Tweets.

I've actually had this for a while now.. (since 2017) . :) https://github.com/CCOSTAN/Home-AssistantConfig/blob/master/config/sensor/hass_stats.yaml

It's a different way to do it .. this one uses a command line sensor every 12 minutes. Might be worth changing though for efficiency as per Franck.

GitHub
CCOSTAN/Home-AssistantConfig
:house: Home Assistant configuration & Documentation for my Smart House. Write ups, videos, part lists and links throughout. Be sure to :star: it. Updated FREQUENTLY! - CCOSTAN/Home-AssistantC...