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