With the release of v0.116 (Really Supervisor v247) we are provided some usage stats about the core and supervisor. These are useful but I’m guessing some people want to see this information on the frontend or have some historical data available. Some quick use of the developer tools in Chrome and some probing of the rest API and we can pull this info into a rest sensor.
For examples pulling indivdiual stats about an add-ons usage see this post.
First we need to pull the data from the Rest API.
#https://www.home-assistant.io/integrations/rest/
- platform: rest
resource: http://192.168.1.24:8123/api/hassio/core/stats
name: core
unit_of_measurement: '%'
value_template: '{{ value_json.data.cpu_percent }}'
scan_interval: 10
headers:
Authorization: !secret llt
Content-Type: application/json
json_attributes_path: "$.data"
json_attributes:
- memory_percent
- platform: rest
resource: http://192.168.1.24:8123/api/hassio/supervisor/stats
name: supervisor
unit_of_measurement: '%'
value_template: '{{ value_json.data.cpu_percent }}'
scan_interval: 10
headers:
Authorization: !secret llt
Content-Type: application/json
json_attributes_path: "$.data"
json_attributes:
- memory_percent
Information on the Rest API can be found HERE and information on the restful sensor is included in the YAML snippet above.
You can adjust the scan_interval to suit your needs, however, be aware that a very short scan interval will require frequent API calls. Likely not a issue but if your network/hardware is stressed this may cause issues.
!secret llt
is a long lived token token in the format
llt: "Bearer eyJ0eXAiOiJKV1QiLCJhSUPERLONgTOKENlZWQ0NzBhYLe8sxN8nup1Uw"
sensor.core
and sensor.supervisor
will report the CPU percent and have attributes available with the other sensors as an attribute(s). You can make another sensor the primary or remove some of the unused attributes if you want.
You can add other attributes from the API by adding the value as a json_attribute restful sensor. The raw output of the API looks as follows:
{
"result": "ok",
"data":{
"cpu_percent": 0.02,
"memory_usage": 72126464,
"memory_limit": 4028276736,
"memory_percent": 1.79,
"network_rx": 16357595,
"network_tx": 19696684,
"blk_read": 10268672,
"blk_write": 233472
}
}
To get the attributes into a sensor usable in the frontend or in your history, just grab the attribute into a template sensor. Examples below from each of the sensors.
#https://www.home-assistant.io/integrations/template/
- platform: template
sensors:
core_mem:
friendly_name: Core Memory Usage
unit_of_measurement: '%'
value_template: "{{ state_attr('sensor.core', 'memory_percent') }}"
- platform: template
sensors:
supervisor_mem:
friendly_name: Supervisor Memory Usage
unit_of_measurement: '%'
value_template: "{{ state_attr('sensor.supervisor', 'memory_percent') }}"
Example integration into the frontend: