If a restful json source is offline, set value to '0', if clause

Hi, 1st post here.

sorry ahead, but after some days searching and trying to solve this i try to ask here

my usecase is to show sensor datas from remote servers, in this sample hwinfo data, now, the mashine (is a gaming VM) is not always on, so when its off (and the json source offline) i would like to replace the datas with 0 so the dashboard gauges etc. are all set to 0 (instead unavailable, specially as gauges are then messed up in the display)

here a sample from my sensor.yaml, Restful 3 which is without the if clause, Restful 4 with my last approach now …

- platform: rest
  scan_interval: 2
  resource: http://192.168.1.200:60000/json.json
  name: "RESTful 3"
  unit_of_measurement: "MB"
  value_template: "{{ value_json.afterburner.entries | selectattr('localName', 'eq', 'Speicher Auslastung') | map(attribute='data') | first | replace(',', '.') | round(0) }}"
- platform: rest
  scan_interval: 2
  resource: http://192.168.1.200:60000/json.json
  name: "RESTful 4"
  unit_of_measurement: "%"
  value_template: >
    {% set state = states('sensor.restful_4') %}
    {% if is_number(state) and state | float > 0 %}
      {{ value_json.hwinfo.readings | selectattr('labelOriginal', 'eq', 'Total CPU Usage') | map(attribute='value') | first | replace(',', '.') | round(2) }}
    {% else %}
      {{ "0" }}
    {% endif %}

the if clause i would like to achieve, if offline, unknown, whatever … set to 0 and keep sensor alive

so when the remote server is offline i get the following error in the logs

2022-06-27 20:26:05 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.restful_4 fails

so i meanwhile guess, no if clause is working so simple to achieve what i would like

for any tipps, thanks ahead

The REST integration throws an error when it encounters an error and the sensor becomes unavailable. The evaluation of the return never happens.

Two choices.

A) create an template sensors whose value is the value of the REST sensor except when it is unavailable it becomes a zero.
B) create command line shell script that does the REST call via curl and has error has error handling to return zero when it fails.

ok, then my understanding of this error was correct, no chance to use a if clause.

about Version A, so in this sample i would add each sensor “double” ?, 1 template which i use then in my dashboard as source (template), in this template i use the 2nd (rest sensor) and/or the “0” value by if clause.

so this would be my starting point then ?

Yep. there is a tab in developer tools that allows you to test the template expression. Get it working in there then paste it into that sensor template syntax.

sorry, but may a sample howto … different tests ending up with no existing extra template sensor and also no errors in the logs

sample from my sensor.yaml

- platform: rest
  scan_interval: 2
  resource: http://192.168.1.200:60000/json.json
  name: "RESTful 3"
  value_template: "{{ value_json.afterburner.entries | selectattr('localName', 'eq', 'Speicher Auslastung') | map(attribute='data') | first | replace(',', '.') | round(0) }}"
- platform: template
  sensors:
    sensor_restful_3_test:
      unit_of_measurement: "MB"
      value_template: "{{ state_attr('sensor.restful_3') }}"

as starter, how would i just simple “copy” the value from the rest sensor in a template sensor which would be useable ? thanks ahead :wink:

sorry, this part is solved, template need a restart and no REST refresh … my fault :wink:

- platform: template
  sensors:
    sensor_restful_3_test:
      unit_of_measurement: "MB"
      value_template: "{{ states('sensor.restful_3') }}"

this is my template sensor now, just in case im wrong … i ll try to build now my IF clause around it, while i still dont get it cause i also will have to check for sensor.restful_3 … i ll see it, thanks anyway for pointing.

ok, i think i figured it now

may as check if i thought on everything, i added also some “or” rule so if HA is restarted while the source is offline it will also return 0 as the state is then unknown instead unavailable.

here the sensor.yaml and configuration.yaml entries

- platform: rest
  scan_interval: 5
  resource: http://192.168.1.200:60000/json.json
  name: "RESTful 3"
  value_template: "{{ value_json.afterburner.entries | selectattr('localName', 'eq', 'Speicher Auslastung') | map(attribute='data') | first | replace(',', '.') | round(0)  | float }}"
- platform: rest
  scan_interval: 5
  resource: http://192.168.1.200:60000/json.json
  name: "RESTful 4"
  value_template: "{{ value_json.hwinfo.readings | selectattr('labelOriginal', 'eq', 'Total CPU Usage') | map(attribute='value') | first | replace(',', '.') | round(2) | float }}"
template:
  - sensor:
    - name: "sensor_restful_3_test"
      unit_of_measurement: "MB"
      state: "{{ 0 if states('sensor.restful_3') == 'unavailable' or states('sensor.restful_3') == 'unknown' or states('sensor.restful_3') == 'none' else states('sensor.restful_3') }}"
  - sensor:
    - name: sensor_restful_4_test
      unit_of_measurement: "%"
      state: "{{ 0 if states('sensor.restful_4') == 'unavailable' or states('sensor.restful_4') == 'unknown' or states('sensor.restful_4') == 'none' else states('sensor.restful_4') }}"

thanks ahead for taking a look

If you wish, you can simplify the template like this:

      state: >
        {% set x = states('sensor.restful_3') %}
        {{ 0 if x in  ['unavailable', 'unknown', 'none'] else x }}

thank you, i prefer your simplifyed way :wink: