Binary sensor with three states

Hi everyone,

I have tried to create a binary_sensor based on three sensors. The binary_sensor will be used as blocking condition for another automation and shall be “on” when all three sensors return “true” and “off” when all three sensors return “false”.

      - name: Kamera Benachrichtigung Nachtmodus
        state: "{{is_state('sensor.xxxs_iphone_battery_state','Not Charging') and is_state('person.xxx','not_home') and is_state('binary_sensor.nacht','off') }}"

So far the binary_sensor changes to “on” or “off” when one of the condition changes, but I want it to change only when all three basically return “true” or “flase”.

Your requirements are as follows:

on = true, true, true
off = false, false, false

There are three binary values so there are 23 combinations. Your requirements specify what the binary sensor should report for 2 of the 8 combinations.

off = false, false, false
on = true, true, true

What should the sensor report for the other 6 combinations? It’s a binary_sensor so it must report either on or off.

off = false, false, false
??? = false, false, true
??? = false, true, false
??? = false, true, true
??? = true, false, false
??? = true, false, true
??? = true, true, false
on = true, true, true


EDIT

Perhaps what you require is a sensor, which can report any number of states, as opposed to a binary_sensor.

1 Like

I believe that given the example he posted he wants it to switch when all three changes state.
I think its going to be used as a reminder to charge the phone at night so only when all three is true should it be true, and then only when all three becomes false should it be false (not that I think that is a good idea, because if you stay home all day then it wont change).
So kind of how the “all” option of groups work but both ways.
Group - Home Assistant (home-assistant.io)

0la1’s posted template reports on when all three are true otherwise it reports off. However, it’s apparently not the desired behavior because:

The problem is that there are other possible combinations other than merely ‘all on’ and ‘all off’. The binary _sensor has to report on or off for those as well.

Hi,

maybe I was thought the solution would be a simple one when the problem is actually a bit more complicated.

What I have is an automation for notifying me when a camera reports movements and send me a screenshot. However this is only working when neither my wife or me are at home. That means if both are home and at sleep, I do not get a summary of what happends and night.

So the idea was to set up a second automation that allows sending notifications with snapshots to me, during the night. I dertmined that (for the time being) the most accurate way of specifying a “I am in bed” situation is when my phone is charging, I am at home and the sensor.nacht is on (between 22:00 and 06:15).

The automation is coming from a blueprint that has a blocking condition in it, which if the sensor defined as blocking condition is “on” prevents the automation from being triggered. I therefore would need to have an option to create a sensor that is on (blocking the automation) when my phone is not charging, I am not at home and it is day. The sensor would need to be switched to “off” if all of the coditions change and I am sleeping.

I hope that does make sense?

  1. What if only one or two of the three conditions changes to false?
  2. What serves to indicate when you are sleeping? When your phone is charging, you are home, and it’s night?
  1. if one or two change I don’t want to have the main sensor to change. When all three true = On. When all three false = off.

Not charging - true
Not at home - true
Not night - true

Sensor - On

Not charging - false
Not at home - false
Not night - false

Sensor - Off

Not charging - true
Not at home - false
Not night - true

Sensor - Off

Etc.

Basically when all three sensors are are false it means I am sleeping. Since I have no sensors in my bedroom yet I did not think of a better idea to determine if I’m sleeping.

I would also be happy to have a different workaround to send me notifications only when I am in bed. If the route I had in mind with the sensor is to far off.

The examples you just posted represent how your existing template currently works. It reports on only when all three conditions are true. If any of the three conditions is false then it reports off.

I think I have difficulties to express my self, sorry. :see_no_evil:

Basically only if all three are true the sensor shall be on and if all three are false it shall be off. The rest of the combination shall be ignored.

Ignore is not an option.
If you mean it was true, so keep it true.
Or it was false, keep it false then that is possible but ignoring is not a state or a binary sensor so that won’t work

it is with templates

{% set a = is_state('sensor.xxxs_iphone_battery_state', 'Charging') %}
{% set b = is_state('person.xxx','not_home') %}
{% set c = is_state('binary_sensor.nacht','on') %}
{% if a and b and c %}
  true
{% elif not a and not b and not c %}
  false
{% else %}
  {{ this.state }}
{% endif %}

You can’t ignore the other 6 combinations. They have to be represented by either on or off.

The example posted by petro reports the other 6 combinations as either on or off. Which one it chooses is based on the Template Binary Sensor’s existing state. It behaves like this:

Example 1

  1. Let’s say all three conditions are true so the binary_sensor reports on.
  2. Now one of the three conditions changes to false. The binary_sensor continues to report its existing state on.
  3. Now two of the three conditions change to false. The binary_sensor continues to report its existing state on.
  4. Now all three conditions change to false.The binary_sensor reports off.

Example 2

  1. Let’s say all three conditions are false so the binary_sensor reports off.
  2. Now one of the three conditions changes to true. The binary_sensor continues to report its existing state off.
  3. Now two of the three conditions change to true. The binary_sensor continues to report its existing state off.
  4. Now all three conditions change to true. The binary_sensor reports on.

Is that the behavior you want? If it’s not then you can’t use petro’s template either.

@123 This is the behaviour I would like to see (at least what I think). I will try out the example from @petro and see if the outcome is what I need.

I will need to do some deep diving in logics of code I think. :slight_smile:

For applications involving more complex logic (based on probabilities), you may wish to consider using the Bayesian integration.

I came across this when searching for topics related to my problem but thought it would be a little overkill for my problem. But it seems worth reading. And as usual, when reading new topics it opens up a bunch of new automation ideas. :see_no_evil:

So you came across your own topic?

No, I came across the Bayesian sensor but not related to my issue.