Entity state to binary

Hey,

So I’ve on all of our mobile devices trackers
the state cycles between several states like: home, away, work etc…

I would like to alert when all mobile devices are != home and home_alarm == disarmed

So setting that automation is really easy but I first need to find a way to convert a state to a binary and later combine all those binaries to a group.

So the question is how do I convert the state of my trackers when != home is true and any other state is false?

Cant answer your question directly, however if you simply want to know or trigger something when all trackers are showing as ‘home’ you could use the home zone entity as it shows number of devices in the home zone.

You could also create a template binary sensor I guess to show on/off based on the number of trackers shown as home.

You could use a template sensor

template:
  - sensor:
      - name: "Alice is home"
        state: >
          {{ is_state('device_tracker.alice','home') }}

That will be true (on) if device_tracker.alice is home and false (off) otherwise

In an automation you can also use not conditions, so you could check that they’re all not home:

condition:
  alias: "Nobody is home, I'm so lonely"
  condition: not
  conditions:
    - condition: state
      entity_id:
        - device_tracker.alice
        - device_tracker.bob
        - device_tracker.charlie
      state: "home"
1 Like

As for the condition in automation that fine but missing a trigger
My idea is to write a new binary sensor that turn true when all trackers are NOT in state home.

So what I’m missing is where I write that and how :slight_smile:

So I’ve actually found out this template thing
and now I’ve 3 entities that report true when the state is not home.

Cool,
So now I’ve tried to create a group of binary sensors and those new entities won’t show up in the drop down as if they are not binary.

Do I need to convert them or something?


And after a few minutes of reading I found out that I just need to change sensor to binary_sensor
I’m now able to group them.

Well, the trigger is simple enough, you’re looking at the same entities going from home

- platform: state
  entity_id:
  - device_tracker.alice
  - device_tracker.bob
  - device_tracker.charlie
  from: 'home'

An alternative method that for me seems simpler and does not require any adjustments / alterations if any tracker entities are added, removed and or adjusted would be something like this:

- binary_sensor:
    name: 'no one is home'
    state: {{states('zone.home') | float(default=0) == 0  }}

This should return true if all trackers are absent and false if any are present!

1 Like