Presence based lights help

I’ve been using an input_boolean for a while now as a basic presence detector. My objective is to create an automation that will turn off lights when leaving if no-one else is present.

So far, I’ve had success in doing this with an arrival lighting automation, but for some reason I can’t get it working with this one.

It either does nothing, or the lights turn off regardless of the conditions set (ie. If one of us is home, it still kills all lighting).

  alias: Departure Lighting
  trigger:
  - entity_id: input_boolean.k_present
    from: 'on'
    platform: state
    to: 'off'
  - entity_id: input_boolean.o_present
    from: 'on'
    platform: state
    to: 'off'
  condition:
    condition: or
    conditions:
    - condition: state
      entity_id: input_boolean.k_present
      state: 'on'
    - condition: state
      entity_id: input_boolean.o_present
      state: 'on'
  action:
  - data:
      entity_id: group.all_lights
    service: homeassistant.turn_off
  - data:
      entity_id: switch.power_off
    service: homeassistant.turn_on

Any help is appreciated!
It’s probably a dumb mistake on my behalf…

Doesn’t your condition equate to “only perform the action if k is home or o is home” - which seems around the wrong way.

When I have the condition state set to off then it runs whether k or o is home regardless. :face_with_raised_eyebrow:

if this can be translated to : lights have to switch Off if both K_present and o_present are Off, the you should reflect that in your automation. As it stands it does exactly what you tell it to do …

 condition:
   condition: template
   value_template: >
     {{is_state('input_boolean.k_present','off') and 
       is_state('input_boolean.o_present','off')}}

btw, you can shorten:

  trigger:
  - entity_id: input_boolean.k_present
    from: 'on'
    platform: state
    to: 'off'
  - entity_id: input_boolean.o_present
    from: 'on'
    platform: state
    to: 'off'

to

  trigger:
    - platform: state
      entity_id: input_boolean.k_present
      to: 'off'
    - platform: state
      entity_id: input_boolean.o_present
      to: 'off'

(booleans only have 2 states, so stating either the to: or the from: state suffices). In fact you could even leave the to: out completely, since it is handled in the condition.

or, you could put both booleans in a group, and use the group behavior and evaluate its state. The group will only turn off when all entities in the group are off, and if one of the entities is on, the group will be on.

Thanks, Marius. The condition:template solved the issue. :slightly_smiling_face:

1 Like

I still think you might need a further clarification for understanding why your initial automation was failing.

It wasn’t the condition template, per se, that solved the problem. It was that you were using the wrong conditions in the first place. All the template did was correct that error but you don’t need a template to do what you wanted.

The way you had the automation written in your first post was:

when either ‘input_boolean.k_present’ OR ‘input_boolean.o_present’ turned off

then check to see if

either ‘input_boolean.k_present’ OR ‘input_boolean.o_present’ was on

if yes then turn off the lights.

So, for example, if ‘o’ left (trigger) and ‘k’ was still home (condition) then the trigger occurred and the conditions were met (because only one or the other needed to be on) therefore the automation turned off the lights.

With the template the conditions changed to needing both of you being gone before it would perform the action.

the updated automation acts as the following:

when either ‘input_boolean.k_present’ OR ‘input_boolean.o_present’ turned off

then check to see if

both ‘input_boolean.k_present’ AND ‘input_boolean.o_present’ was off

Again, for example, if ‘o’ left (trigger) and ‘k’ was still home (condition) then the trigger occurred but the conditions were not met (because one of the AND conditions was not met) therefore the automation action was prevented.

You could have done the same thing without the template like:

  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: input_boolean.k_present
      state: 'off'
    - condition: state
      entity_id: input_boolean.o_present
      state: 'off'

but since AND is the default then it would be simplified to:

  condition:
    - condition: state
      entity_id: input_boolean.k_present
      state: 'on'
    - condition: state
      entity_id: input_boolean.o_present
      state: 'on'

If you already knew all of that…then never mind…:wink:

you’re right, it was ‘or-ing’ instead of the necessary ‘and’-ing’ which solved the OP’s issue.

about the condition: template. This is my preferred way of writing automations.
I always try to write this as a condition , (and not as a trigger with a to: state, because that makes the triggers less complex.), and, added to that, preferably as a condition template, because one can test in the dev-template if and how it evaluates. The condition: state can’t be evaluated there.

One could argue this is less efficient, because it now triggers more often, since there is no condition set in the triggering section, only an entity. Can’t say I have seen any downside effect of that, with all my automations though.

cheers!

1 Like

Sure. I understand. There are sometimes more than one way to do the same thing.

I just wanted the OP to understand why the way you did it worked and an alternative since most people would not do it with templates and I didn’t want any confusion the next time they see it done another way.

Thanks for all the input guys, I really appreciate it.

I found that the condition:template made most sense to me, that’s why I went with Marius’ implementation.

The knowledge and willingness of this community still doesn’t cease to amaze me!

1 Like