Help retrieving multiple values from REST sensor

I’m having trouble getting the right format for my value_template and json_attribute_path.

This will retrieve JSON for a specific USGS sensor.
https://waterservices.usgs.gov/nwis/iv/?format=json&sites=02087183&siteStatus=all

I want to retrieve the 3 values from x.value.timeSeries

There’ll be three nodes within values and I want to grab value[0].value for each of the items as an attribute. I can’t quite work out the correct configuration. This is what I’ve got, but it’s not working. Can someone push me in the right direction?

  - platform: rest
    resource: https://waterservices.usgs.gov/nwis/iv/?format=json&sites=02087183&siteStatus=all
    name: USGS Falls Gage Info
    value_template: "{{ value_json[0]['value']['timeSeries'] }}"
    json_attributes_path: "$.['values']['value'][0]['value']"
    json_attributes:
      - flowrate
      - gageheight
      - gageelevation

https://jsonpathfinder.com/ is handy for visualizing the data.

Testing via the template editor, I can see the three nodes I want and the correct path to them…

{% set value_json = [{"name":"ns1:timeSeriesResponseType","declaredType":"org.cuahsi.waterml.TimeSeriesResponseType","scope":"javax.xml.bind.JAXBElement$GlobalScope","value":{"queryInfo":{"queryURL":"http://waterservices.usgs.gov/nwis/iv/format=json&sites=02087183&siteStatus=all","criteria":{"locationParam":"[ALL:02087183]","variableParam":"ALL","parameter":[]},"note":[{"value":"[ALL:02087183]","title":"filter:sites"},{"value":"[mode=LATEST, modifiedSince=null]","title":"filter:timeRange"},{"value":"methodIds=[ALL]","title":"filter:methodId"},{"value":"2024-10-07T12:47:13.324Z","title":"requestDT"},{"value":"467627b0-84aa-11ef-812f-005056beda50","title":"requestId"},{"value":"Provisional data are subject to revision. Go to http://waterdata.usgs.gov/nwis/help/?provisional for more information.","title":"disclaimer"},{"value":"caas01","title":"server"}]},"timeSeries":[{"sourceInfo":{"siteName":"NEUSE RIVER NEAR FALLS, NC","siteCode":[{"value":"02087183","network":"NWIS","agencyCode":"USGS"}],"timeZoneInfo":{"defaultTimeZone":{"zoneOffset":"-05:00","zoneAbbreviation":"EST"},"daylightSavingsTimeZone":{"zoneOffset":"-04:00","zoneAbbreviation":"EDT"},"siteUsesDaylightSavingsTime":true},"geoLocation":{"geogLocation":{"srs":"EPSG:4326","latitude":35.94,"longitude":-78.5808333},"localSiteXY":[]},"note":[],"siteType":[],"siteProperty":[{"value":"ST","name":"siteTypeCd"},{"value":"03020201","name":"hucCd"},{"value":"37","name":"stateCd"},{"value":"37183","name":"countyCd"}]},"variable":{"variableCode":[{"value":"00060","network":"NWIS","vocabulary":"NWIS:UnitValues","variableID":45807197,"default":true}],"variableName":"Streamflow, ft³/s","variableDescription":"Discharge, cubic feet per second","valueType":"Derived Value","unit":{"unitCode":"ft3/s"},"options":{"option":[{"name":"Statistic","optionCode":"00000"}]},"note":[],"noDataValue":-999999,"variableProperty":[],"oid":"45807197"},"values":[{"value":[{"value":"4960","qualifiers":["P"],"dateTime":"2024-10-07T08:15:00.000-04:00"}],"qualifier":[{"qualifierCode":"P","qualifierDescription":"Provisional data subject to revision.","qualifierID":0,"network":"NWIS","vocabulary":"uv_rmk_cd"}],"qualityControlLevel":[],"method":[{"methodDescription":"","methodID":89128}],"source":[],"offset":[],"sample":[],"censorCode":[]}],"name":"USGS:02087183:00060:00000"},{"sourceInfo":{"siteName":"NEUSE RIVER NEAR FALLS, NC","siteCode":[{"value":"02087183","network":"NWIS","agencyCode":"USGS"}],"timeZoneInfo":{"defaultTimeZone":{"zoneOffset":"-05:00","zoneAbbreviation":"EST"},"daylightSavingsTimeZone":{"zoneOffset":"-04:00","zoneAbbreviation":"EDT"},"siteUsesDaylightSavingsTime":true},"geoLocation":{"geogLocation":{"srs":"EPSG:4326","latitude":35.94,"longitude":-78.5808333},"localSiteXY":[]},"note":[],"siteType":[],"siteProperty":[{"value":"ST","name":"siteTypeCd"},{"value":"03020201","name":"hucCd"},{"value":"37","name":"stateCd"},{"value":"37183","name":"countyCd"}]},"variable":{"variableCode":[{"value":"00065","network":"NWIS","vocabulary":"NWIS:UnitValues","variableID":45807202,"default":true}],"variableName":"Gage height, ft","variableDescription":"Gage height, feet","valueType":"Derived Value","unit":{"unitCode":"ft"},"options":{"option":[{"name":"Statistic","optionCode":"00000"}]},"note":[],"noDataValue":-999999,"variableProperty":[],"oid":"45807202"},"values":[{"value":[{"value":"4.15","qualifiers":["P"],"dateTime":"2024-10-07T08:15:00.000-04:00"}],"qualifier":[{"qualifierCode":"P","qualifierDescription":"Provisional data subject to revision.","qualifierID":0,"network":"NWIS","vocabulary":"uv_rmk_cd"}],"qualityControlLevel":[],"method":[{"methodDescription":"","methodID":89129}],"source":[],"offset":[],"sample":[],"censorCode":[]}],"name":"USGS:02087183:00065:00000"},{"sourceInfo":{"siteName":"NEUSE RIVER NEAR FALLS, NC","siteCode":[{"value":"02087183","network":"NWIS","agencyCode":"USGS"}],"timeZoneInfo":{"defaultTimeZone":{"zoneOffset":"-05:00","zoneAbbreviation":"EST"},"daylightSavingsTimeZone":{"zoneOffset":"-04:00","zoneAbbreviation":"EDT"},"siteUsesDaylightSavingsTime":true},"geoLocation":{"geogLocation":{"srs":"EPSG:4326","latitude":35.94,"longitude":-78.5808333},"localSiteXY":[]},"note":[],"siteType":[],"siteProperty":[{"value":"ST","name":"siteTypeCd"},{"value":"03020201","name":"hucCd"},{"value":"37","name":"stateCd"},{"value":"37183","name":"countyCd"}]},"variable":{"variableCode":[{"value":"63160","network":"NWIS","vocabulary":"NWIS:UnitValues","variableID":51438657,"default":true}],"variableName":"Stream water level elevation above NAVD 1988, in ft","variableDescription":"Stream water level elevation above NAVD 1988, in feet","valueType":"Derived Value","unit":{"unitCode":"ft"},"options":{"option":[{"name":"Statistic","optionCode":"00000"}]},"note":[],"noDataValue":-999999,"variableProperty":[],"oid":"51438657"},"values":[{"value":[{"value":"202.55","qualifiers":["P"],"dateTime":"2024-10-07T08:15:00.000-04:00"}],"qualifier":[{"qualifierCode":"P","qualifierDescription":"Provisional data subject to revision.","qualifierID":0,"network":"NWIS","vocabulary":"uv_rmk_cd"}],"qualityControlLevel":[],"method":[{"methodDescription":"","methodID":314202}],"source":[],"offset":[],"sample":[],"censorCode":[]}],"name":"USGS:02087183:63160:00000"}]},"nil":false,"globalScope":true,"typeSubstituted":false}] %}

