Question about binary sensor as presence template

I got this suggestion on reddit:

Good stuff

I found some good info on a uptime sensor

I added this to my sensors

  - platform: uptime
    name: "HA runtime in minutes"
    unit_of_measurement: minutes

Then added this to automations that were having issues

  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1 

I also did a lot of changes to move more of my arrival/departure automation to be based on input selects, which can save the states using the recorder item in the configuration.yaml

# Record events
recorder:
  purge_interval: 1
  purge_keep_days: 5

I have this as well. Works pretty good so far.

1 Like

I tried to do the same but inside the bayesian sensor, but it doesnā€™t seem to work for some reason?


binary_sensor:
  - platform: bayesian
    prior: 0.75
    name: fez_presence_bayesian
    probability_threshold: 0.95
    observations:
      - entity_id: 'device_tracker.ping_fezsiphone7'
        prob_given_true: 1
        prob_given_false: 0.4
        platform: 'state'
        to_state: 'home'
      - entity_id: 'device_tracker.nmap_fezsiphone7'
        prob_given_true: 1
        prob_given_false: 0.3
        platform: 'state'
        to_state: 'home'
      - entity_id: 'device_tracker.icloud_fezsiphone7'
        prob_given_true: 0.4
        prob_given_false: 0.4
        platform: 'state'
        to_state: 'home'
      - entity_id: 'sensor.home_assistant_uptime'
        prob_given_true: 1
        prob_given_false: 0
        platform: 'numeric_state'
        below: 3

couple of things. Iā€™m not an expert so I could be wrong.

I donā€™t believe you can give a value of 1. 1 means 100% accurate all the time. If that was the case, then you would not need multiple. Try .9

The uptime was not meant to be used for the bayesian sensor, but as a condition in automations that trigger when you restart. Maybe it could work here, interesting concept, but I think it would not work well.

Also just on a personal level. I found ping to be useless for the iphone. It only reports true when Iā€™m actually up and looking at my phone, soon after it reports off. Makes it pretty useless to me unless your phone is up and screen unlocked 100% of the time.

I recommend starting off with one or two sensors, get it to work and then add a couple more, then play with the true and false values until it works like you need it to.

Yes you can use 1, in my case it means that when the uptime is below 3 minutes this is going to be 100% accurate.

About ping, I recently added it as a fallback to nmap and it helped to make the sensor more reliable.

Good to know about the value of 1.

So does the sensor work without the uptime sensor added?

To me the syntax looks correct, so if it were me, Iā€™d remove one sensor and see if it works, if so, then figure out what is ā€œwrongā€ with the logic there.

As far as ping, totally a matter of choice. Like I said, for me on an iPhone, I realized that it only shows as ON when the phone is actively being used. If thatā€™s a useful sensor, then use it. Of course it took me a while to realize that because I used my phone much of the time I was looking at presence, etc. It was not until I looked at states and state history on my computer that I realized that how it was working. I definitely agree that if it was on I was home, but that total ON time might only be 5 minutes here, 15 minutes, there, then off for hours.

I mean, the sensor works just fine, but it doesnā€™t mark me as home on restart of Hass, probably Iā€™m doing something wrong, or the other sensors combined are still stronger than the uptime sensorā€¦ probably I just have to reduce the prob_given_false of them? Iā€™ll try

1 Like

Ahh that makes sense. I understand what your goal is now. I suspect that it still will not work, as it takes time to get the states from the other sensors, so even after a restart it might be off for a bit before switching to on. Not 100% sure though, so if you are able to get that working as intended, that would be pretty cool. I could see that having other uses as well.

For me I only have 4-5 automatons that were having issues, and adding the condition of uptime to those automatons solved my issue 100%. With the added bonus of not having to come back later and change anything if I made other changes.

  condition:
  - condition: numeric_state
    entity_id: sensor.home_assistant_uptime
    above: 1 

So, I removed the uptime sensor from the bayesian and edited the presence sensor instead:


  - platform: template
    sensors:
      fez_presence:
        friendly_name: "Fez presence"
        device_class: presence
        value_template: >-
          {% if states('sensor.home_assistant_uptime')|float > 3 %}
            {{ is_state('binary_sensor.fez_presence_bayesian', 'on') }}
          {% else %}
            {{ True }}
          {% endif %}

