RESTful sensors - How to use old value when API call fails?

I have RESTful sensors relying on an API call which sometimes fails. What I want to do is then have the sensors keep the previous value. I do check the result for unavailable, unknown and none but somehow it doesn’t work. Any hints, anyone?

rest.yaml

- resource: http://opendata.fmi.fi/wfs?service=WFS&version=2.0.0&request=getFeature&storedquery_id=fmi::observations::weather::timevaluepair&place=hattula&timestep=10&starttime=-10
  headers:
    User-Agent: >-
      Home Assistant {{ states('sensor.current_version') }} (+https://www.gofferje.net)
  scan_interval: 900
  sensor:
    - name: "Hattula test"
      value_template: "OK"
      json_attributes_path: "$.wfs:FeatureCollection.wfs:member[0]"
      json_attributes:
        - "omso:PointTimeSeriesObservation"
    - name: "Hattula Temperature"
      value_template: >-
        {% set result = value_json['wfs:FeatureCollection']['wfs:member'][0]['omso:PointTimeSeriesObservation']['om:result']['wml2:MeasurementTimeseries']['wml2:point']['wml2:MeasurementTVP']['wml2:value'] %}
        {% if result not in ['unavailable', 'unknown', 'none'] %}
        {{ result }}
        {% else %}
        {{ states('sensor.hattula_temperature') }}
        {% endif %}
      unit_of_measurement: "°C"
      unique_id: "hattula_temperature"
    - name: "Hattula Humidity"
      value_template: >-
        {% set result = value_json['wfs:FeatureCollection']['wfs:member'][4]['omso:PointTimeSeriesObservation']['om:result']['wml2:MeasurementTimeseries']['wml2:point']['wml2:MeasurementTVP']['wml2:value'] %}
        {% if result not in ['unavailable', 'unknown', 'none'] %}
        {{ result }}
        {% else %}
        {{ states('sensor.hattula_humidity') }}
        {% endif %}
      unit_of_measurement: "%"
      unique_id: "hattula_humidity"
    - name: "Hattula Dewpoint"
      value_template: >-
        {% set result = value_json['wfs:FeatureCollection']['wfs:member'][5]['omso:PointTimeSeriesObservation']['om:result']['wml2:MeasurementTimeseries']['wml2:point']['wml2:MeasurementTVP']['wml2:value'] %}
        {% if result not in ['unavailable', 'unknown', 'none'] %}
        {{ result }}
        {% else %}
        {{ states('sensor.hattula_dewpoint') }}
        {% endif %}
      unit_of_measurement: "°C"
      unique_id: "hattula_dewpoint"

Error when the REST call fails:

Logger: homeassistant.components.rest.util
Source: components/rest/util.py:33
integration: RESTful (documentation, issues)
First occurred: January 22, 2026 at 17:01:53 (93 occurrences)
Last logged: 09:29:45

JSON result was not a dictionary or list with 0th element a dictionary

Example for failed API call return:

<wfs:FeatureCollection
  timeStamp="2026-01-23T07:40:58Z"
  numberMatched="0"
  numberReturned="0"
  xsi:schemaLocation="http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd http://www.opengis.net/gmlcov/1.0 http://schemas.opengis.net/gmlcov/1.0/gmlcovAll.xsd http://www.opengis.net/sampling/2.0 http://schemas.opengis.net/sampling/2.0/samplingFeature.xsd http://www.opengis.net/samplingSpatial/2.0 http://schemas.opengis.net/samplingSpatial/2.0/spatialSamplingFeature.xsd http://www.opengis.net/swe/2.0 http://schemas.opengis.net/sweCommon/2.0/swe.xsd http://inspire.ec.europa.eu/schemas/ompr/3.0 https://inspire.ec.europa.eu/schemas/ompr/3.0/Processes.xsd http://inspire.ec.europa.eu/schemas/omso/3.0 https://inspire.ec.europa.eu/schemas/omso/3.0/SpecialisedObservations.xsd http://www.opengis.net/waterml/2.0 http://schemas.opengis.net/waterml/2.0/waterml2.xsd http://xml.fmi.fi/namespace/om/atmosphericfeatures/1.1 https://xml.fmi.fi/schema/om/atmosphericfeatures/1.1/atmosphericfeatures.xsd">
</wfs:FeatureCollection>

Try

{% if result is defined %}
1 Like

You probably need to do:

{% if value_json is defined %}

otherwise it’ll fail whilst trying to work out result.

OP: you can also use {{ this.state }} as your prior-value fallback.

1 Like

According to the OP, isn’t the failed API call still returning a value_json though? Just with a numberMatched value of 0. Wouldn’t it be better to test:

 {% if value_json['wfs:FeatureCollection']['numberMatched'] is defined %}

and then test if it == 0 (Not sure if that can be done on one line).

1 Like

Agreed. According to OP, value_json will be defined, but the value he is looking for won’t.

@Troon In the Template dev tool, referencing an absent attributes doesn’t produce an error. Mileage may vary in an actual template sensor, ofc.
It doesn’t even return none, strangely. Not sure how different are “none” and “defined” in HA.

EDIT: Ha. Trying to get a sub-attribute of a missing attribute DO produces an error, though, so @reste_narquois 's solution or similar is the best

2 Likes

I guess for belt and braces, should probably test first for value_json, then numberMatched, then whether numberMatched > 0 (assuming it’s returned in every API call).

I never know if this can be done on one line or not: does it need to be in seperate if statements?

1 Like

Thanks everybody! I’ll test all!