HA 0.116 / Supervisor 247 - Core and Supervisor & Addon Stats

YES!

I have been chasing down an intermittent (but prolonged - requiring a restart to stop) high core CPU use for weeks. Profiler and py-spy have failed to reveal the culprit. So my next option was going to be disabling addons one by one.

I’ll implement these sensors instead and see if that helps track the issue down. Thank you.

EDIT: haven’t seen one of these for a while:

warn

All appears to be working though:

Well that was a dead end. Though I am beginning to suspect something is amiss with the api call for the core cpu %. This:

- platform: rest
  resource: https://mydomain_redacted.duckdns.org/api/hassio/core/stats
  name: Core CPU
  unit_of_measurement: '%'
  value_template: '{{ value_json.data.cpu_percent }}'
  scan_interval: 30
  headers:
    Authorization: !secret ha_api_token
    Content-Type: application/json
  json_attributes_path: "$.data"
  json_attributes:
    - memory_percent

Is currently showing:
Screenshot_2020-10-14 Administration - Home Assistant
And as you can see from the graph it has been growing steadily up.

However, monitoring the same statistic in the supervisor it never gets above 7%:
Untitled

EDIT: I just saw the supervisor core cpu monitor hit 98%, very briefly - about the same time as the api sensor updated. Is the cpu API call causing the cpu use to skyrocket?

Why does restarting home assistant stop this happening?

EDIT2: in case this is caused by multiple api calls happening at once I have updated my rest sensors to use different prime number scan intervals, from 23 to 71 seconds.

One more update. After speaking with Ludeeus on Discord, rather than use the external API access like:

  resource: https://mydomain_redacted.duckdns.org/api/hassio/core/stats

If you use internal access to the API this considerably reduces the overhead. As I use duckDNS and its security certs I have to use https and so accessing

    resource: http://supervisor/core/stats

will not work for me. If you can access home assistant locally via http use that resource. However if you use TLS certs directly in home assistant you can can get around this by using a command line sensor instead:

- platform: command_line
  name: Core CPU
  command: 'curl -sSL -H "Authorization: Bearer $SUPERVISOR_TOKEN" http://supervisor/core/stats'
  unit_of_measurement: '%'
  value_template: "{{ (value_json.data.cpu_percent|float/4)|round(2) }}"
  scan_interval: 60
  json_attributes: 
    - data

- platform: command_line
  name: Supervisor CPU
  command: 'curl -sSL -H "Authorization: Bearer $SUPERVISOR_TOKEN" http://supervisor/supervisor/stats'
  unit_of_measurement: '%'
  value_template: "{{ (value_json.data.cpu_percent|float/4)|round(2) }}"
  scan_interval: 60
  json_attributes: 
    - data

- platform: template
  sensors:
    core_memory:
      friendly_name: "Core RAM"
      value_template:  "{{ (state_attr('sensor.core_cpu', 'data')|to_json|from_json).memory_percent }}"
      unit_of_measurement: '%'
    supervisor_memory:
      friendly_name: "Supervisor RAM"
      value_template: "{{ (state_attr('sensor.supervisor_cpu', 'data')|to_json|from_json).memory_percent }}"
      unit_of_measurement: '%'

Note that as I am using a four thread CPU the percent CPU value has to be divided by 4. Adjust the template for your hardware.

Finally an addon example for completeness:

- platform: command_line
  name: Grafana CPU
  command: 'curl -sSL -H "Authorization: Bearer $SUPERVISOR_TOKEN" http://supervisor/addons/a0d7b954_grafana/stats'
  unit_of_measurement: '%'
  value_template: "{{ (value_json.data.cpu_percent|float/4)|round(2) }}"
  scan_interval: 60
  json_attributes: 
    - data

- platform: template
  sensors:
    grafana_memory:
      friendly_name: "Grafana RAM"
      value_template: "{{ (state_attr('sensor.grafana_cpu', 'data')|to_json|from_json).memory_percent }}"
      unit_of_measurement: '%'

This has considerably reduced the CPU % when

1 Like

as I am on Homeassistant OS and always use the !secret for the bearer token, please le me ask where your token is stored when you use "Authorization: Bearer $SUPERVISOR_TOKEN" ?

or in other words, can I rewrite this:

  - platform: rest
    name: Hassio Rpi4 config
    resource: !secret resource_hassio_rpi4_config
    value_template: > #components|list|count}}
      {{value_json.version}}
    json_attributes:
      - components
      - unit_system
      - config_dir
    headers:
      Content-Type: application/json
      Authorization: !secret api_bearer_token
      User-Agent: Home Assistant REST sensor
    scan_interval: 3600

to the sensors you use with the ‘http://supervisor/core/stats’ as resource?

$SUPERVISOR_TOKEN is like a system variable. No need to define a token! Just use that template verbatim. I don’t know if it can be used in the rest Authorisation option though. I suspect not.

finally! with that info I could use this thread Update notifications! Core, HACS, Supervisor and Addons and the sensor:

  - platform: command_line
    name: Supervisor updates
    command: 'curl http://supervisor/supervisor/info -H "Authorization: Bearer $SUPERVISOR_TOKEN" | jq ''{"newest_version":.data.version_latest,"current_version":.data.version,"addons":[.data.addons[] | select(.version != .installed)]}'''
    value_template: >
      {{value_json.addons|length}}
    json_attributes:
      - newest_version
      - current_version
      - addons

to pull all info from supervisor I was looking for. Only thing is:

None od my add-ons seem to be there… I noticed something this morning already when installing the Portainer, and all it could find was the hassio_observer

where have they all gone?!?

The home assistant containers are hidden by default. Read the portainer addon docs, it tells you how to un-hide them. I’d tell you but it’s ages since I did it and I’ve forgotten.