This seems to work fine!

You could simplify that a bit:

  - platform: template
    sensors:
      fez_presence:
        friendly_name: "Fez presence"
        device_class: presence
        value_template: >-
          {{ states('sensor.home_assistant_uptime')|float < 3 or
             is_state('binary_sensor.fez_presence_bayesian', 'on') }}
2 Likes

right, thanks

If I wanted to display a sensor in the UI that included zone, what would you recommend. I would probably be happy with just using the standard HA tracker that has the zone as a state. I can show it as a device tracker, but would like to show it as a sensor with an image, if possible I swear I had that going on a while back, but cannot seem to get it to show anymore.

Think you can use a template sensor and set the device class as a device tracker and use the entity_picture_template to change your pic dynamically.

So what I have which is not bad it this (AC is a zone). Also how do I get ā€˜homeā€™ to be captilized, :slight_smile: Itā€™s capital as a zone name.

Image-1

That is based on this in my sensors.yaml

  - platform: template
    sensors:
      paul_status:
        value_template: '{{ states.device_tracker.pauls_iphone.state }}'
        friendly_name: 'Paul'

      tracy_status:
        value_template: '{{ states.device_tracker.tracys_iphone.state }}'
        friendly_name: 'Tracy'

What Iā€™d ultimately like would be just a Pic of each of us in the circle, and in the zone below. I can get HOME, and AWAY if I use a binary sensor with a device class, but it will not show the zone. can I add

device_class: presence

to a template in a sensors.yaml? the examples Iā€™ve seen in the forums mostly focus on binary sensors

Easy, if you want it completely capitalized (i.e., not just the first letter.) Also, you might want to use the states() function to avoid errors if the entity doesnā€™t exist for some reason (e.g., during startup. Probably not necessary, but I think a good habit to get into. :slight_smile:) So:

value_template: "{{ states('device_tracker.pauls_iphone')|upper }}"

According to the docs, you can add device_class to a template sensor, but it doesnā€™t support presence as an option:

Thanks, good info. All caps will probably work for now, but what is weird is that all my zones (other than Home), show proper capitalization. Home is capitalized in my configuration.yaml (where my zones are). Would prefer just to have it as ā€œHomeā€, so that I dont have to captilize everything like ā€œSCHOOLā€, etc.

Again, no biggie. As far as showing the zone info below the pic, Iā€™ll keep reading,

Thanks

Yeah, for Device Tracker entities, home and not_home (all lower case) are special state values. Itā€™s the frontend that shows them as Home and Away I believe. But if you want, you could do something like:

value_template: >
  {% set loc = states('device_tracker.pauls_iphone') %}
  {{ 'Home' if loc == 'home' else 'Away' if loc == 'not_home' else loc }}

If youā€™re currently in a zone (other than home) it should show the zone name as you defined it.

1 Like

Thanks for the great tips. I will definitely use this elsewhere in my system, I swore when I first started messing with HA, before I really knew what I was doing, I had all my sensors showing up in my default view, and I swore I had a sensor like this for presence. I photo shopped the ZONE in this image

goal

I mean I feel very confident that I remember zone names in the orange oval. Now of course Iā€™m changed my device trackers, etc. But I feel like Iā€™m just missing something simple. Is there any sort of device, sensor, etc that can display the zone in that area. If I could do that, then I could put an image vs text for the circle.

I know I could do home and away like that with a binary sensor.

So maybe Iā€™m not following, but the basic device_tracker, as shown in the frontend, as-is, I think already does that. E.g., here I am:

image

The name of the zone Iā€™m currently in is WorkPB, which is shown in the oval (albeit completely capitalized) because itā€™s also the device_trackerā€™s state:

image

The picture, and the friendly name, come from my customize.yaml.

Ha, thanks. That helped me to figure out what I was doing wrong. :slight_smile:
I started cleaning up my groups, and I had a group of device trackers. Then I displayed that group in another group. like this.

Got so focused on the list and groups, forgot the basics. Thanks!!

1 Like