Be safer with entities in templates

This commit is contained in:
Georgi Gardev
2022-12-14 21:58:32 +02:00
parent b34f12e2c3
commit d34590e4e9
8 changed files with 17 additions and 17 deletions
@@ -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) + '%' ]]]"
@@ -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) + '°' ]]]"
@@ -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'
@@ -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';
]]]
@@ -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) + "%";
]]]
@@ -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
@@ -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: |
@@ -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