Binary_sensor and template sensor, are they both binary?

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 %}

I have only just started looking into templating & just read that if the entity starts with a number you need to use brackets such as [‘1st_floor…’]

From the templating docs
NOTE: If your template uses an entity_id that begins with a number (example: states.device_tracker.2008_gmc) you must use a bracket syntax to avoid errors caused by rendering the entity_id improperly. In the example given, the correct syntax for the device tracker would be: states.device_tracker[‘2008_gmc’]

Also you have not accounted for a scenario where both the input_boolean & switch are both “on”. Maybe try something more like…

{% if 
is_state("input_boolean.['1st_floor_window_detector']", "on") and 
is_state("switch.['1st_floor_window_tampered_detector']", "on") %} 
tampered
{% elif ("input_boolean.['1st_floor_window_detector']", "on") and 
is_state("switch.['1st_floor_window_tampered_detector']", "off") %} 
open
{% else %}
closed
{% endif %}

I think it’s only applicable to situations like states.switch[‘1234’], but not is_state().
It works for value_template bit anyway.
Checked it as you suggested and there is no difference at all.

The scenario logic was wrong, I’ve just posted an update.