This post is part of a group of posts all associated with the acquisition of information from external resources and making it available in Home Assistant.
- Simple Sensor (Background Collection: Retrieve one value with each call)
- Multi-Entity Sensor (Background Collection: Retrieve multiple values with each call)
Simple Sensor
In order to set up Home Assistant to collect data at regular intervals from web sites / devices and make the information available, sensors can be used.
This sensor uses a rest mechanism to extract multiple parameters from a single request / call.
Step 1: Include File for Sensor Settings
Update 'configuration.yaml' to include a separate file which will include all of the sensor configurations:
...
rest: !include rest.yaml
Step 2: Add a Sensor
Create a 'rest.yaml' file alongside the 'configuration.yaml' file
$ vi rest.yaml
- method: GET
scan_interval: 900
resource: "https://api.openweathermap.org/data/2.5/weather?lat=51.1789&lon=-1.8262&appid=MySecret"
sensor:
- name: "Test Weather - Deprecated"
value_template: "{{ value_json['weather'][0]['description'] | default('unknown') }}"
json_attributes_path: "$.['main']"
json_attributes:
- temp
- feels_like
- pressure
- humidity
- name: "Test Weather: Description"
value_template: "{{ value_json['weather'][0]['description'] | default('unknown') }}"
- name: "Test Weather: Feels Like"
value_template: "{{ value_json['main']['feels_like'] - 273.15 }}"
unit_of_measurement: "°C"
- name: "Test Weather: Temperature"
value_template: "{{ value_json['main']['temp'] - 273.15 }}"
unit_of_measurement: "°C"
Hint: The indentation is critical, so in case the indentation above is unclear, see the clarification below:
- method: ...
␠␠scan_interval: ...
␠␠resource: ...
␠␠sensor:
␠␠␠␠- name: ...
␠␠␠␠value_template: ...
␠␠␠␠json_attributes:
␠␠␠␠␠␠- attribute_1
Step 3: Check Things
Visit your Home Assistant / Developer Tools / Yaml page, and select "check configuration", and if everything is OK, select "Restart".
Once restarted, visit Home Assistant / Settings / Developer Tools / States, and search for "Test Weather" in the list and you should see something similar to the following.
What have we Just Done
In the above example, the JSON has come from the openweathermap website, and the path for the description is x.weather[0].description.