If A+C, or if B+C

I have an if-then-else set up. If any of two conditions matches, do X, else, do Y. Easy enough.

I would like to add an additional requirement which, if not met, results in Y instead of X, irrespective of which of the two conditions gets matched.

So currently, it’s “if one of A or B is true,” but I want it to be “if one of A or B is true, and C is also true.”

How can I do this?

Are you asking about an automation’s trigger, a script condition, a template, or something else…?

https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371/3

An automation trigger. Here’s what it is currently:

actions:
  - if:
      - condition: or
        conditions:
          - condition: template
            value_template: '{{ trigger.json.Item.MediaStreams[0].ColorPrimaries == ''bt2020'' }}'
          - condition: template
            value_template: '{{ trigger.json.Item.Video3DFormat == "MVC" }}'
    then:

But I want to add a boolean toggle on my dashboard that will override the behavior.

If “C” is a condition that should be applied generally to all all runs of the automation, you would add it to the conditions block that precedes the actions.

triggers:
   ....
conditions:
  - condition: state
    entity_id: input_boolean.YOUR_TOGGLE 
    state: 'on'
actions:
  - if:
      - condition: or
        conditions:
          - condition: template
            value_template: '{{ trigger.json.Item.MediaStreams[0].ColorPrimaries == ''bt2020'' }}'
          - condition: template
            value_template: '{{ trigger.json.Item.Video3DFormat == "MVC" }}'
    then:
....

If “C” only applies to actions within the If/Then, add the condition wherever you need it:

triggers:
   ....
conditions: []
actions:
  - if:
      - condition: state
        entity_id: input_boolean.YOUR_TOGGLE 
        state: 'on'
      - condition: or
        conditions:
          - condition: template
            value_template: '{{ trigger.json.Item.MediaStreams[0].ColorPrimaries == ''bt2020'' }}'
          - condition: template
            value_template: '{{ trigger.json.Item.Video3DFormat == "MVC" }}'
    then:
      ....
    else:
#     Conditions are also valid in action sequences, so the condition could
#     also be added here if needed to control the execution of these actions.
      - condition: state
        entity_id: input_boolean.YOUR_TOGGLE 
        state: 'on'
....

For more complicated branched logic you can use a Choose action, which supports If/Elif/Else constructions.

If the toggle is off, I want it to have no impact. If the toggle is on, I want it to perform the “else” action even if the ColorPrimaries or Video3DFormat suggest it should perform the “then” action.

My understanding is that your code will execute the “then” action depending on the toggle irrespective of the primaries or 3D format, rather than executing the “else” action depending on the toggle irrespective of the primaries or 3D format.

actions:
  - if:
      - condition: state
        entity_id: input_boolean.YOUR_TOGGLE 
        state: 'off'
      - condition: or
        conditions:
          - condition: template
            value_template: '{{ trigger.json.Item.MediaStreams[0].ColorPrimaries == ''bt2020'' }}'
          - condition: template
            value_template: '{{ trigger.json.Item.Video3DFormat == "MVC" }}'
    then:
      ...
    else:
      ...
1 Like