renemarc/home-assistant-config

View on GitHub
sensors/system.yaml

Summary

Maintainability
Test Coverage
#
# System monitor
#
# Lots of geeky details about the server hardware.
#
# @link https://www.home-assistant.io/integrations/systemmonitor/
#
- platform: systemmonitor
  scan_interval: 60
  resources:
    - type: disk_use
      arg: /home
    - type: disk_free
      arg: /home
    - type: load_1m
    - type: load_5m
    - type: load_15m
    - type: memory_use
    - type: memory_free
    - type: processor_use
    - type: last_boot


#
# Version
#
# @link https://www.home-assistant.io/integrations/version/
#
- platform: version


#
# Friendly version report
#
# Useful for HADashboard.
#
# @see /appdaemon/dashboards/Status.dash
#
- platform: template
  sensors:
    version:
      friendly_name: Version
      friendly_name_template: >-
        {% if states('binary_sensor.updater') == 'on' %}
          Update Available
        {% else %}
          Version Installed
        {% endif %}
      icon_template: >-
        {% if states('binary_sensor.updater') == 'on' %}
          mdi:update
        {% else %}
          mdi:home-assistant
        {% endif %}
      value_template: >-
        {% set update = states('binary_sensor.updater') %}
        {% set version = state_attr('binary_sensor.updater', 'newest_version') %}

        {% if update == 'on' and version != None %}
          {{ version }}
        {% else %}
          {{ states('sensor.current_version') }}
        {% endif %}
      attribute_templates:
        current_version: >-
          {{ states('sensor.current_version') }}
        newest_version: >-
          {{ state_attr('binary_sensor.updater', 'newest_version') }}
        release_notes: >-
          {{ state_attr('binary_sensor.updater', 'release_notes') }}
        release_notes_html: >-
          {% set release_notes = state_attr('binary_sensor.updater', 'release_notes') %}

          {% if release_notes != None %}
            <a href="{{ release_notes }}" target="_new" style="color:var(--secondary-text-color);">Release notes</a>
          {% endif %}

    version_friendly:
      friendly_name: Home Assistant Version
      icon_template: "mdi:home-assistant"
      value_template: >-
        {% set version = states('sensor.current_version') %}

        Home Assistant {% if version != 'unknown' -%}
          {{ version }}
        {%- endif %}

    update_available:
      friendly_name: Update Available
      icon_template: "mdi:update"
      value_template: >-
        {% set version = state_attr('binary_sensor.updater', 'newest_version') %}

        {% if version != None %}
          Update available: {{ version }}
        {% endif %}


#
# Disk space
#
# @see /appdaemon/dashboards/Status.dash
#
- platform: template
  sensors:
    disk_use_percent:
      friendly_name: Disk Use /home
      icon_template: "mdi:harddisk"
      unit_of_measurement: '%'
      value_template: >-
        {% set disk_use = states('sensor.disk_use_home') | float(-1) %}
        {% set disk_free = states('sensor.disk_free_home') | float(-1) %}

        {% if disk_use >= 0 and disk_free >= 0 %}
          {{ (100 * disk_use / (disk_use + disk_free)) | round }}
        {% else %}
          unknown
        {% endif %}

    disk_free_percent:
      friendly_name: Disk Free /home
      icon_template: "mdi:harddisk"
      unit_of_measurement: '%'
      value_template: >-
        {% set disk_use = states('sensor.disk_use_percent') | int(-1) %}

        {% if disk_use >= 0 %}
          {{ 100 - disk_use }}
        {% else %}
          unknown
        {% endif %}


#
# RAM space
#
# @see /appdaemon/dashboards/Status.dash
#
- platform: template
  sensors:
    memory_use_percent:
      friendly_name: Memory Use
      icon_template: "mdi:memory"
      unit_of_measurement: '%'
      value_template: >-
        {% set memory_use = states('sensor.memory_use') | float(-1) %}
        {% set memory_free = states('sensor.memory_free') | float(-1) %}

        {% if memory_use >= 0 and memory_free >= 0 %}
          {{ (100 * memory_use / (memory_use + memory_free)) | round }}
        {% else %}
          unknown
        {% endif %}

    memory_free_percent:
      friendly_name: Memory Free
      icon_template: "mdi:memory"
      unit_of_measurement: '%'
      value_template: >-
        {% set memory_use = states('sensor.memory_use_percent') | int(-1) %}

        {% if memory_use >= 0 %}
          {{ 100 - memory_use }}
        {% else %}
          unknown
        {% endif %}