ok I will, but is that the same reason they don’t show up in the sensor?

No, probably not. Just a sec. Let me check my addon sensor.

EDIT: I don’t see any addons listed either. I think that attribute only shows addons with updates available.

ok thanks.
as for the Portainer if you have time… I did find the hidden containers, but can only Remove them :wink:

and the Add-ons are listed as Images:

They are not the containers. They are the instructions to hide the containers.

Access to these containers can be gained by going into Portainer → Settings → Hidden containers. Then delete the listed hidden labels (io.hass.type labels). Only do this if you know what you’re doing!

Tom,

Why are you dividing by 4? That sensor tells you how much CPU is used by supervisor. Why does the number of cores matter?

Also I have similar for all addons etc… check my system monitor package in my repo.

Because the report returns the sum of all cores.

Ok but if supervisor is telling you CPU percentage then can that not be spread over 4 cores?

Yes it can, which is why you have to divide by 4 to get a real percentage. Consider this, if the process is using 100% of all 4 cores it is reported as 400%, which is kinda daft. Hence to get a real pet-cent (per 100) you divide by the number of cores. Thus it is now reported as 100% use of the total available cpu resources.

If the process was using 100% of one of four cores, this would now be reported as 25% instead of 100%.

Reporting 100% if not dividing by 4 may mislead you into thinking all resources are used unless you remember the total available is (the mathematically improper) 400%.

1 Like

Ok. I would have assumed and expected that it was taking that into account. I will have to investigate this on my system.

It’s definitely using the sum of the cores on my system, I used to see usage in excess of 200%. Also see: https://discord.com/channels/330944238910963714/332167321311510530/766620091302412300

Also I may have a way to implement these sensors over https locally using the rest sensor (less overhead than the command line sensor). Testing today…

1 Like

Ok so you can use an https local api endpoint if you set verify_ssl to false an use this endpoint shown below.

Core and Supervisor (set the template divisor to the number of threads your CPU supports):

- platform: rest
  resource: https://homeassistant:8123/api/hassio/core/stats
  name: Core CPU
  unit_of_measurement: '%'
  value_template: '{{ (value_json.data.cpu_percent|float/4)|round(2) }}'
  scan_interval: 60
  verify_ssl: false
  headers:
    Authorization: !secret ha_api_token
    Content-Type: application/json
  json_attributes_path: "$.data"
  json_attributes:
    - memory_percent

- platform: rest
  resource: https://homeassistant:8123/api/hassio/supervisor/stats
  name: Supervisor CPU
  unit_of_measurement: '%'
  value_template: '{{ (value_json.data.cpu_percent|float/4)|round(2) }}'
  scan_interval: 60
  verify_ssl: false
  headers:
    Authorization: !secret ha_api_token
    Content-Type: application/json
  json_attributes_path: "$.data"
  json_attributes:
    - memory_percent

- platform: template
  sensors:
    core_memory:
      friendly_name: "Core RAM"
      value_template: "{{ state_attr('sensor.core_cpu', 'memory_percent') }}"
      unit_of_measurement: '%'
    supervisor_memory:
      friendly_name: "Supervisor RAM"
      value_template: "{{ state_attr('sensor.supervisor_cpu', 'memory_percent') }}"
      unit_of_measurement: '%'

Addon

- platform: rest
  resource:  https://homeassistant:8123/api/hassio/addons/core_duckdns/stats
  name: DuckDNS CPU
  unit_of_measurement: '%'
  value_template: '{{ (value_json.data.cpu_percent|float/4)|round(2) }}'
  scan_interval: 60
  verify_ssl: false
  headers:
    Authorization: !secret ha_api_token
    Content-Type: application/json
  json_attributes_path: "$.data"
  json_attributes:
    - memory_percent

- platform: template
  sensors:
    duckdns_memory:
      friendly_name: "DuckDNS RAM"
      value_template: "{{ state_attr('sensor.duckdns_cpu', 'memory_percent') }}"
      unit_of_measurement: '%'

Secret (Long Lived Access Token, set in your profile):

ha_api_token: "Bearer eyJ0eXA..."

would you know of a way to get this working over the local resource, (vs the now configured duckdns resource) in

  - platform: rest
    name: Hassio Rpi4 config
    resource: !secret resource_hassio_rpi4_config
    value_template: > #components|list|count}}
      {{value_json.version}}
    json_attributes:
      - components
      - unit_system
      - config_dir
    headers:
      Content-Type: application/json
      Authorization: !secret api_bearer_token
      User-Agent: Home Assistant REST sensor
    scan_interval: 3600

and resource:

https://redacted.duckdns.org:1234/api/config

?
tried a few endpoints but with no real luck so far…or should I simply use that

https://homeassistant:8123/api/config

cant be that simple??

update
no it can’t…

anyone got the Portainer add-on working?

this is the info

- description: Manage your Docker environment with ease
  icon: true
  installed: 1.2.2
  logo: true
  name: Portainer
  repository: a0d7b954
  slug: a0d7b954_portainer
  state: stopped
  version: 1.2.2

so I am using

  - platform: command_line
    name: Stats Portainer
    command: >
      curl -sSL -H "Authorization: Bearer $SUPERVISOR_TOKEN" http://supervisor/addons/a0d7b954_portainer/stats
    unit_of_measurement: '%'
    value_template: >
      {{value_json.data.cpu_percent}}
    scan_interval: 60
    json_attributes:
      - data

like on all other add-ons, but this one remains Unknown. Even after having started the add-on.

btw Ive tried to go ups 1 branch in the resource too and use

 http://supervisor/addons/a0d7b954_portainer

and adjusted the value_template too, but still nothing happening.

it can be if