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’.
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:
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.
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:
Otherwise, if the “unknown” state is set in “value_template”, it anyway is “turned” to “off”.
Because you’re talking past the original issue, which is your icon template and you’re focusing on this completely unrelated question. Do you want to fix your unknown icon or what?
“He” did restart even after changing a code.
Then I played with input_select, and changing the “input_select” was supposed to change the binary_sensor too.
Right, which has been explained. If you just restarted after executing your logic, it should restore your previous state.
Then the word ‘unknown’ being output should result in on or off, because binary sensors only accept on, off, or none. All other words resolve to either on or off because that’s how the resolver works.
You can try just making a simple binary sensor that just outputs the word {{ 'unknown' }} and see what it resolves to.
if you want tri-logic, then set your unknown case to output none or make an availablity template for that case. It will show up as unavailable with an unknown icon.
Binary_snesors can only be unknown at startup with trigger based binary_sensors and IIRC that now restores states so you shouldn’t get unknown in 2022.5 anymore.
When the sensor becomes unavailable, it’s icon is same as it was before this change (except Sensor 4). So, if the sensor becomes unavailable, it’s icon cannot be changed - the code “icon_template: mdi:battery-unknown” does not work.
Output for Sensors 2, 3, 5 is same. Defining device_class does not affect on the icon since it is controlled by the icon_template.
It is possible to change the icon for “unknown” case (see Sensor 1) but the state will be reported as “off”.
The triple-state sensor (not binary) has a properly changing icon & state; icon’s color is not changing (since this is not binary_sensor; may be fixed by Custom-UI or card-mod); state is “on/off” instead of “charging/not charging” (may be fixed by custom cards or card-mod).