#
# Host uptime
#
# @see /appdaemon/dashboards/Status.dash
#
- platform: template
  sensors:
    uptime_host:
      friendly_name: Server Uptime
      icon_template: "mdi:server"
      entity_id:
        - sensor.time
      value_template: >-
        {% set lb = states('sensor.last_boot') %}

        {% if lb != 'unknown' %}
          {% set seconds = (as_timestamp(now()) - as_timestamp(lb))|int %}
          {% set days = (seconds / 86400)|int %}
          {% set seconds = seconds % 86400 %}
          {% set hours = (seconds / 3600)|int %}
          {% set seconds = seconds % 3600 %}
          {% set minutes = (seconds / 60)|int %}
          {% set seconds = seconds % 60 %}

          {% if hours == 0 and minutes == 0 %}
            {{ time }} seconds
          {% else %}
            {%- if days > 0 -%}
              {%- if days == 1 -%}
                1 day
              {%- else -%}
                {{ days }} days
              {%- endif -%}
            {%- endif -%}
            {%- if hours > 0 and days < 7 -%}
              {%- if days > 0 -%}
                {{ ', ' }}
              {%- endif -%}
              {%- if hours == 1 -%}
                1 hour
              {%- else -%}
                {{ hours }} hours
              {%- endif -%}
            {%- endif -%}
            {%- if days == 0 and minutes > 0 -%}
              {%- if hours > 0 -%}
                {{ ', ' }}{{ minutes }} min
              {%- else -%}
                {%- if minutes == 1 -%}
                  1 minute
                {%- else -%}
                  {{ minutes }} minutes
                {%- endif -%}
              {%- endif -%}
            {%- endif -%}
          {% endif %}
        {% else %}
          unknown
        {% endif %}


#
# Home Assistant uptime
#
# For Home Assistant process/container.
#
# @see /appdaemon/dashboards/Status.dash
#
# @link https://www.home-assistant.io/integrations/uptime/
#
- platform: uptime
  name: Uptime
  scan_interval: 60
  unit_of_measurement: 'hours'

- platform: template
  sensors:
    uptime_friendly:
      friendly_name: HASS Uptime
      icon_template: "mdi:home-assistant"
      entity_id:
        - sensor.uptime
      value_template: >-
        {% set uptime = states('sensor.uptime') | float(-1) %}

        {% if uptime >= 0 %}
          {% set time = (3600 * uptime) | int %}
          {% set minutes = ((time % 3600) / 60) | int %}
          {% set hours = ((time % 86400) / 3600) | int %}
          {% set days = (time / 86400) | int %}

          {% if time < 60 %}
            {{ time }} seconds
          {% else %}
            {%- if days > 0 -%}
              {%- if days == 1 -%}
                1 day
              {%- else -%}
                {{ days }} days
              {%- endif -%}
            {%- endif -%}
            {%- if hours > 0 and days < 7 -%}
              {%- if days > 0 -%}
                {{ ', ' }}
              {%- endif -%}
              {%- if hours == 1 -%}
                1 hour
              {%- else -%}
                {{ hours }} hours
              {%- endif -%}
            {%- endif -%}
            {%- if days == 0 and minutes > 0 -%}
              {%- if hours > 0 -%}
                {{ ', ' }}{{ minutes }} min
              {%- else -%}
                {%- if minutes == 1 -%}
                  1 minute
                {%- else -%}
                  {{ minutes }} minutes
                {%- endif -%}
              {%- endif -%}
            {%- endif -%}
          {% endif %}
        {% else %}
          unknown
        {% endif %}


#
# CPU average load
#
# @see /appdaemon/dashboards/Status.dash
#
- platform: template
  sensors:
    average_load:
      friendly_name: Average load
      icon_template: "mdi:chip"
      value_template: >-
        {% set load1 = states('sensor.load_1m') | float(-1) %}
        {% set load5 = states('sensor.load_5m') | float(-1) %}
        {% set load15 = states('sensor.load_15m') | float(-1) %}

        {% if load1 > 0 and load5 > 0 and load15 > 0 %}
          {% set load1f = '{0:0.2f}'.format(load1) %}
          {% set load5f = '{0:0.2f}'.format(load5) %}
          {% set load15f = '{0:0.2f}'.format(load15) %}

          {{ load1f }} {{ load5f }} {{ load15f }}
        {% endif %}


