I currently have working code for multiple REST calls against a single API endpoint (see below). In order to reduce load on the server, I would like to combine them into a single REST call. I have tried and tried… but, just can’t get the formatting right. I haven’t made much progress with trial and error.
Original “working” configuration with multiple REST calls + respective templates: (working)
sensor:
- platform: systemmonitor
resources:
- type: disk_use_percent
arg: /home
- type: memory_use
- type: memory_use_percent
- type: processor_use
- platform: rest
resource: http://192.168.1.2:61208/api/3/uptime
value_template: '{{ value_json | regex_replace(find="[\"]", replace="") | regex_replace(find="(?<=( (\d{1})|(\d{2})):\d{2}):\d{2}", replace=" hours") }}'
name: Horizon Uptime
verify_ssl: false
timeout: 30
scan_interval: 16
- platform: rest
resource: http://192.168.1.2:61208/api/3/cpu
name: Horizon CPU
value_template: '{{ value_json.total | round }}'
unit_of_measurement: "%"
verify_ssl: false
timeout: 30
scan_interval: 16
- platform: rest
resource: http://192.168.1.2:61208/api/3/mem
name: Horizon RAM
value_template: '{{ value_json.percent | round }}'
unit_of_measurement: "%"
verify_ssl: false
timeout: 30
scan_interval: 16
- platform: rest
resource: http://192.168.1.2:61208/api/3/memswap
name: Horizon Pagefile
value_template: '{{ value_json.percent | round }}'
unit_of_measurement: "%"
verify_ssl: false
timeout: 30
scan_interval: 16
- platform: rest
name: gpu
resource: http://192.168.1.2:61208/api/3/gpu
value_template: '{{ ( value_json ) }}'
json_attributes:
- name
- mem
- proc
- temperature
verify_ssl: false
timeout: 30
scan_interval: 16
- platform: rest
name: network
resource: http://192.168.1.2:61208/api/3/network
value_template: N/A
json_attributes:
- interface_name
- time_since_update
# - cumulative_rx
- rx
# - cumulative_tx
- tx
# - cumulative_cx
- cx
# - is_up
# - speed
verify_ssl: false
timeout: 30
scan_interval: 16
- platform: rest
resource: http://192.168.1.2:61208/api/3/fs
name: Horizon C Drive Used
value_template: '{{ value_json[0].percent | round }}'
unit_of_measurement: "%"
verify_ssl: false
timeout: 30
scan_interval: 16
- platform: rest
resource: http://192.168.1.2:61208/api/3/fs
name: Horizon E Drive Used
value_template: '{{ value_json[1].percent | round }}'
unit_of_measurement: "%"
verify_ssl: false
timeout: 30
scan_interval: 16
- platform: rest
name: diskio_0
resource: http://192.168.1.2:61208/api/3/diskio
value_template: '{{ ( value_json[0] ) }}'
json_attributes_path: "$.0"
json_attributes:
- time_since_update
- disk_name
# - read_count
# - write_count
- read_bytes
- write_bytes
verify_ssl: false
timeout: 30
scan_interval: 16
- platform: rest
name: diskio_1
resource: http://192.168.1.2:61208/api/3/diskio
value_template: '{{ ( value_json[1] ) }}'
json_attributes_path: "$.1"
json_attributes:
- time_since_update
- disk_name
# - read_count
# - write_count
- read_bytes
- write_bytes
verify_ssl: false
timeout: 30
scan_interval: 16
#Respective Templates for REST sensors:
- platform: template
sensors:
horizon_c_drive_read_bytes:
value_template: >-
{% set read_byte = state_attr('sensor.diskio_0', 'read_bytes') %}
{% set time_since = state_attr('sensor.diskio_0', 'time_since_update') %}
{% if (read_byte != None ) and (time_since != None ) %}
{{ ((read_byte | int) / (time_since | int) / 1000000) | round (2) }}
{% endif %}
availability_template: >-
{{ states("sensor.diskio_0") not in ["unknown", "unavailable"] }}
unit_of_measurement: 'MB/s'
- platform: template
sensors:
horizon_c_drive_write_bytes:
value_template: >-
{% set write_byte = state_attr('sensor.diskio_0', 'write_bytes') %}
{% set time_since = state_attr('sensor.diskio_0', 'time_since_update') %}
{% if (write_byte != None ) and (time_since != None ) %}
{{ ((write_byte | int) / (time_since | int) / 1000000) | round (2) }}
{% endif %}
availability_template: >-
{{ states("sensor.diskio_0") not in ["unknown", "unavailable"] }}
unit_of_measurement: 'MB/s'
- platform: template
sensors:
horizon_e_drive_read_bytes:
value_template: >-
{% set read_byte = state_attr('sensor.diskio_1', 'read_bytes') %}
{% set time_since = state_attr('sensor.diskio_1', 'time_since_update') %}
{% if (read_byte != None ) and (time_since != None ) %}
{{ ((read_byte | int) / (time_since | int) / 1000000) | round (2) }}
{% endif %}
availability_template: >-
{{ states("sensor.diskio_1") not in ["unknown", "unavailable"] }}
unit_of_measurement: 'MB/s'
- platform: template
sensors:
horizon_e_drive_write_bytes:
value_template: >-
{% set write_byte = state_attr('sensor.diskio_1', 'write_bytes') %}
{% set time_since = state_attr('sensor.diskio_1', 'time_since_update') %}
{% if (write_byte != None ) and (time_since != None ) %}
{{ ((write_byte | int) / (time_since | int) / 1000000) | round (2) }}
{% endif %}
availability_template: >-
{{ states("sensor.diskio_1") not in ["unknown", "unavailable"] }}
unit_of_measurement: 'MB/s'
- platform: template
sensors:
horizon_gpu_name:
value_template: '{{ state_attr("sensor.gpu", "name") }}'
# value_template: '{{ states.sensor.gpu.attributes["name"] }}'
- platform: template
sensors:
horizon_gpu_memory:
value_template: '{{ state_attr("sensor.gpu", "mem") | round }}'
# value_template: '{{ states.sensor.gpu.attributes["mem"] | round }}'
unit_of_measurement: "%"
- platform: template
sensors:
horizon_gpu_processor:
value_template: '{{ state_attr("sensor.gpu", "proc") }}'
# value_template: '{{ states.sensor.gpu.attributes["proc"] }}'
unit_of_measurement: "%"
- platform: template
sensors:
horizon_gpu_temperature:
value_template: '{{ state_attr("sensor.gpu", "temperature") }}'
# value_template: '{{ states.sensor.gpu.attributes["temperature"] }}'
unit_of_measurement: "°C"
- platform: template
sensors:
horizon_download_speed:
value_template: >-
{% set receive = state_attr('sensor.network', 'rx') %}
{% set time_since = state_attr('sensor.network', 'time_since_update') %}
{% if (receive != None ) and (time_since != None ) %}
{{ ((receive | int) / (time_since | int) * 8 / 1000000) | round (2) }}
{% endif %}
availability_template: >-
{{ states("sensor.network") not in ["unknown", "unavailable"] }}
unit_of_measurement: 'MB/s'
- platform: template
sensors:
horizon_upload_speed:
value_template: >-
{% set transmit = state_attr('sensor.network', 'tx') %}
{% set time_since = state_attr('sensor.network', 'time_since_update') %}
{% if (transmit != None ) and (time_since != None ) %}
{{ ((transmit | int) / (time_since | int) * 8 / 1000000) | round (2) }}
{% endif %}
availability_template: >-
{{ states("sensor.network") not in ["unknown", "unavailable"] }}
unit_of_measurement: 'MB/s'
- platform: template
sensors:
horizon_total_speed:
value_template: >-
{% set total = state_attr('sensor.network', 'cx') %}
{% set time_since = state_attr('sensor.network', 'time_since_update') %}
{% if (total != None ) and (time_since != None ) %}
{{ ((total | int) / (time_since | int) * 8 / 1000000) | round (2) }}
{% endif %}
availability_template: >-
{{ states("sensor.network") not in ["unknown", "unavailable"] }}
unit_of_measurement: 'MB/s'
Consolidated REST + respective templates: (not correct!)
The following integrations and platforms could not be set up:
- rest
2021-02-08 17:58:10 ERROR (SyncWorker_6) [homeassistant.util.yaml.loader] mapping values are not allowed here
in “/config/configuration.yaml”, line 70, column 14
sensor:
- platform: systemmonitor
resources:
- type: disk_use_percent
arg: /home
- type: memory_use
- type: memory_use_percent
- type: processor_use
rest:
- resource: http://192.168.1.2:61208/api/3/all
sensors:
- name: Horizon C Drive Used
value_template: '{{ value_json[0].fs.percent | round }}'
unit_of_measurement: "%"
- name: Horizon E Drive Used
value_template: '{{ value_json[1].fs.percent | round }}'
unit_of_measurement: "%"
- name: diskio_0
value_template: '{{ ( value_json[0].diskio ) }}'
json_attributes_path: "$.0"
json_attributes:
- time_since_update
- disk_name
# - read_count
# - write_count
- read_bytes
- write_bytes
- name: diskio_1
value_template: '{{ ( value_json[1].diskio ) }}'
json_attributes_path: "$.1"
json_attributes:
- time_since_update
- disk_name
# - read_count
# - write_count
- read_bytes
- write_bytes
- name: Horizon Uptime
value_template: '{{ value_json.uptime | regex_replace(find="[\"]", replace="") | regex_replace(find="(?<=( (\d{1})|(\d{2})):\d{2}):\d{2}", replace=" hours") }}'
- name: Horizon CPU
value_template: '{{ value_json.cpu.total | round }}'
unit_of_measurement: "%"
- name: Horizon RAM
value_template: '{{ value_json.mem.percent | round }}'
unit_of_measurement: "%"
- name: Horizon Pagefile
value_template: '{{ value_json.memswap.percent | round }}'
unit_of_measurement: "%"
- name: gpu
value_template: '{{ ( value_json.gpu ) }}'
json_attributes:
- name
- mem
- proc
- temperature
- name: network
value_template: '{{ ( value_json.network ) }}'
json_attributes:
- interface_name
- time_since_update
# - cumulative_rx
- rx
# - cumulative_tx
- tx
# - cumulative_cx
- cx
# - is_up
# - speed
verify_ssl: false
timeout: 30
scan_interval: 16
- platform: template
sensors:
horizon_c_drive_read_bytes:
value_template: >-
{% set read_byte = state_attr('sensor.diskio_0', 'read_bytes') %}
{% set time_since = state_attr('sensor.diskio_0', 'time_since_update') %}
{% if (read_byte != None ) and (time_since != None ) %}
{{ ((read_byte | int) / (time_since | int) / 1000000) | round (2) }}
{% endif %}
availability_template: >-
{{ states("sensor.diskio_0") not in ["unknown", "unavailable"] }}
unit_of_measurement: 'MB/s'
- platform: template
sensors:
horizon_c_drive_write_bytes:
value_template: >-
{% set write_byte = state_attr('sensor.diskio_0', 'write_bytes') %}
{% set time_since = state_attr('sensor.diskio_0', 'time_since_update') %}
{% if (write_byte != None ) and (time_since != None ) %}
{{ ((write_byte | int) / (time_since | int) / 1000000) | round (2) }}
{% endif %}
availability_template: >-
{{ states("sensor.diskio_0") not in ["unknown", "unavailable"] }}
unit_of_measurement: 'MB/s'
Here’s another attempt: (not working either)
Invalid config for [sensor.rest]: [sensors] is an invalid option for [sensor.rest]. Check: sensor.rest->sensors. (See ?, line ?).
sensor:
- platform: systemmonitor
resources:
- type: disk_use_percent
arg: /home
- type: memory_use
- type: memory_use_percent
- type: processor_use
- platform: rest
resource: http://192.168.1.2:61208/api/3/all
sensors:
- name: Horizon C Drive Used
value_template: '{{ value_json[0].fs.percent | round }}'
unit_of_measurement: "%"
- name: Horizon E Drive Used
value_template: '{{ value_json[1].fs.percent | round }}'
unit_of_measurement: "%"
- name: diskio_0
value_template: '{{ ( value_json[0].diskio ) }}'
json_attributes_path: "$.0"
json_attributes:
- time_since_update
- disk_name
# - read_count
# - write_count
- read_bytes
- write_bytes
- name: diskio_1
value_template: '{{ ( value_json[1].diskio ) }}'
json_attributes_path: "$.1"
json_attributes:
- time_since_update
- disk_name
# - read_count
# - write_count
- read_bytes
- write_bytes
- name: Horizon Uptime
value_template: '{{ value_json.uptime | regex_replace(find="[\"]", replace="") | regex_replace(find="(?<=( (\d{1})|(\d{2})):\d{2}):\d{2}", replace=" hours") }}'
- name: Horizon CPU
value_template: '{{ value_json.cpu.total | round }}'
unit_of_measurement: "%"
- name: Horizon RAM
value_template: '{{ value_json.mem.percent | round }}'
unit_of_measurement: "%"
- name: Horizon Pagefile
value_template: '{{ value_json.memswap.percent | round }}'
unit_of_measurement: "%"
- name: gpu
value_template: '{{ ( value_json.gpu ) }}'
json_attributes:
- name
- mem
- proc
- temperature
- name: network
value_template: '{{ ( value_json.network ) }}'
json_attributes:
- interface_name
- time_since_update
# - cumulative_rx
- rx
# - cumulative_tx
- tx
# - cumulative_cx
- cx
# - is_up
# - speed
verify_ssl: false
timeout: 30
scan_interval: 16
- platform: template
sensors:
horizon_c_drive_read_bytes:
value_template: >-
{% set read_byte = state_attr('sensor.diskio_0', 'read_bytes') %}
{% set time_since = state_attr('sensor.diskio_0', 'time_since_update') %}
{% if (read_byte != None ) and (time_since != None ) %}
{{ ((read_byte | int) / (time_since | int) / 1000000) | round (2) }}
{% endif %}
availability_template: >-
{{ states("sensor.diskio_0") not in ["unknown", "unavailable"] }}
unit_of_measurement: 'MB/s'
- platform: template
sensors:
horizon_c_drive_write_bytes:
value_template: >-
{% set write_byte = state_attr('sensor.diskio_0', 'write_bytes') %}
{% set time_since = state_attr('sensor.diskio_0', 'time_since_update') %}
{% if (write_byte != None ) and (time_since != None ) %}
{{ ((write_byte | int) / (time_since | int) / 1000000) | round (2) }}
{% endif %}
availability_template: >-
{{ states("sensor.diskio_0") not in ["unknown", "unavailable"] }}
unit_of_measurement: 'MB/s'
Here’s one more variation: (doesn’t work either)
sensor:
- platform: systemmonitor
resources:
- type: disk_use_percent
arg: /home
- type: memory_use
- type: memory_use_percent
- type: processor_use
- platform: rest
resource: http://192.168.1.2:61208/api/3/all
sensors:
Horizon C Drive Used
value_template: '{{ value_json[0].fs.percent | round }}'
unit_of_measurement: "%"
Horizon E Drive Used
value_template: '{{ value_json[1].fs.percent | round }}'
unit_of_measurement: "%"
diskio_0
value_template: '{{ ( value_json[0].diskio ) }}'
json_attributes_path: "$.0"
json_attributes:
- time_since_update
- disk_name
# - read_count
# - write_count
- read_bytes
- write_bytes
diskio_1
value_template: '{{ ( value_json[1].diskio ) }}'
json_attributes_path: "$.1"
json_attributes:
- time_since_update
- disk_name
# - read_count
# - write_count
- read_bytes
- write_bytes
Horizon Uptime
value_template: '{{ value_json.uptime | regex_replace(find="[\"]", replace="") | regex_replace(find="(?<=( (\d{1})|(\d{2})):\d{2}):\d{2}", replace=" hours") }}'
Horizon CPU
value_template: '{{ value_json.cpu.total | round }}'
unit_of_measurement: "%"
Horizon RAM
value_template: '{{ value_json.mem.percent | round }}'
unit_of_measurement: "%"
Horizon Pagefile
value_template: '{{ value_json.memswap.percent | round }}'
unit_of_measurement: "%"
gpu
value_template: '{{ ( value_json.gpu ) }}'
json_attributes:
- name
- mem
- proc
- temperature
network
value_template: '{{ ( value_json.network ) }}'
json_attributes:
- interface_name
- time_since_update
# - cumulative_rx
- rx
# - cumulative_tx
- tx
# - cumulative_cx
- cx
# - is_up
# - speed
verify_ssl: false
timeout: 30
scan_interval: 16
- platform: template
sensors:
horizon_c_drive_read_bytes:
value_template: >-
{% set read_byte = state_attr('sensor.diskio_0', 'read_bytes') %}
{% set time_since = state_attr('sensor.diskio_0', 'time_since_update') %}
{% if (read_byte != None ) and (time_since != None ) %}
{{ ((read_byte | int) / (time_since | int) / 1000000) | round (2) }}
{% endif %}
availability_template: >-
{{ states("sensor.diskio_0") not in ["unknown", "unavailable"] }}
unit_of_measurement: 'MB/s'
- platform: template
sensors:
horizon_c_drive_write_bytes:
value_template: >-
{% set write_byte = state_attr('sensor.diskio_0', 'write_bytes') %}
{% set time_since = state_attr('sensor.diskio_0', 'time_since_update') %}
{% if (write_byte != None ) and (time_since != None ) %}
{{ ((write_byte | int) / (time_since | int) / 1000000) | round (2) }}
{% endif %}
availability_template: >-
{{ states("sensor.diskio_0") not in ["unknown", "unavailable"] }}
unit_of_measurement: 'MB/s'