Automation with AND OR NOT relations

Yesterday I opened a topic, but I think it was far to complicated :slight_smile: So, as a newbie I’d like to ask your help.
I have 4 MQTT switches (Sonoff-Tasmota), let’s call them: Sw1, Sw2, Sw3, Sw4
I’d like to have an automation like:
If I switch on (Sw1 OR Sw2 OR Sw3, so any of them) and Sw4 is in Switched off state, it switches on Sw1 AND Sw2 AND Sw3 (but no Sw4)
If I switch on Sw4, it switches on only Sw1 AND Sw2
If I switch off any of the 4 switches, all other switches off.

How could I make the automation?

No MQTT here, but here is the idea using switch.
First, triggers are always “or”, because it’s not about the status the device is, but the status the device goes into. So your first condition “If I switch on (Sw1 OR Sw2 OR Sw3, so any of them)” is just a list of entity in the trigger.
The “sw4 is off” is a condition. Conditions are ‘AND’ if just a list. You can have an “OR” condition if needed.
And then in the action, you put anything you want.
So you first automation will look like :

trigger:
- platform : state
  entity_id : switch.sw1
  to: 'on'
- platform: state
  entity_id: switch.sw2
  to: 'on'
- platform: state
  entity_id: switch.sw3
  to: 'on'
condition:
- condition: state
  entity_id: switch.sw4
  state: 'off'
action:
- service: switch.turn_on
  entity_id: switch.sw1
- service: switch.turn_on
  entity_id: switch.sw2
- service: switch.turn_on
  entity_id: switch.sw3

Then you can do 2 other automation for the 2 other cases.
There are other ways to write it, like with several entities in the same trigger or action, or just one automation for everything, but I like to keep all separated. It’s easier to modify later.

Thanks, that’s perfect. I’ll try if I can do
trigger:
- platform : state
entity_id :
- switch.sw1
- switch.sw2
- switch.sw3
to: ‘on’

etc.

You should be able to do:

trigger:
- platform : state
  entity_id : switch.sw1, switch.sw2, switch.sw3
  to: 'on'

Same with the action end of things, i.e.:

action:
- service: switch.turn_on
  entity_id : switch.sw1, switch.sw2, switch.sw3
1 Like