Combining 'AND' and 'OR' conditions

I’m trying to set up an automation that turns on a smart switch when one of my TV’s turn on, but I can’t seem to figure out what I’m doing wrong. I use the brightness setting of a light in the same room as a trigger:

- id: '1538927914592'
  alias: TV on plug on (Attick)
  trigger:
  - platform: numeric_state
    entity_id: light.attick
    value_template: '{{ state.attributes.brightness }}'
    below: 70
    for:
      seconds: 4
  condition:
  - condition: state
    entity_id: media_player.sony_XXXX
    state: 'on'
  - condition: state
    entity_id: media_player.panasonic_XXXX
    state: 'on'
  - condition: state
    entity_id: sun.sun
    state: below_horizon                                                  
  action:
  - entity_id: light.onoff_plug_1
    service: light.turn_on

I want the plug to turn on when one of my tv’s turns on, but as I set it up right now that doesn’t seem to work.
I tried changing the conditions to:

condition:
  condition: or
  conditions:
    - condition: state
      entity_id: media_player.sony_XXXX
      state: 'on'
    - condition: state
      entity_id: media_player.panasonic_XXXX
      state: 'on'
    - condition: and
      conditions:
        - condition: state
          entity_id: sun.sun
          state: below_horizon

Edit: I’ve set up an automation that lowers the brightness when the tv turns on btw. The setup works if I leave out one tv.
I hope someone can help me out. Thanks in advance for any help.

If I’m reading it correctly (and apologies if I’m not, I’ve just woken up), shouldn’t your condition block be…

condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  - condition: or
    conditions:
      - condition: state
        entity_id: media_player.sony_XXXX
        state: 'on'
      - condition: state
        entity_id: media_player.panasonic_XXXX
        state: 'on'

(which says the sun must be below the horizon, and either of the media players is on)

4 Likes

I think you need to modify it a bit. Try this one:

condition:
  condition: and
  conditions:
    - condition: state
      entity_id: sun.sun
      state: below_horizon
    - condition: or
      conditions:
        - condition: state
          entity_id: media_player.sony_XXXX
          state: 'on'
        - condition: state
          entity_id: media_player.panasonic_XXXX
          state: 'on'

Effectively you want to say: sun is below horizon AND (sony is on OR panasonic is on). Hope this helps.

@ton2008 - that does exactly the same as mine, but with two extra lines of text :wink:

@anon43302295 - yeah, you were a bit faster as well!:smile:

Though for the extra two lines: I prefer to make everything explicit, sometimes it facilitates modifications later. And it is definitely better for people who just start writing complex conditions.

FYI using and on the top level of condition have caused problems for many people. You really should go with @anon43302295’s solution if you want to avoid future headaches.

Sounds a bit strange to me but you definitely have much more experience with yaml and different people :smile:

I also have some conditions with and at the top. Could you eloborate on the problem. I might be having issues which I am not even aware of.

I’ve just seen automations flat out not work with an and condition. I’ve worked with at least 3 people in the last year who had the correct yaml but the automation didn’t work. As soon as

condtion: and
conditons:

was removed, the automations worked.

This may be purely anecdotal, but it’s what I’ve seen on more than one occasion.

EDIT: This only applied to the top level:

  condition:
    # This is the top level I'm refering to.
    - condition: and
      conditions:
        # Second level, works fine.
        - condition: and
          condtions:
            # Third level, never tried.
            - condition: state

To be fair, there’s no reason it shouldn’t work and I suspect that the people you’ve helped have had indentation problems with the top level and probably other syntax problems that you’ve fixed along the way.

But I definitely think less is more when it comes to this kind of thing and fewer lines in the configuration is fewer places for mistakes to creep in :slight_smile:

1 Like

This looks like what I’m looking for indeed. I’ll try It out and report back.

@everybody, thanks for all the replies. Didn’t expect that to happen :+1:.

I also have this condition and it works. But it’s good to know that the default action for conditions are “and” and it doesn’t need to be explicitly written. Thank you.

  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: switch.wall_switch_xxxxxxxxxx
      state: 'off'

    - condition: or 
      conditions:    
      - condition: time
        weekday:
        - tue
        - wed
        - thu
        - fri
        - sat
        - sun
      - condition: time
        before: 09:00:00
        after: '15:00:00'
1 Like

Thanks for the help. Your solution is working wonderfully.

Just another quick question. For the automation I’m using the brightness as a trigger:

  trigger:
  - platform: numeric_state
    entity_id: light.attick
    value_template: '{{ state.attributes.brightness }}'
    below: 70
    for:
      seconds: 4

I’m trying to use state.attributes.brightness_pct instead of state.attributes.brightness, but HA doesn’t like that.

I tried (as I have the light set up so it dims when a tv state changes from off to on):

value_template: '{{ state.attributes.brightness }}'
below: 22

But it doesn’t like the number 22 for some reason. Do I have to use quotes maybe or other punctuation marks?

Edit:

And is it better to explicitly create a condition with the original automation that states that the plug I’m triggering should be in the ‘off’ state. Like this:

  - condition: state
    entity_id: light.onoff_plug_1
    state: 'off'

Or doesn’t that matter?

I do this because I like to check my logs often and I don’t like to see automations firing for no reason and not doing anything, but functionally it doesn’t matter too much.

But it doesn’t like the number 22 for some reason. Do I have to use quotes maybe or other punctuation marks?

You don’t need anything apart from the number there. It’s strange that it doesn’t seem to like that number.

Try

trigger:
  platform: template 
  value_template: "{{ state_attr('light.attick' , 'brightness_pct')|int < 22 }}"

And yeah, I would use the condition to stop the automation firing in the first place if I know for a fact that it shouldn’t be running in those circumstances.

Hope this helps :+1:

Yeah, I thought as much. That would be one of the reasons I’d add it in as well.

I wouldn’t have come up with that myself. Thanks for the quick response. I’ll try it out tonight.

1 Like

That’s wrong for the value template on a numerical_state trigger. He has the correct syntax. In all other value templates (for the most part) your statement is true.

you could try just doing the calculation yourself if you want to maintain the ‘for 4 seconds’ portion of your trigger.

  trigger:
  - platform: numeric_state
    entity_id: light.attick
    value_template: '{{ state.attributes.brightness / 255 }}'
    below: 22
    for:
      seconds: 4

Also, I’m not sure you are aware, but 22% does not translate to 70 pot value. It translates to 56. So if you wanted to maintain the 70 pot value, you’d want below to be 27.5

Oops! Thanks for the correction.

1 Like