Combining NOT & AND conditions

Hi,

Really simple logic problem I can’t seem to resolve today…

I’m trying to create an automation that triggers if both of two entities are not in a specific state.

Doesn’t seem to work:

- id: New lounge presence on automation
  alias: New lounge presence on automation
  trigger:
  - entity_id: switch.lounge_presence
    platform: state
    to: 'on'
  condition:
    condition: not
    conditions:
      - condition: state
        entity_id: input_boolean.night_quiet_mode
        state: 'on'
      - condition: state
        entity_id: person.ralph
        state: 'home'
  action:
  - data:
      entity_id:
      - input_boolean.lounge_occupied
    service: homeassistant.turn_on
  initial_state: true

So I don’t want it to trigger if both input_boolean.night_quiet_mode is on and person.ralph is home, any other combination should trigger.

(Ralph being the dog and I don’t want the lights to come on at night if the dog is walking around)

Thanks for any help.

:slight_smile:

The automation as written passes if both conditions are not valid (which sounds like the opposite of what you want).
To be sure, you want this to trigger if the dog is not home and night mode is off, but also when either the dog is home or night mode is on, but not both. Is that right?

Please try this

alias: New lounge presence on automation
trigger:
  - entity_id: switch.lounge_presence
    platform: state
    to: 'on'
condition:
  - condition: and
    conditions:
      - condition: template
        value_template: '{{ states(''input_boolean.night_quiet_mode'' ) != ''on'' }}'
      - condition: template
        value_template: '{{ states(''person.ralph'' ) != ''home'' }}'
action:
  - data:
      entity_id:
        - input_boolean.lounge_occupied
    service: homeassistant.turn_on
initial_state: true

I have used templates with and condition

EDIT: this was wrong.

Thanks everyone, I fully accept this may well be my brain logic not clearly explaining it…

night mode	 off	on	    off	    on	
ralph	  	 away	away	home	home


Trigger		 yes	 yes	 yes	no

Good truth table, try this:

  condition:
    condition: or
    conditions:
      - condition: not
        conditions:
          - condition: state
            entity_id: person.ralph
            state: 'home'
      - condition: and
        conditions:
          - condition: state
            entity_id: person.ralph
            state: 'home'
          - condition: state
            entity_id: input_boolean.night_quiet_mode
            state: 'off'

So it will trigger if you are not home (away) OR ( if you are home AND night quite mode is off ).

It just seems strange that you want to turn the lounge occupied boolean on when you are away.

Thank you, I’ll give that a go.

(Ralph is a dog who spends half his time at my partners place).

1 Like

Maybe there is something I have missed but based on the table you posted, your original condition implements its logic.

Trigger Night Mode Ralph
Yes Off Away
Yes Off Home
Yes On Away
No On Home

According to the table, it should not trigger when Night Mode is on and Ralph is home (and trigger for all other combinations).

  condition: "{{ not (is_state('input_boolean.night_quiet_mode', 'on') and is_state('person.ralph', 'home')) }}"

EDIT

Apparently the original condition you posted doesn’t in fact capture the table’s logic. However, the Template Condition I posted should.

1 Like

I don’t think it does.

according to the docs that condition only returns true if both conditions are not true.

so it’s more like:

condition: "{{ state('input_boolean.night_quiet_mode') != 'on' and state('person.ralph') != 'home' }}" 

but that said your template actually does implement the logic correctly and was what I was going to post myself.

I thought the original example inverted the result of ANDing the two conditions. However, after reviewing the docs, it appears to work a different way.

Meh, I’ll go with ‘sour grapes’; the YAML style of NOT, AND, OR seems more awkward to me than a good ol’ Template Condition.

I agree.

And in this case I don’t think there even is a way to do it without a template.

the yaml just isn’t set up to do it.

Maybe you have to explicitly (not implicitly) AND the two conditions first before applying NOT (resulting YAML will be awkward). Anyway, I can’t say I have a deep interest in testing that hypothesis.

Ahem. Combining NOT & AND conditions - #7 by tom_l

Edit: oh I see what you are saying, just the not case. Yeah 123’s idea should work.

1 Like

Yeah, I think that will do it too.

When I first looked at it I didn’t think it was right.

Sorry about that.

But I still like the template better. :laughing:

the NAND implementation is a bit confusing. :crazy_face:

Hi,

Spent a bit more time on it now…

This didn’t work, I’m not sure the logic is quite right;

"{{ state('input_boolean.night_quiet_mode') != 'on' and state('person.ralph') != 'home' }}" 

But this did;

"{{ not (is_state('input_boolean.night_quiet_mode', 'on') and is_state('person.ralph', 'home')) }}"

Big thanks to everyone cos I’d spent a while going round in circles with this what I thought would be simple before i started on it.

:slight_smile:
Amanda

1 Like