Binary template trigger sensor not persistent after restart

Hello.

I have a template binary sensor that is triggered by a webhook.

template:
  - trigger:
    - platform: webhook
      webhook_id: xxx
    binary_sensor:
      - name: "Videocall"
        state: "{{ 'provider' in trigger.json }}"
        delay_on: 0:00:05

The webhook receives a JSON and if there is a certain key in it (‘provider’), it should render true if the webhook isn’t triggered again within 5 seconds. If the JSON doesn’t contain the key it should render false. The sensor works without a problem when HA is running.
If I restart HA while the sensor is true it always renders false after restart. There is no triggering in the meantime and also in the logbook the last event is that it was turned on. Why doesn’t the sensor store it’s state at restart? Shouldn’t it just change when it is triggered?

P.s.: I’m not looking for a workaround. I know that I can achieve what I want with an input boolean and an automation. I want to understand why this isn’t working.

Probably because on a restart lights and switches of physical devices are updated by the integration that provides access to those devices, which is why they “remember” the state of those devices. But if you have MQTT lights for example or even temperature sensors Home Assistant will not know the state of them on a restart until it has received state messages for each device. The template sensors are not behaving in a different way to the rest of the Home Assistant, they are evaluated at startup like everything else, in this case there is nothing to evaluate for your sensor (at startup) because it is waiting for trigger, and because it is a binary_sensor whose state is essentially unknown, it can only evaluate to false (off).
The alternative to this - would be Home Assistant “remembering” the state of every device, and on a restart setting each device to the state it last remembered, which may not be the state devices are actually in now in the real world.

1 Like

Because the state value of a Template entity is not stored, it’s computed by its template.

In this case, it’s a Trigger-based Template Binary Sensor so its state value is computed by its template when triggered by its Webhook Trigger. The value it computes persists until it’s re-computed by the next trigger or by a restart or a Reload Template Entities. In this case, this entity doesn’t have triggers to handle restart/reload so its state value defaults to false (although I suspect that might change to unknown in a future version).

1 Like

Surely a binary sensor can’t have an unknown status though? A binary sensor is either true or false, if the status is unknown - then it is not true, so it has to be false?

It’s essentially Schrodinger’s Cat - the cat is dead, until proven to be alive.

What if the cat is out of sight, then you don’t know if it’s dead or alive.
Surely it’s unknown.

I have an ESP-Home device that reports a binary sensor to Home Assistant, when I unplug the ESP the binary sensor becomes unknown

or possibly unavailable, as well, depending on the device/integration.

That’s confusing,
Binary is 1 or 0. That’s literally it’s definition (in computing anyway).

It can only have 2 states. 1 or 0. The observation of it is irrelevant, it can’t ever be in a state where it is 0 and 1 at the same time. If a device is on, but you aren’t observing it at the time, it doesn’t stop the device still being on.

A binary sensor should always be in a true or false state. Even if it becomes unavailable, it should be in the last known state until it becomes available again. If my ESP8266 turns the boiler on, and then becomes unavailable - I still want to know the boiler is on, unknown is not helpful to anyone.

I would say you are wrong.

A binary SENSOR is different from a binary output.
The sensor can be disconnected from what it’s sensing, it can be off, it can be broken.
The binary output on the other hand, yes 1 or 0.

And as finity said it was perhaps unavailable.

Found the relevant PR; it has been merged into the development branch and, in all probability, likely to be part of the next release (and it’s a Breaking Change):

Basically, it will behave like other Trigger-based Template entities; it doesn’t have a state value until one of its triggers fires and causes its template to compute a value.

1 Like

There are many programming languages (including Python) that allows for a nullable state. It’s not uncommon. For programming languages it often means it’s an uninitialised value. You (and others) are confusing boolean/binary datatypes with logic systems. A boolean datatype can be ternary. Also, some languages in case of binary logic defines zero as false and any other value as true (it’s not only about 0 vs 1, which is an implementation detail). There are subtleties and variations — and a good dose of pragmatism needed.

Technically, you’re not wrong, but there’s more to the story and what HA does isn’t ridiculous.

1 Like

Well no, what they do which makes perfect sense to me as a programmer - is consider zero AND any negative number as False and any positive number as true.

Ternary logic is not something I have previously encountered (off the top of my head anyway), although the ternary logical operator is something I use on a daily basis - but of course is still essentially something that evaluates to true or false.

Thanks for the clarification. This and the PR show me that I need to take another route for my problem.