elradix

hass/rpi uptime

Jul 2nd, 2019
190
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #==========================
  2. #=== Home Assistant uptime
  3. #==========================
  4. ha_uptime:
  5. friendly_name: HA Uptime
  6. value_template: >
  7. {% if states('sensor.uptime') == '0.0' %}
  8. Just restarted...
  9. {% else %}
  10. {% macro phrase(value, name) %}
  11. {%- set value = value | int %}
  12. {%- set end = 's' if value > 1 else '' %}
  13. {{- '{} {}{}'.format(value, name, end) if value | int > 0 else '' }}
  14. {%- endmacro %}
  15.  
  16. {% set weeks = (states('sensor.uptime') | int / 7) | int %}
  17. {% set days = (states('sensor.uptime') | int) - (weeks * 7) %}
  18. {% set hours = (states('sensor.uptime') | float - states('sensor.uptime') | int) * 24 %}
  19. {% set minutes = (hours - hours | int) * 60 %}
  20.  
  21. {{ [ phrase(weeks, 'week'), phrase(days, 'day'), phrase(hours, 'hr'), phrase(minutes, 'min') ] | select('!=','') | list | join(', ') }}
  22. {% endif %}
  23.  
  24.  
  25. #=========================================
  26. #=== Raspberry Pi last boot date and time
  27. #=========================================
  28. last_boot_date_time:
  29. friendly_name: Last Boot
  30. value_template: >
  31. {% set date = as_timestamp(states('sensor.last_boot')) | timestamp_custom('%d') %}
  32. {% set date = '{:01}'.format(date | int) %}
  33. {% if date in ('1', '21', '31') %}
  34. {% set date = date ~ 'st' %}
  35. {% elif date in ('2', '22') %}
  36. {% set date = date ~ 'nd' %}
  37. {% elif date in ('3', '23') %}
  38. {% set date = date ~ 'rd' %}
  39. {% else %}
  40. {% set date = date ~ 'th' %}
  41. {% endif %}
  42.  
  43. {{ as_timestamp(states('sensor.last_boot')) | timestamp_custom('%H:%M on %a') }} {{ date }} {{ as_timestamp(states('sensor.last_boot')) | timestamp_custom('%b %Y') }}
  44.  
  45.  
  46. #=========================
  47. #=== Raspberry Pi Up Time
  48. #=========================
  49. rpi_uptime:
  50. friendly_name: RPi Uptime
  51. entity_id: sensor.time
  52. value_template: >
  53. {% set up_time = as_timestamp(now()) - as_timestamp(states('sensor.last_boot')) %}
  54.  
  55. {% set minutes = (up_time // 60) | int %}
  56. {% set hours = (minutes // 60) %}
  57. {% set days = (hours // 24) %}
  58. {% set weeks = (days // 7) %}
  59.  
  60. {% set minutes = (minutes % 60) %}
  61. {% set hours = (hours % 24) %}
  62. {% set days = (days % 7) %}
  63.  
  64. {% macro phrase(value, name) %}
  65. {%- set value = value %}
  66. {%- set end = 's' if value > 1 else '' %}
  67. {{- '{} {}{}'.format(value, name, end) if value | int > 0 else '' }}
  68. {%- endmacro %}
  69.  
  70. {% set text = [ phrase(weeks, 'week'), phrase(days, 'day'), phrase(hours, 'hr'), phrase(minutes, 'min') ] | select('!=','') | list | join(', ') %}
  71. {% set last_comma = text.rfind(',') %}
  72. {% if last_comma != -1 %}
  73. {% set text = text[:last_comma] + ' and' + text[last_comma + 1:] %}
  74. {% endif %}
  75.  
  76. {{ text }}
RAW Paste Data