As a starting point, one could just grab all the content into an attribute. As a sensor
, I have this:
##
## water.weather.gov
##
- platform: rest
name: Russian River Level
resource: https://water.weather.gov/ahps2/hydrograph_to_xml.php?gage=guec1&output=xml
value_template: "{{ now() }}"
json_attributes:
- site
This gives me this as a sensor
which has all the XML (as JSON) attached to one attribute.
You could do many things with that. If you are familiar with HA, you can just create that sensor
to your URL. I used Guerneville because that is where I live and we are almost at flood stage right now. I would also probably change the scan_interval because the data is certainly not something you need to grab every 30 secs,.
Extending that sample to eliminate some of the data to focus on observed and forecast, you would do this:
##
## water.weather.gov
##
- platform: rest
name: Russian River Level
scan_interval: 3600
resource: https://water.weather.gov/ahps2/hydrograph_to_xml.php?gage=guec1&output=xml
value_template: "{{ now() }}"
json_attributes_path: site
json_attributes:
- observed
- forecast
You then have one sensor with two attributes, one is “observed” and one is “forecast”.
Let’s say you only want to know the maximum forecasted value. You could do this:
{% set levels = namespace(height=[]) %}
{% for level in state_attr('sensor.russian_river_level','forecast').datum %}
{% set levels.height = levels.height + [level.primary['#text']] %}
{% endfor %}
{{levels.height | max}}
Testing this gives:
Which is correct … the current forecast river level here is 33.1 ft. Going on from there you cn grab other values in the JSON (XML) like the monitor and flood values and compare to forecast and do what you like.
For me, I might actually wire this into the APEX Charts card, that would be a nice to have in our home dashboards.