From d34590e4e9fa3aa0567ad3efcc1ac958a6ba1d88 Mon Sep 17 00:00:00 2001 From: Georgi Gardev Date: Wed, 14 Dec 2022 21:58:32 +0200 Subject: [PATCH] Be safer with entities in templates --- lovelace/button_card_templates/v3/v3_badge_humidity.yaml | 2 +- lovelace/button_card_templates/v3/v3_badge_temp.yaml | 2 +- lovelace/button_card_templates/v3/v3_button_ac.yaml | 6 +++--- lovelace/button_card_templates/v3/v3_button_cover.yaml | 4 ++-- lovelace/button_card_templates/v3/v3_button_light.yaml | 4 ++-- lovelace/button_card_templates/v3/v3_button_power.yaml | 2 +- lovelace/button_card_templates/v3/v3_button_purifier.yaml | 6 +++--- lovelace/button_card_templates/v3/v3_button_radiator.yaml | 8 ++++---- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lovelace/button_card_templates/v3/v3_badge_humidity.yaml b/lovelace/button_card_templates/v3/v3_badge_humidity.yaml index fc7cec9..4f2bc58 100644 --- a/lovelace/button_card_templates/v3/v3_badge_humidity.yaml +++ b/lovelace/button_card_templates/v3/v3_badge_humidity.yaml @@ -1,3 +1,3 @@ v3_badge_humidity: template: v3_badge - state_display: "[[[ return Number.parseFloat(entity.state.split(' ')[0]).toFixed(1) + '%' ]]]" + state_display: "[[[ return Number.parseFloat(entity?.state.split(' ')[0]).toFixed(1) + '%' ]]]" diff --git a/lovelace/button_card_templates/v3/v3_badge_temp.yaml b/lovelace/button_card_templates/v3/v3_badge_temp.yaml index 4a465b3..b3fc0f1 100644 --- a/lovelace/button_card_templates/v3/v3_badge_temp.yaml +++ b/lovelace/button_card_templates/v3/v3_badge_temp.yaml @@ -1,3 +1,3 @@ v3_badge_temp: template: v3_badge - state_display: "[[[ return Number.parseFloat(entity.state.split(' ')[0]).toFixed(1) + '°' ]]]" + state_display: "[[[ return Number.parseFloat(entity?.state.split(' ')[0]).toFixed(1) + '°' ]]]" diff --git a/lovelace/button_card_templates/v3/v3_button_ac.yaml b/lovelace/button_card_templates/v3/v3_button_ac.yaml index b3309a7..0e198c9 100644 --- a/lovelace/button_card_templates/v3/v3_button_ac.yaml +++ b/lovelace/button_card_templates/v3/v3_button_ac.yaml @@ -10,9 +10,9 @@ v3_button_ac: if (entity.state === "off") { return "Off"; } - return entity.attributes.hvac_action.charAt(0).toUpperCase() + - entity.attributes.hvac_action.slice(1) + ' to ' + - entity.attributes.temperature + '°'; + return entity.attributes?.hvac_action?.charAt(0).toUpperCase() + + entity.attributes?.hvac_action?.slice(1) + ' to ' + + entity.attributes?.temperature + '°'; ]]] state: - value: 'off' diff --git a/lovelace/button_card_templates/v3/v3_button_cover.yaml b/lovelace/button_card_templates/v3/v3_button_cover.yaml index 90ddc89..8c844ab 100644 --- a/lovelace/button_card_templates/v3/v3_button_cover.yaml +++ b/lovelace/button_card_templates/v3/v3_button_cover.yaml @@ -2,6 +2,6 @@ v3_button_cover: template: [v3_button, cover] state_display: | [[[ - if (entity.state === 'closed') { return 'Closed' } - return entity.attributes.current_position + '% Open'; + if (entity?.state === 'closed') { return 'Closed' } + return entity?.attributes?.current_position + '% Open'; ]]] diff --git a/lovelace/button_card_templates/v3/v3_button_light.yaml b/lovelace/button_card_templates/v3/v3_button_light.yaml index ab2b0a4..1dc18ad 100644 --- a/lovelace/button_card_templates/v3/v3_button_light.yaml +++ b/lovelace/button_card_templates/v3/v3_button_light.yaml @@ -2,6 +2,6 @@ v3_button_light: template: v3_button state_display: | [[[ - if (entity.state === 'off') { return 'Off'; } - return Math.round(entity.attributes.brightness * 100 / 255) + "%"; + if (entity?.state === 'off') { return 'Off'; } + return Math.round(entity?.attributes?.brightness * 100 / 255) + "%"; ]]] diff --git a/lovelace/button_card_templates/v3/v3_button_power.yaml b/lovelace/button_card_templates/v3/v3_button_power.yaml index c1669be..b24e440 100644 --- a/lovelace/button_card_templates/v3/v3_button_power.yaml +++ b/lovelace/button_card_templates/v3/v3_button_power.yaml @@ -4,7 +4,7 @@ v3_button_power: power_sensor: state_display: | [[[ - if (entity.state === 'off') return 'Off'; + if (entity?.state === 'off') return 'Off'; return states[variables.power_sensor].state + " W"; ]]] icon: mdi:power-socket diff --git a/lovelace/button_card_templates/v3/v3_button_purifier.yaml b/lovelace/button_card_templates/v3/v3_button_purifier.yaml index caf11e9..87b046f 100644 --- a/lovelace/button_card_templates/v3/v3_button_purifier.yaml +++ b/lovelace/button_card_templates/v3/v3_button_purifier.yaml @@ -2,9 +2,9 @@ v3_button_purifier: template: v3_button state_display: | [[[ - if (entity.state === 'off') { return 'Off'; } - if (!entity.attributes.preset_mode) { return 'Unknown preset'; } - return entity.attributes.preset_mode.charAt(0).toUpperCase() + entity.attributes.preset_mode.slice(1); + if (entity?.state === 'off') { return 'Off'; } + if (!entity?.attributes.preset_mode) { return 'Unknown preset'; } + return entity?.attributes?.preset_mode.charAt(0).toUpperCase() + entity?.attributes?.preset_mode.slice(1); ]]] variables: value_has_error: | diff --git a/lovelace/button_card_templates/v3/v3_button_radiator.yaml b/lovelace/button_card_templates/v3/v3_button_radiator.yaml index d8747d8..3663031 100644 --- a/lovelace/button_card_templates/v3/v3_button_radiator.yaml +++ b/lovelace/button_card_templates/v3/v3_button_radiator.yaml @@ -3,10 +3,10 @@ v3_button_radiator: icon: mdi:radiator state_display: | [[[ - if (entity.state === 'off') { return 'Off'}; - return entity.state.charAt(0).toUpperCase() + - entity.state.slice(1) + " to " + - entity.attributes.temperature + '°'; + if (entity?.state === 'off') { return 'Off'}; + return entity?.state.charAt(0).toUpperCase() + + entity?.state.slice(1) + " to " + + entity?.attributes?.temperature + '°'; ]]] tap_action: action: call-service