Hi All,
I’m playing with sensors now and stumbled upon the following:
if I declare a sensor as
sensor: - platform: template
I can change its displayed value via defining value_template
(like ‘open/closed’ instead of on/off), but cannot change its icon via icon_template
, it isn’t changing.
if I declare a sensor as
binary_sensor: - platform: template
I can change its icon via icon_template
, but cannot change its displayed value (it remains predefined).
And in both cases my sensor remains binary, i.e no matter how many if/elif
I have in value_template
, I can never get more than 2 values displayed.
For example,
sensor: - platform: template
sensors:
1st_floor_window_sensor:
friendly_name: “window is”
value_template: >-
{% if is_state(‘input_boolean.1st_floor_window_detector’, ‘on’) %}
open
{% elif is_state(‘switch.1st_floor_window_tampered_detector’, ‘on’) %}
tampered
{% else %}
closed
{% endif %}
shows only open
/closed
, but never tampered
.
Am I missing something crucial, or that’s the way it should be?
Update: answering my own question - the template sensors are not only binary, I swapped if
and elif
clauses, and it did the job.
But I still unable to change icon with the following code:
icon_template: >
{% if is_state(‘switch.1st_floor_window_tampered_detector’, ‘on’) %}
mdi:alert-circle-outline
{% elif is_state(‘input_boolean.1st_floor_window_detector’, ‘on’) %}
mdi:window-open
{% else %}
mdi:window-closed
{% endif %}
Speaking about binary_sensor, I wanted to change its icon only when it’s on
, but I don’t know where to get the default icon from for the off
state… the following code brings not what I want:
- platform: template
sensors:
1st_floor_window_sensor_tampered:
friendly_name: “the sensor is”
device_class: safety
value_template: “{{ is_state(‘switch.1st_floor_window_tampered_detector’, ‘on’) }}”
icon_template: >
{% if is_state(‘switch.1st_floor_window_tampered_detector’, ‘on’) %}
mdi:alert-circle-outline
{% else %}
{{ states.binary_sensor[‘1st_floor_window_sensor_tampered’].attributes.icon }}
{% endif %}