Handling REST connection error?

Hi,

I have installed a client rest api service on my pc and it returns response shown below.

Then, I have this REST command

- headers:
    x-api-key: !secret sa_auth_key_hp_mini
  scan_interval: 60
  resource: http://192.168.0.195:8080/api/v1/system?memoryUnit=gb
  sensor:
    - name: "HP Mini CPU Load"
      unit_of_measurement: " %"
      json_attributes_path: "$.processor"
      value_template: "{{ value_json.processor.cpuLoad | round(2) }}"
      json_attributes:
        - "name"
        - "model"
        - "family"
        - "vendor"
        - "cpuLoad"
        - "temp"
    - name: "HP Mini Memory"
      unit_of_measurement: " %"
      json_attributes_path: "$.memory"
      value_template: "{{ ( value_json.memory.used / value_json.memory.total ) | multiply(100) | round(2) }}"
      json_attributes:
        - "available"
        - "total"
        - "used"

that receives the following response

{
    "processor": {
        "name": "12th Gen Intel(R) Core(TM) i5-12450H",
        "family": "6",
        "model": "154",
        "vendor": "GenuineIntel",
        "cpuLoad": 2.703287197231834,
        "temp": 0.0
    },
    "memory": {
        "available": 6.3531951904296875,
        "total": 15.700065612792969,
        "used": 9.346870422363281
    }
}

but when the pc is off, I would get a connection error which is expected.

However, on my card I would see something undesirable.

image

Is there any way to default this to zero (0) if the REST api is not accessible?

That is a rest sensor, not a rest command.

I have a similar rest sensor that is unreliable so I deal with it using availability templates and checking if the json value exists:

- platform: rest
  unique_id: 60ec678d-51d9-48e6-82fa-6547402754c4
  name: UV Index
  resource: https://uvdata.arpansa.gov.au/xml/uvvalues.xml
  value_template: >
    {% if value_json is defined %}
      {{ value_json.stations.location | selectattr('name', 'eq', 'kin') | map(attribute='index') | join }}
    {% else %}
      it's broken again but you will never see this text due to the availability template
    {% endif %}
  availability: >
    {{ value_json is defined }}
  unit_of_measurement: " "
  state_class: measurement

If you want to set yours to 0 instead of unavailable then don’t include the availability template and set the value to 0 instead of:

      it's broken again but you will never see this text due to the availability template

This seems to have work. Although I did not specify the availability option in my yaml, it still shows as Unavailable in my card whenever the service is down. Maybe setting 0 in my else statement may have done this but I haven’t actually tested returning other values than 0 so I can’t say for sure. But this is good enough for me. Thank you.