I would like to gather data from an API that returns JSON data using the most efficient means. Currently I am using several RESTful Sensors. It is a small IoT device which cannot handle a lot of load so many simultaneous calls will cause requests to timeout. I would like to figure out how to stagger the time requests are made to not overwhelm the device (other than incrementing the scan_interval).
There are multiple endpoints, about a dozen or so. Some update every minute, some every 5 minutes and others once a day. Here is an example of how I am pulling some data. Overall I have created separate yaml files to organize by the url path.
Would a RESTful Command be a better option? what I do not see is something similar to scan_interval
in the documentation.
Command Line seems to support scan_interval
but I do not see a way to ensure multiple calls are not made simultaneously.
I have no intention of sending POST calls to endpoints so maybe it is pointless to consider RESTful Command or Command Line?
Here is an example of one yaml file
foo1_2.yaml
rest:
- resource: https://10.0.11.1/foo/buzz/foo1_status.json
verify_ssl: false
headers:
Authorization: !secret secret_api
Content-Type: application/json
scan_interval: 300
timeout: 15
sensor:
- name: "foo_sensor1"
unique_id: myuniqueid
json_attributes_path: "$.[0]"
json_attributes:
- 1
- 2
- 3
- 4
value_template: "{{ value_json[0]['timestamp'] | timestamp_local }}"
device_class: timestamp
- name: "foo_sensor2"
unique_id: myuniqueid
json_attributes_path: "$.[1]"
json_attributes:
- 5
- 6
- 7
- 8
value_template: "{{ value_json[0]['timestamp'] | timestamp_local }}"
device_class: timestamp
# Breaking out json attributes in another yaml file. sensor template {{ state_attr('sensor.foo_sensor2', 'communicating') }}
rest:
- resource: https://10.0.11.1/foo/buzz/foo2_status.json
verify_ssl: false
headers:
Authorization: !secret secret_api
Content-Type: application/json
scan_interval: 60
timeout: 15
sensor:
- name: "foo2_sensor1"
unique_id: myuniqueid
json_attributes_path: "$.[1][0]"
json_attributes:
- A
- B
- C
- D
value_template: "{{ value_json[1][0]['timestamp'] | timestamp_local }}"
device_class: timestamp
- name: "Awesome Foo 1 A"
unique_id: myuniqueid
value_template: "{{ (value_json[1][0][A'][value_x'] | float / 1000) | round(2) }}"