I made a template binary_sensor as I don’t want it to be unknown or unavailable (for automation reason)
It is based on a smoke detector that could become unavailable or unknown, that is the reason why I had to do that.
You should replace {{ states.switch.source.state == 'on' }} with the is_state() function that returns true /false and never gives an unknown result.
But it is according to history
dark lines are unknown values
EDIT So I did this and hope for the best, but isn’t it a bug?
template:
- binary_sensor:
- name: fumee_rdc
device_class: smoke
state: >
{% set result = "off" %}
{% if is_state("binary_sensor.smoke_1","on") %}
{% set result = "on" %}
{% endif %}
{{ result }}
EDIT 2 I want to keep both values as it is used in my automation to detect a failure of the smoke detector while not triggering the alarm in the whole house
The Template Binary Sensor’s template is evaluated when binary_sensor.smoke_1changes state. Until that happens, the Template Binary Sensor’s initial value is unknown.
For your application, the binary_sensor is a smoke detector so its state rarely ever changes (hopefully never, otherwise it has detected smoke). However, that makes it an entity that’s a bit too inert/quiet for use in a Template Binary Sensor.
I suggest creating a Trigger-based Template Binary Sensor with a State Trigger, to monitor the smoke detector, and an Event Trigger, to detect when Template entities are reloaded.
Assume there is some binary sensor (source sensor from some integration) which changes “on” ↔ “off” sometimes.
We created a template sensor (not trigger-based):
state: >-
{% set result = "off" -%}
{%- if is_state("binary_sensor.smoke_1","on") -%}
{%- set result = "on" -%}
{%- endif -%}
{{ result }}
To add the created sensor, we do “Reload templates”.
What happens to the template sensor after creation?
According to your logics: since the source sensor has not changed right after creation → the template sensor = “unknown”.
This does not seem correct.
What is a difference with a “trigger-based” template then? In case of “trigger-based” - the template sensor will be “unknown” untill the source sensor changes, then the template sensor is triggered and changes accordingly.
I think this is it.
If my main sensor change or if the templates are reloaded (like HA start or Developer tools), the binary sensor fumee_rdc will be update as well and it is on if the other is on.
When freshly created, it will be unknown, that’s the reason why Taras is advising to reload the templates.
I got your point but it was not working the way I did it.
Hence my initial question.
It was off when created but then switched to unknown even if it shouldn’t
Take it as a solution to my issue even thou it is not explaining the reason why my initial template is sometimes unknown…
It is a workaround with which I can live.
This should probably be tackled in an issue on the github of HomeAssistant.
Even if I’m coming to this forum multiple times a day, I believe that I miss already a million of useful informations already.
Being away for one hour and there is already a hundred of new/updated post.
This post is probably already lost for everyone, marked as solved or not.
If you believe that I marked as solution something that shouldn’t have been, flag it, a moderator will come and explain to me what we should do instead.
But as far as I’m concerned, I’m fine with Taras’s “solution that isn’t really a solution”.