Complex Conditions Using And + Or

Essentially: if there is motion detected, and the living room light is currently off, and its after dusk, and im not on vacation and neither the chromecast or kodi are playing, then turn the light on.

I was able to get it working without the media player options but adding them to the equation has been the pain.

- id: motion_livingroom_night_on
  alias: Motion livingroom nighttime on
  hide_entity: true
  trigger:
    - platform: state
      entity_id:
       - sensor.livingroomsensor_burglar
      to: '8'

  
  condition: 
    condition: and
    conditions: 
      - condition: state
        entity_id: light.living_room
        state: "off"
      - condition: state
        entity_id: sun.sun
        state: below_horizon
      - condition: state
        entity_id: input_boolean.vacation_mode
        state: "off"
      - condition: or
        conditions: 
          - condition: template
            value_template: "{% if is_state('media_player.nut', 'playing') %}false{% endif %}"
          - condition: template
            value_template: "{% if is_state('media_player.pauls_cast', 'playing') %}false{% endif %}"

  action:
    - service: light.turn_on
      entity_id: light.living_room

Any help is greatly appreciated. I borrowed a lot of ideas from here:
https://community.home-assistant.io/t/help-with-and-and-or-conditions/
The code validates but it doesn’t run. May be an indenting issue but I can’t figure out where.

First, please update your post using the </> code button in the toolbar. This makes it easier to help since spacing is essential in the configuration files.

ok, no problem.

Did you test the template in developer tools? I wonder why you added \n to template.

yup, I had tested it there. Removed it now.

i think,

  - condition: or
    conditions: 
      - condition: template
        value_template: "{% if is_state('media_player.nut', 'playing') %}false{% endif %}"
      - condition: template
        value_template: "{% if is_state('media_player.pauls_cast', 'playing') %}false{% endif %}"

should be

  - condition: 
      condition: or
      conditions: 
        - condition: template
          value_template: "{% if is_state('media_player.nut', 'playing') %}false{% endif %}"
        - condition: template
          value_template: "{% if is_state('media_player.pauls_cast', 'playing') %}false{% endif %}"

That doesnt validate when i enter it

It is possible to just use one OR condition under your AND section. That would probably be the easiest thing.

In what way?
Right now there is only one or.
Either Cast is Playing Or Kodi is not playing

the and conditions are dusk, vacation, light status

edit: playing to not playing. which was what i actually meant for.

you would do it

   condition: and
    conditions: 
      - condition: state
        entity_id: light.living_room
        state: "off"
      - condition: state
        entity_id: sun.sun
        state: below_horizon
      - condition: state
        entity_id: input_boolean.vacation_mode
        state: "off" 
      - condition: template
        value_tempate: "{{ states.media_player.nut.state == 'playing' or states.media_player.pauls_cast.state == 'playing' }}"

the last line is true if either is playing and false if both are not playing, but it’s still an and condition. so all 4 would have to be true in order to trigger.

I think this is what you were looking for.

mta: fixed syntax

1 Like

I had to change it to reflect “not playing”
{{ states.media_player.nut.state != ‘playing’ or states.media_player.pauls_cast.state != ‘playing’ }}

but no luck

alright… last thing I can think of. Try a one liner

condition:
  condition: template
  value_template: "{{ states.light.living_room.state == 'off' and states.sun.sun.state == 'below_horizon' and states.input_boolean.vacation_mode.state != 'off' and (states.media_player.nut.state != 'playing' or states.media_player.pauls_cast.state != 'playing') }}"

if you take just the {{ states.light.living_room.state == 'off' and states.sun.sun.state == 'below_horizon' and states.input_boolean.vacation_mode.state != 'off' and (states.media_player.nut.state != 'playing' or states.media_player.pauls_cast.state != 'playing') }} and throw it in the template editor it will show True or False. If it’s showing neither then something isn’t there or a state is incorrect.