Rest API sensor definaition

Hey folks,

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

Which results in:

I guess what I am asking is, do I need both the rest and template platforms and could I compress it into a single entity?

Cheers

J

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.

You do not need the template sensor.

 - platform: rest
   resource: https://environment.data.gov.uk/flood-monitoring/id/measures/L2309-level-stage-i-15_min-m.json
   name: "Reeth River Level"
   #Check every 30 mins
   scan_interval: 1800
   force_update: true
   headers:
     Content-Type: application/json
   value_template: "{{ value_json.items.['latestReading'] }}"
   unit_of_measurement: m

You will have to add the icon via customize though.

1 Like

I think that should read

value_template: "{{ value_json.items.latestReading.value }}"

Despite the name, “items” in the JSON response is not an array.

If so it must be this:

value_template: "{{ value_json.items.latestReading['value'] }}"

To prevent the template using value which means the whole received value.

Thanks guys,

I have tried this:

 - platform: rest
   resource: https://environment.data.gov.uk/flood-monitoring/id/measures/L2309-level-stage-i-15_min-m.json
   name: "Reeth River Level"
   scan_interval: 1800
   force_update: true
   headers:
     Content-Type: application/json
   value_template: "{{ value_json.items.latestReading['value'] }}"
   unit_of_measurement: m

Which results in this:

So I dont think its working.

The json from the api endpoint is below if it helps

{ 
  "@context" : "http://environment.data.gov.uk/flood-monitoring/meta/context.jsonld" ,
  "meta" : { 
    "publisher" : "Environment Agency" ,
    "licence" : "http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/" ,
    "documentation" : "http://environment.data.gov.uk/flood-monitoring/doc/reference" ,
    "version" : "0.9" ,
    "comment" : "Status: Beta service" ,
    "hasFormat" : [ "http://environment.data.gov.uk/flood-monitoring/id/measures/L2309-level-stage-i-15_min-m.rdf", "http://environment.data.gov.uk/flood-monitoring/id/measures/L2309-level-stage-i-15_min-m.ttl", "http://environment.data.gov.uk/flood-monitoring/id/measures/L2309-level-stage-i-15_min-m.html" ]
  }
   ,
  "items" : { 
    "@id" : "http://environment.data.gov.uk/flood-monitoring/id/measures/L2309-level-stage-i-15_min-m" ,
    "label" : "Grinton Bridge - level-stage-i-15_min-m" ,
    "latestReading" : { 
      "@id" : "http://environment.data.gov.uk/flood-monitoring/data/readings/L2309-level-stage-i-15_min-m/2022-01-10T12-15-00Z" ,
      "date" : "2022-01-10" ,
      "dateTime" : "2022-01-10T12:15:00Z" ,
      "measure" : "http://environment.data.gov.uk/flood-monitoring/id/measures/L2309-level-stage-i-15_min-m" ,
      "value" : 0.277
    }
     ,
    "notation" : "L2309-level-stage-i-15_min-m" ,
    "parameter" : "level" ,
    "parameterName" : "Water Level" ,
    "period" : 900 ,
    "qualifier" : "Stage" ,
    "station" : "http://environment.data.gov.uk/flood-monitoring/id/stations/L2309" ,
    "stationReference" : "L2309" ,
    "type" : [ "http://environment.data.gov.uk/flood-monitoring/def/core/Measure", "http://environment.data.gov.uk/flood-monitoring/def/core/WaterLevel" ] ,
    "unit" : "http://qudt.org/1.1/vocab/unit#Meter" ,
    "unitName" : "m" ,
    "valueType" : "instantaneous"
  }
}

Nope, doesn’t work. And though I do not neet the river’s water level, i figured out how to define a matching rest sensor:

  - platform: rest                                                                                 
    name: "Reeth River Level"
    resource: https://environment.data.gov.uk/flood-monitoring/id/measures/L2309-level-stage-i-15_min-m.json
    force_update: true                           
    headers:                                                                                       
      Content-Type: application/json
    json_attributes:                                                     
      - date                                                             
    json_attributes_path: "$.items.latestReading"                                                  
    scan_interval: 1800                                                  
    unit_of_measurement: 'm'                                             
    value_template: "{{ value_json['items']['latestReading']['value'] }}"

@jondotnet Try this …

Thank you!

This worked:

   value_template: "{{ value_json['items']['latestReading']['value'] }}"

Just need to figure out how to show if its trending up or down now with a nice arrow :slight_smile:

Create a Trend sensor using the REST sensor’s entity_id.

Thanks, will have a play

This

Should be equivalent to this

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.