#
# CPU cores count
#
# Useful to get an idea of the system load. Scan interval is set to daily,
# because it's quite unlikely that cores change ...often unless one plays with
# the VM's settings all the time.
#
# The actual command is kept in the secrets.yaml file to allow for
# platform-specific variations.
#
# On Hassio and others:
#   `grep 'model name' /proc/cpuinfo | wc -l`
#
# On macOS:
#   `sysctl -n hw.ncpu`
#
# @see /customize.yaml
# @see /secrets-dummy.yaml
#
# @link https://www.home-assistant.io/integrations/command_line/
#
- platform: command_line
  name: CPU Cores
  scan_interval: 86400
  command: >-
    if [ -e /proc/cpuinfo ]; then
      grep 'model name' /proc/cpuinfo | wc -l
    elif [ $(uname -s) == 'Darwin' ]; then
      sysctl -n hw.activecpu
    fi


#
# CPU temperature
#
# On Hassio, Raspberry Pi and others:
#   `cat /sys/class/thermal/thermal_zone0/temp`
#
# On systems with lm_sensors installed:
#   `/usr/bin/sensors | grep "Core 0" | cut -c16-19`
#
# On macOS (osx-cpu-temp available via Homebrew):
#   `osx-cpu-temp -C`
#
# @see /appdaemon/dashboards/Status.dash
#
# @link https://community.home-assistant.io/t/displaying-cpu-temperature-within-home-assistant/8641
# @link https://www.home-assistant.io/integrations/command_line/
#
- platform: command_line
  name: CPU Temp
  scan_interval: 60
  unit_of_measurement: '°C'
  command: >-
    if [ -e /sys/class/thermal/thermal_zone0/temp ]; then
      cat /sys/class/thermal/thermal_zone0/temp
    elif [ -x /usr/bin/sensors ]; then
      /usr/bin/sensors | grep "Core 0" | cut -c16-19
    elif command -v osx-cpu-temp &> /dev/null; then
      osx-cpu-temp -C
    fi
  value_template: >-
    {% if value != '' %}
      {% set value = value | regex_replace('[^-0-9\.]', '') | float %}
      {% if value >= 200 %}
        {% set value = value | multiply(0.001) %}
      {% endif %}
      {{ value | round }}
    {% else %}
      unknown
    {% endif %}


#
# File sizes
#
# @see /appdaemon/dashboards/Status.dash
#
# @link https://www.home-assistant.io/integrations/filesize/
#
- platform: filesize
  file_paths:
    - !secret filesize_database
    - !secret filesize_log

- platform: template
  sensors:
    database_size:
      friendly_name: "Database Size"
      icon_template: "mdi:database"
      unit_of_measurement: 'MiB'
      value_template: >-
        {{ states('sensor.home_assistant_v2_db') | round }}

    log_size:
      friendly_name: "Log Size"
      icon_template: "mdi:history"
      unit_of_measurement: 'KiB'
      value_template: >-
        {% set size = state_attr('sensor.home_assistant_log', 'bytes') %}

        {% if size != None %}
          {{ (size / 1024) | round }}
        {% else %}
          unknown
        {% endif %}


#
# Git current branch
#
# Returns the selected local git branch. This command supports up to 3 levels
# of depth: `group/name/extra`.
#
# On systems with git installed:
#   `git -C /config/.git symbolic-ref --short HEAD`
#
# On Hassio and others where the git command isn't available:
#   'cat /config/.git/HEAD | cut -d \  -f 2 | cut -d / -f 3,4,5'
#
# @link https://stackoverflow.com/a/33133769
#
- platform: command_line
  name: "Git Branch"
  command: >-
    if command -v git &> /dev/null; then
      git -C $(pwd)/.git symbolic-ref --short HEAD
    else
      cat $(pwd)/.git/HEAD | cut -d \  -f 2 | cut -d / -f 3,4,5
    fi


#
# Server port
#
# Used as a flexible placeholder for cameras.
#
# @see /cameras/wallpapers.yaml
#
- platform: template
  sensors:
    server_port:
      friendly_name: "Server Port"
      icon_template: "mdi:ethernet"
      value_template: !secret http_server_port