{{ value_json[0]['value']['timeSeries'][0]['values'][0]['value'][0]['value'] }}
{{ value_json[0]['value']['timeSeries'][1]['values'][0]['value'][0]['value'] }}
{{ value_json[0]['value']['timeSeries'][2]['values'][0]['value'][0]['value'] }}

rest:
  - resource: https://waterservices.usgs.gov/nwis/iv/?format=json&sites=02087183&siteStatus=all
    sensor:
    - name: USGS Falls Gage Flow Rate
      unit_of_measurement: ft3/s
      value_template: "{{ value_json.value.timeSeries[0]['values'].value[0].value }}"
    - name: USGS Falls Gage Gage Height
      unit_of_measurement: ft
      value_template: "{{ value_json.value.timeSeries[1]['values'].value[0].value }}"
    - name: USGS Falls Gage Elevation
      unit_of_measurement: ft
      value_template: "{{ value_json.value.timeSeries[2]['values'].value[0].value }}"

Thanks!

Had to tweak that slightly, but that showed me the correct format.

    resource: https://waterservices.usgs.gov/nwis/iv/?format=json&sites=02087183&siteStatus=all
    sensor:
    - name: USGS Falls Gage Flow Rate
      unit_of_measurement: ft3/s
      value_template: "{{ value_json.value.timeSeries[0]['values'][0]['value'][0]['value'] }}"
    - name: USGS Falls Gage Gage Height
      unit_of_measurement: ft
      value_template: "{{ value_json.value.timeSeries[1]['values'][0]['value'][0]['value'] }}"
    - name: USGS Falls Gage Elevation
      unit_of_measurement: ft
      value_template: "{{ value_json.value.timeSeries[2]['values'][0]['value'][0]['value'] }}"

I now have multiple sensors from restarting ( sensor.usgs_falls_gage_elevation, sensor.usgs_falls_gage_elevation_2, sensor.usgs_falls_gage_elevation_3 etc)

And it seems like I get a new batch each time I reload YAML or restart HA.

I can’t figure out how to delete the extras nor prevent the duplicates.

Add a unique_id to each sensor and restart, do not reload. After that, you can reload to your hearts content.

1 Like

Thanks. That did the trick.

Since these are created in YAML, where can I delete the extras that were created? I tried recorder.purge_entities.

They will be removed when you restart

1 Like