Question about binary sensor as presence template

So I got around to creating two Bayesian sensors for Presence, and they work great. I then realized I had a lot of automations that used

    from: not_home
    to: home

and of course the Bayesian sensor is Boolean, so Off and on. Initially I just changed my automations to use from Off to ON, but then had issues when I restarted HA. The state would be off for a second, then to on, so of course the automation would kick off.

Then I changed the Bayesian sensor to a presensce template.

     multiple_presence_paul:
       friendly_name: "Presence Me (Multiple)"
       device_class: presence
       value_template: >-
         {{ is_state('binary_sensor.me_presence', 'on') }}   

This did seem to keep the automations from kicking off when I restarted HA. Soā€¦ Seems good, but not quite. I also have a group I use for my wife and I. I use this for a ā€œeveryone is goneā€ or ā€œSomeone has come homeā€ automations,

When I used actual device trackers, this was working. But now it does not seem to work.

   sensors: 
     group_presence_for_routines:
       friendly_name: "Group Presence"
       device_class: presence
       value_template: >-
         {{ is_state('group.presense_for_routines', 'on') }} 

If I template do I need to use home and away vs home and not_home?

Iā€™ll be doing some testing, but was wondering if others had this issue and what they did to move past it.

for a device tracker I believe youā€™ll indeed need home and not_home, though personally Iā€™d use home and != home.
If you create other zones (say work), the not_home will not be true as the value will be work (as opposed to awayā€¦

I agree, my automations seem to not be working with not_home with the template. Iā€™ll have to look closer, maybe I have something spelled wrong somewhere.

I like the idea of !=home, might have some use for that in the future.

1 Like

Iā€™ve looked at my automations, and they seem correct. I have a feeling that a template sensor does not use the same logic. When I look at the states they show as on, or off, when I click on them, they show home or away. All my device trackers show home, not_home, or one of my zones.

Iā€™ll do some searching and see if anyone else has anything similar, in older posts

thereā€™s a typo here, it should be presence

Unfortunatl, its not a typo, Iā€™m just bad at spelling, That is the name of my group. :slight_smile:

but, I do need to fix that so I donā€™t get confused in the future. haha

Thanks

Also note that device_class: presence is invalid

What is invalid? It looks to be showing home and away

@ptdalen didnā€™t provide the context, but I believe his multiple_presence_paul and group_presence_for_routines are Template Binary Sensors, and for that, it is valid.

Sorry for the lack of details, that is correct.

Sorry, one last question. I think this has helped me with figuring out why my automation has not been triggering since I started using Bayesian sensors for Presence, One issue I did have with one of my binary sensors like this was when I restarted the initial state of the sensor was off, then quickly turned to on (as it should have done), but that triggered my ā€œIā€™m backā€ automation.

Iā€™ve been reading and it does not look like I can set an initial state of a binary sensor.

Is there a simple way or condition I could add to my some of my automations to keep them from triggering after a HA restart.

Well, in general, I would say it doesnā€™t make sense to be able to set the initial state of most sensors, since itā€™s up to the sensor itself to decide its state. Itā€™s not the same as components like input_boolean, etc.

But for template sensors, I suppose you could add something in the template for this. But itā€™s probably better to handle it at the automation level. E.g., maybe somehow make the automation ignore triggers for a short time after HA starts.

There may be a better way to do this, but one thing you could do is:

input_boolean:
  startup:
    initial: 'on'

automation:
  - alias: Startup Period
    trigger:
      platform: homeassistant
      event: start
    action:
      - delay:
          seconds: 5
      - service: input_boolean.turn_off
        entity_id: input_boolean.startup

  - alias: Use input_boolean.startup as a condition
    trigger:
      <something>
    condition:
      - condition: state
        entity_id: input_boolean.startup
        state: 'off'
    action:
      <something else>

I suppose you could also use input_boolean.startup in a template sensorā€™s template to force a particular value during this startup period.

1 Like

I know you didnā€™t ask for this :wink:, but since weā€™re kind of on the topic, for what itā€™s worth, this is what I do:

input_boolean:
  guests:
    name: We have guests
    icon: mdi:account-multiple

input_select:
  home_mode:
    name: Home Mode
    icon: mdi:home-account
    options:
      - Home
      - Away
      - Returning

binary_sensor:
  - platform: template
    sensors:
      people_home:
        friendly_name: People Home
        device_class: presence
        value_template: >
          {{ is_state('group.all_devices',    'home') or
             is_state('input_boolean.guests', 'on')   }}

automation:
  - alias: Home Mode - Leaving
    trigger:
      platform: state
      entity_id: binary_sensor.people_home
      to: 'off'
    condition:
      condition: state
      entity_id: input_select.home_mode
      state: Home
    action:
      service: input_select.select_option
      entity_id: input_select.home_mode
      data:
        option: Away

  - alias: Home Mode - Arriving
    trigger:
      - platform: state
        entity_id:
          - device_tracker.person1
          - device_tracker.person2
          - device_tracker.person3
        to: home
      - platform: state
        entity_id:
          - input_boolean.guests
        to: 'on'
    action:
      service: input_select.select_option
      entity_id: input_select.home_mode
      data:
        option: Home

Then all my automations (that care about ā€œHome Modeā€) use input_select.home_mode as a trigger and/or condition.

This configuration has the following advantages:

  1. If a device_tracker entity doesnā€™t update properly (which used to happen a lot when I used SmartThings, but not so much now that Iā€™m on HA :slight_smile:), or more likely, someone forgets their phone at home, I can still manually override Home Mode via the frontend.
  2. Even if the above were to happen, Home Mode will still automatically switch back to Home whenever someone arrives. This is why the arrive automation uses the individual entities instead of binary_sensor.people_home.
  3. I have three states for home_mode since I use the Returning state to cause my thermostat to come out of ā€œECOā€ mode, yet my cameras are still active, lights are still simulating presence, etc.
  4. The input_boolean.guests switch can effectively prevent ā€œaway modeā€ automations if we have guests in the house that are not integrated with my HA setup.
  5. Since I only trigger on binary_sensor.people_home going to ā€˜offā€™, luckily itā€™s not affected by the ā€˜offā€™ -> ā€˜onā€™ transition at startup.
3 Likes

Thatā€™s great stuff. Iā€™m also a previous ST user. One of the first things I set up was a ā€œhome modeā€

Mine is
Home
Away
Night

I have had it switching modes, but never used it to trigger the automations, but instead had the automations switch the modes, haha. I like the thinking above, seem easier to use.

I have not had Guests yet, but created a group where I figured Iā€™d throw devices that were discovered by my asuswrt component, and use that for some automations. But being able to manually set a guest switch makes a lot of sense too.

I didnā€™t include it above, but I also have an input_boolean.person4 that is updated by automations that effectively use IFTTT as a trigger. So far Iā€™ve used that for my son who isnā€™t in my Life360 circle (which is where my device_tracker entities come from. The automations for that input_boolean are a bit complicated to deal with the fact that you canā€™t always count on IFTTT Applets working promptly, or at all sometimes.) The main point, though, is you can just add something like this (or what you were describing) as another entity in binary_sensor.people_home and as another trigger to the ā€œHome Mode - Arrivingā€ automation (in fact just list it as another entity along with input_boolean.guests, as long as itā€™s a binary sensor of some sort.)

Awesome, quick question I think is an easy answer. Since Iā€™m using the Bayesian sensor for our presence, this should work right? I made a couple small entity name changes to match my enties a bit more, but overall mostly the same

  - alias: Home Mode - Arriving
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.multiple_presence_me
          - binary_sensor.multiple_presence_person2
          - input_boolean.guests_present
        to: on
    action:
      service: input_select.select_option
      data_template:
        entity_id: input_select.house_mode
        option: Home 

Yes, except that I think you have to quote on:

to: 'on'

1 Like

If I use house mode as a trigger or condition do I need

quotes around the

  trigger:
  - platform: state
    entity_id: input_select.house_mode
    to: 'Home'

like above? Looking through some of my automations I seem to have several without the quotes, so I think not. Not sure how to tell when I need them and when I donā€™t.

Thatā€™s a great question. Yes, it is indeed hard to tell when you need to quote.

I believe the issue is that some strings (e.g., 1, true, yes, on, enable, 0, false, no, off & disable, in any capitalization, and there may be others, I donā€™t know) can, in some circumstances, be interpreted as, and converted to, boolean values (i.e., True & False.) If this were to happen in, e.g., a to: parameter of a state trigger, then it wouldnā€™t match properly and the trigger (probably) wouldnā€™t fire when it should.

So, the bottom line is, it never hurts to quote these. But, except for a small list of strings, itā€™s not necessary. The trick is knowing which strings in which contexts. That I really donā€™t know for sure. But I can say from experience that you donā€™t need to quote Home in this case. :slight_smile:

1 Like

Throwing my 2 cents, if the string is simple (all low caps, no special characters, no spaces) HA should handle workout quotes. But as soon as you go out of the above, you can pretty much guarantee youā€™ll need quotesā€¦

1 Like