Template binary_sensor & "unknown" state

Got a template sensor, still using an old notation:

      battery_charging_xxx:

        value_template: >-
          {% if .... -%}
            ...
          {%- elif ... -%}
            {{ "unknown" }}
          {%- else -%}
            ...
          {%- endif %}

        icon_template: >-
          {%- if states("binary_sensor.battery_charging_xxx") in ["unavailable","unknown"] -%}
            {{ "mdi:battery-unknown" }}
          {%- elif is_state("binary_sensor.battery_charging_xxx","on") -%}
            {{ "mdi:battery-charging" }}
          {%- else -%}
            {{ "mdi:battery" }}
          {%- endif %}

        availability_template: >-
          ....

The idea is to show the “mdi:battery-unknown” icon for unknown state.
But I keep seeing “mdi:battery” for the icon.

Pasting the code into Dev tools → Templates gives this:

battery_charging_xxx:
        value_template: >-
          unknown
        icon_template: >-
          mdi:battery
        availability_template: >-
          True

“Set state” page gives this:
image

Setting the “unknown” state in the “Set state” gives this output in the Templates:

battery_charging_xxx:
        value_template: >-
          unknown
        icon_template: >-
          mdi:battery-unknown
        availability_template: >-
          True

But UI still showing the “mdi:battery” icon.
And the “icon” internally is still “mdi:battery” too:
template:

{{states("binary_sensor.battery_charging_xxx")}}
{{state_attr("binary_sensor.battery_charging_xxx","icon")}}

output:

unknown
mdi:battery

Have you tried copying the same conditions present at value_template to icon_template ?

It has been a while since I used the legacy format, but I never put the icons in curly brackets and that worked.

No, it does not matter.

This does not help.

Check this simple case:

binary_sensor:
  - platform: template
    sensors:

      test_unknown:
        value_template: >-
          {{ "unknown" }}
        icon_template: >-
          {% set STATE = states("binary_sensor.test_unknown") -%}
          {%- if not STATE in ["unavailable","unknown"] -%}
            {%- if STATE == "on" -%}
              {{ "mdi:battery-charging" }}
            {%- else -%}
              {{ "mdi:battery" }}
            {%- endif -%}
          {%- else -%}
            {{ "mdi:battery-unknown" }}
          {%- endif %}

Here is a small card with the sensor:

type: entities
entities:
  - entity: binary_sensor.test_unknown

After creation the state is shown as “off”:
image

The internal state is “off”, icon is “battery”:

Then set the “unknown” state in the “Set state” panel:


The icon is still “battery”.

What the Templates shows:

What is on the card:
image

And what if put the sensor’s code into Templates:

There is smth strange about “unknown” state for binary sensors.

You’re referencing the state in the state machine, which is the previous state. Don’t do that, replicate your logic from the value template in your icon template for unknown.

1 Like

But as mentioned earlier it does not help anyway…
For that small example I will create some input_select value to manage icon_template value.

There’s nothing strange with it. Look at your original icon template. It’s referring to itself. That’s the state machine and the previous state.

Out of curiosity: I just tried both ( {{ 'unknown' }} and states('binary_sensor.xxxx') ) and both are displaying the wanted icon battery-unknown. But I created the sensors without an availability template.

SO just to clarify and pull from the original post…

The ... in this elif

needs to replace the if here

Because using states("binary_sensor.battery_charging_xxx") inside itself is always referencing the current state machines state. During execution of this icon template, that’s the previous state.

I mean - I tested this case w/o referring to itself as I wrote here, did not help.

One more simple example - w/o referring to itself:

binary_sensor:
  - platform: template
    sensors:
      test_unknown:
        value_template: >-
          {% set STATE = states("input_select.test_unknown_selector") -%}
          {%- if not STATE == "xxxxxx" -%}
            {{ STATE }}
          {%- else -%}
            {{ "unknown" }}
          {%- endif %}
        icon_template: >-
          {% set STATE = states("input_select.test_unknown_selector") -%}
          {%- if not STATE == "xxxxxx" -%}
            {%- if STATE == "on" -%}
              {{ "mdi:battery-charging" }}
            {%- else -%}
              {{ "mdi:battery" }}
            {%- endif -%}
          {%- else -%}
            {{ "mdi:battery-unknown" }}
          {%- endif %}


input_select:
  test_unknown_selector:
    options:
      - "xxxxxx"
      - "on"
      - "off"

The card:

type: entities
entities:
  - entity: input_select.test_unknown_selector
  - entity: binary_sensor.test_unknown

If input_select set to xxxxxx then the state is shown as “off”:
image

Templates - the state is “unknown”:

So, the same result, frontend shows “off” for “unknown”.

The icon is changing properly:
image
image

Are you sure?

This works for me, I just tested it:

template:
  - binary_sensor:
      - name: "Moisture Limit"
        state: >
          {{ states('sensor.lawn_moisture')|float(0) >= states('input_number.moisture_limit')|float(0) }}
        icon: >
          {% if is_state("binary_sensor.moisture_limit", "on") %}
            mdi:water
          {% else %}
            mdi:water-off
          {% endif %}

You’re using not incorrectly.

that shoudl be

{%- if STATE != "xxxxxx" %}

not ‘XXXXXX’ will return false for the == because it’s resolved prior to the ==.

1 Like

Oh! will come back with results after corrections.

I might have this backwards. Either way, you should be using !=. WIthout parenthesis, I’d have to look up the order of operations.

Stop, Petro. The how do you explain this output from the previous post?


The state IS “unknown”. It was defined properly.

Also, I corrected the template:

      test_unknown:
        value_template: >-
          {% set STATE = states("input_select.test_unknown_selector") -%}
          {%- if STATE != "xxxxxx" -%}
            {{ STATE }}
          {%- else -%}
            {{ "unknown" }}
          {%- endif %}
        icon_template: >-
          {% set STATE = states("input_select.test_unknown_selector") -%}
          {%- if STATE != "xxxxxx" -%}
            {%- if STATE == "on" -%}
              {{ "mdi:battery-charging" }}
            {%- else -%}
              {{ "mdi:battery" }}
            {%- endif -%}
          {%- else -%}
            {{ "mdi:battery-unknown" }}
          {%- endif %}

but it produces same “off” state instead of “unknown”.

I have no idea what STATE currently is. If it’s unknown, the logic still works because it’s not “XXX”. not STATE if populated will be FALSE and FALSE == “XXXXXX” will be false, i.e. it will hit unknown in that case.

You keep focusing on unkonwn, but there’s nothing special with it. It’s a string, not an object. There is no special case here. It’s the word ‘unknown’.

1 Like

Can we just focus on your original template, the actual template instead of these tests? They don’t solve anything other than confuse everyone. Post your original issue with the original templates (not these fake templates), I’ll fix it and we can move on.

My original case is rather complex.
But results are same as in the simple example.
In short - when the value_template is unknown, the reported state is “off”.
I wanted to understand the problem, that is why I created a simple example.
Here is the output:

One window, 2 different results.

binary sensors restore states, did you just restart? Did you change the input_select? Again, the states machine holds the last state, not the current state. I feel like a broken record here.

No, should I restart?

Several times for testing, how could I show you different screenshots with the card otherwise?

No need to get irritated here.

Now let’s change to sensor from binary_sensor - it works as expected:


The problem with “unknown” happens only to “binary_sensor”.

Only when I specify “unknown” state to the binary_sensor via “Set state” panel - the card shows “unknown” state:
image
image
Otherwise, if the “unknown” state is set in “value_template”, it anyway is “turned” to “off”.