Still relatively new to home assistant and I am trying to setup a sensor that retrieves a river level from a rest API. I have it working through more luck than judgement but have ended up with two entities for it.
I wanted to pick peoples brains on if there was a better way to do it?
The yaml config I have is:
- platform: rest
resource: https://environment.data.gov.uk/flood-monitoring/id/measures/L2309-level-stage-i-15_min-m.json
name: river_swale_reeth
#Check every 30 mins
scan_interval: 1800
force_update: true
headers:
Content-Type: application/json
#Filter down to the nested schema object
json_attributes_path: "$.items.['latestReading']"
#Get the value attribute = current river level
json_attributes:
- value
value_template: "{{ state_attr('sensor.river_swale_reeth','value') }}"
- platform: template
sensors:
reeth_river_level:
unique_id: 5514537822219699 #random number to allow formatting in UI
friendly_name: "Reeth River Level"
icon_template: "mdi:Waves"
value_template: "{{ state_attr('sensor.river_swale_reeth','value') }}"
unit_of_measurement: m
The template sensor adds friendly_name, icon_template and unit_of_measurement to the REST sensor. At least the latter can be set at the REST sensor directly. Not sure about the other two atttributes, but I’d guess that they can be set via the UI at least.
Did you try? I did and while the first variant worked, the second didn’t. Honestly, don’t even know why, especially why value_json.items.latestReading.value didn’t either.
Because like the word value_json (everything returned by the resource interpreted as json), the word value has a specific meaning in a template (everything returned by the resource) and must be used in square bracket notation to avoid this.
And no I did not try it as I am on mobile. I believe you I just don’t see why the two templates give different results. They should be equivalent. Not sure what I am missing.