This is a Challenge I have had for as long as I have run Home-Assistant, sensors going offline/unavailable or something like that, and then braking the UI.
I have examples.
My question is, how should I deal with these kind of errors on a general basis whiteout making my code unnecessarily long and complicated? I guess a “if x, else y” for every sensor is one way, but it is making it all more complex.
Example:
{% if is_state("binary_sensor.syncthing_http_response_status", "off") -%}✔️ HTTP Response OK{%- else -%}❌ HTTP Response FAIL{%- endif %}
JOBB-PC
remote State: {{ state_attr("sensor.syncthing_unraid_jobb_pc", "remoteState") }}
Completion: {{ state_attr("sensor.syncthing_unraid_jobb_pc", "completion") }}%
Global items: {{ state_attr("sensor.syncthing_unraid_jobb_pc", "globalItems") }} / {{ ( state_attr("sensor.syncthing_unraid_jobb_pc", "globalBytes") / (1024*1024*1024) ) | round(2) }} GB
KONTOR-PC
remote State: {{ state_attr("sensor.syncthing_unraid_kontor_pc", "remoteState") }}
Completion: {{ state_attr("sensor.syncthing_unraid_kontor_pc", "completion") }}%
Global items: {{ state_attr("sensor.syncthing_unraid_kontor_pc", "globalItems") }} / {{ ( state_attr("sensor.syncthing_unraid_kontor_pc", "globalBytes") / (1024*1024*1024) ) | round(2) }} GB
FLEMMING-LAPTOP
remote State: {{ state_attr("sensor.syncthing_unraid_flemming_laptop", "remoteState") }}
Completion: {{ state_attr("sensor.syncthing_unraid_flemming_laptop", "completion") }}%
Global items: {{ state_attr("sensor.syncthing_unraid_flemming_laptop", "globalItems") }} / {{ ( state_attr("sensor.syncthing_unraid_flemming_laptop", "globalBytes") / (1024*1024*1024) ) | round(2) }} GB
FLEMMING-TLF
remote State: {{ state_attr("sensor.syncthing_unraid_flemming_tlf", "remoteState") }}
Completion: {{ state_attr("sensor.syncthing_unraid_flemming_tlf", "completion") }}%
Global items: {{ state_attr("sensor.syncthing_unraid_flemming_tlf", "globalItems") }} / {{ ( state_attr("sensor.syncthing_unraid_flemming_tlf", "globalBytes") / (1024*1024*1024) ) | round(2) }} GB
One more:
type: custom:vertical-stack-in-card
horizontal: true
cards:
- type: custom:mushroom-template-card
primary: Kaffetrakter
secondary: |+
{% if is_state('switch.zwave_6_switch', 'on') %}På{% else %}Av{% endif %}
{{ states("sensor.zwave_6_electric1_w_meter_66049_value")| round(0) }}W
icon: mdi:coffee-maker
multiline_secondary: true
tap_action:
action: none
hold_action:
action: none
double_tap_action:
action: none
icon_color: >-
{% if is_state('switch.zwave_6_switch', 'on') %} yellow {% else %}{% endif
%}
- type: vertical-stack
cards:
- type: horizontal-stack
cards:
- type: vertical-stack
cards:
- type: custom:mushroom-chips-card
chips:
- type: conditional
conditions:
- entity: switch.zwave_6_switch
state: 'off'
chip:
type: template
double_tap_action:
action: none
content: Slå på
tap_action:
action: call-service
service: switch.turn_on
data: {}
target:
entity_id: switch.zwave_6_switch
hold_action:
action: none
icon_color: green
icon: mdi:power-on
- type: conditional
conditions:
- entity: switch.zwave_6_switch
state: 'on'
chip:
type: template
double_tap_action:
action: none
content: Slå av
tap_action:
action: call-service
service: switch.turn_off
data: {}
target:
entity_id: switch.zwave_6_switch
hold_action:
action: none
icon_color: red
icon: mdi:power-off
- type: horizontal-stack
cards:
- type: custom:mushroom-chips-card
chips:
- type: conditional
conditions:
- entity: switch.zwave_6_switch
state: 'on'
chip:
type: template
double_tap_action:
action: none
icon: mdi:restart
content: Tilbakestill timer
tap_action:
action: call-service
service: timer.start
data: {}
target:
entity_id: timer.caffe_maker_countdown
hold_action:
action: none
icon_color: orange
- type: conditional
conditions:
- entity: switch.zwave_6_switch
state: 'on'
card:
type: custom:timer-bar-card
name: ''
icon: mdi:mdi-blank
bar_width: null
bar_height: null
entity:
- timer.caffe_maker_countdown