How can this binary_sensor be unknown?

Hello there,

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.

My sensor is

template:
  - binary_sensor:
    - name: fumee_rdc
      device_class: smoke
      state: >
        {{ iif(is_state("binary_sensor.smoke_1","on"), "on", "off") }}

How could this possibly be unknown?

As per the documentation:

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
image
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_1 changes 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.

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.smoke_1
      - platform: event
        event_type: event_template_reloaded
    binary_sensor:
      - name: fumee_rdc
        device_class: smoke
        state: "{{ is_state('binary_sensor.smoke_1', 'on') }}"

After you create it, execute Developer Tools > YAML> Reload Template Entities. Its initial value should now be the same as binary_sensor.smoke_1.

3 Likes

So far, so good, thank you.
I’ll wait 24 hours to see if it stays off and mark as solution.

The continuity is not there but it doesn’t switch to unknown.


Don’t know why it is like that but it is ok for my automation, it doesn’t trigger like before.
Thank you.

1 Like

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.

Not about non-trigger based sensors.
Consider this:

  • the sensor is added in yaml
  • it is not actually created yet
  • reload templates

I repeat - the source sensor is not changed, the destination sensor is not trigger-based - so as per the logics described above - the sensor is supposed to be unknown - but I do not observe this.
So again - How can this binary_sensor be unknown? - #5 by Ildar_Gabdullin

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

Sorry, did you understand a reason of your issue? I am asking since you marked the issue as SOLVED.

I believed that non-trigger based sensor cannot be unknown after creation, but trigger-based is unknown since it is not triggered yet.

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.

If something does not explain - it is not a solution.
Also, since the topic Is SOLVED - less people will pay attention.

I got your point. You’re right.

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”.

It is ok, I will probably create a new thread.

I did, it is here

Very possible that I’ll be banned for that one …

1 Like

Asking questions is not a silly thing)

Indeed, but there is substance and form, and I am not sure about the form.