Help needed with automation configuration

I could really do with some help to get my automation working perfectly.
I’m using a this blueprint (Master switch to control other slave switches (when master turned off, then all slaves switches goes off as well. When any slave switch is "on" then master switch is on as well. For example one switch next to outdoor to turn off all light in the house when leaving a home, or one switch next the bed to turn off all lights when I go sleep. · GitHub)

and have changed the code to include extra triggers. Everything is working as I want except for 1 thing:

The master switch is turning off if any 1 of the slave switches turn off. I need to keep the master switch on if any of the slaves switches are on. I only want the master to turn off if slave1 and slave 2 and slave 3 and slave 4 and slave 5 turn off.

Could anyone help with what I’d need to change to get this working?

blueprint:
name: Master switch to off other switches
description: Master switch to control other slave switches (when master turned off,
then all slaves switches goes off as well. When any slave switch is “on” then
master switch is on as well. For example one switch next to outdoor to turn off
all light in the house when leaving a home, or one switch next the bed to turn
off all lights when I go sleep.
domain: automation
input:
switch_master:
name: Master Switch
selector:
entity:
domain:
- switch
multiple: false
switch_slave1:
name: Slave Switch 1
selector:
entity:
domain:
- switch
multiple: false
switch_slave2:
name: Slave Switch 2
selector:
entity:
domain:
- switch
multiple: false
switch_slave3:
name: Slave Switch 3
selector:
entity:
domain:
- switch
multiple: false
switch_slave4:
name: Slave Switch 4
selector:
entity:
domain:
- switch
multiple: false
switch_slave5:
name: Slave Switch 5
selector:
entity:
domain:
- switch
multiple: false
source_url: Master switch to control other slave switches (when master turned off, then all slaves switches goes off as well. When any slave switch is "on" then master switch is on as well. For example one switch next to outdoor to turn off all light in the house when leaving a home, or one switch next the bed to turn off all lights when I go sleep. · GitHub
variables:
switch_master: !input switch_master
switch_slave1: !input switch_slave1
switch_slave2: !input switch_slave2
switch_slave3: !input switch_slave3
switch_slave4: !input switch_slave4
switch_slave5: !input switch_slave5
switch_slaves:

  • !input switch_slave1
  • !input switch_slave2
  • !input switch_slave3
  • !input switch_slave4
  • !input switch_slave5

TRIGGERS

trigger:

  • platform: state
    entity_id:

    • !input switch_master
      from: ‘on’
      to: ‘off’
  • platform: state
    entity_id:

    • !input switch_slave1
    • !input switch_slave2
    • !input switch_slave3
    • !input switch_slave4
    • !input switch_slave5
      from: ‘off’
      to: ‘on’
  • platform: state
    entity_id:

    • !input switch_slave1
    • !input switch_slave2
    • !input switch_slave3
    • !input switch_slave4
    • !input switch_slave5
      from: ‘on’
      to: ‘off’

condition:
or:

  • condition: template
    value_template: ‘{{ states(switch_master) != states(switch_slave1) }}’
  • condition: template
    value_template: ‘{{ states(switch_master) != states(switch_slave2) }}’
  • condition: template
    value_template: ‘{{ states(switch_master) != states(switch_slave3) }}’
  • condition: template
    value_template: ‘{{ states(switch_master) != states(switch_slave4) }}’
  • condition: template
    value_template: ‘{{ states(switch_master) != states(switch_slave5) }}’

action:

  • service: homeassistant.turn_{{ trigger.to_state.state }}
    data:
    entity_id: ‘{% if trigger.from_state.entity_id != switch_master %} {{ switch_master
    }} {% else %} {{ switch_slaves }} {% endif %}’

please learn how to format your code. We cannot help without it being formatted. Check the FAQS. Point 11.

Thanks for the tip, sorry I missed that. I’ve read the FAQ but not sure what I need to do. Can I upload the code in .yaml or .txt file?

I’ll try to paste the text again using code blocks, but it seems to appear the same way.
Sorry, I know this is basic but I’d really like to share the code and I expect I’m missing something small

I’ts a backtick. `
not a quote ’
The characters you use matter. The backtick button on US keyboards shares the same key as the ~.

blueprint:
  name: Master switch to off other switches
  description: Master switch to control other slave switches (when master turned off,
    then all slaves switches goes off as well. When any slave switch is "on" then
    master switch is on as well. For example one switch next to outdoor to turn off
    all light in the house when leaving a home, or one switch next the bed to turn
    off all lights when I go sleep.
  domain: automation
  input:
    switch_master:
      name: Master Switch
      selector:
        entity:
          domain:
          - switch
          multiple: false
    switch_slave1:
      name: Slave Switch 1
      selector:
        entity:
          domain:
          - switch
          multiple: false
    switch_slave2:
      name: Slave Switch 2
      selector:
        entity:
          domain:
          - switch
          multiple: false
    switch_slave3:
      name: Slave Switch 3
      selector:
        entity:
          domain:
          - switch
          multiple: false
    switch_slave4:
      name: Slave Switch 4
      selector:
        entity:
          domain:
          - switch
          multiple: false
    switch_slave5:
      name: Slave Switch 5
      selector:
        entity:
          domain:
          - switch
          multiple: false
  source_url: https://gist.github.com/biesyl2/d852f5fd31856317f4500d101a219ca1#file-master_slaves_switches-yaml
variables:
  switch_master: !input switch_master
  switch_slave1: !input switch_slave1
  switch_slave2: !input switch_slave2
  switch_slave3: !input switch_slave3
  switch_slave4: !input switch_slave4
  switch_slave5: !input switch_slave5
  switch_slaves:
  - !input switch_slave1
  - !input switch_slave2
  - !input switch_slave3
  - !input switch_slave4
  - !input switch_slave5
  
## TRIGGERS ##
trigger:
- platform: state
  entity_id:
  - !input switch_master
  from: 'on'
  to: 'off'
  
- platform: state
  entity_id:
  - !input switch_slave1
  - !input switch_slave2
  - !input switch_slave3
  - !input switch_slave4
  - !input switch_slave5
  from: 'off'
  to: 'on'

- platform: state
  entity_id:
  - !input switch_slave1
  - !input switch_slave2
  - !input switch_slave3
  - !input switch_slave4
  - !input switch_slave5
  from: 'on'
  to: 'off'

condition:
  or:
  - condition: template
    value_template: '{{ states(switch_master) != states(switch_slave1) }}'
  - condition: template
    value_template: '{{ states(switch_master) != states(switch_slave2) }}'
  - condition: template
    value_template: '{{ states(switch_master) != states(switch_slave3) }}'
  - condition: template
    value_template: '{{ states(switch_master) != states(switch_slave4) }}'
  - condition: template
    value_template: '{{ states(switch_master) != states(switch_slave5) }}'

action:
- service: homeassistant.turn_{{ trigger.to_state.state }}
  data:
    entity_id: '{% if trigger.from_state.entity_id != switch_master %} {{ switch_master
      }} {% else %} {{ switch_slaves }} {% endif %}'

Thanks Petro, think I have that now
Code uploaded if anyone has a few moments to see what I’m missing. I’d really appreciate it

1 Like

This will do what you want with any number of slave switches.

blueprint:
  name: Master switch to off other switches
  description: Master switch to control other slave switches (when master turned off,
    then all slaves switches goes off as well. When any slave switch is "on" then
    master switch is on as well. For example one switch next to outdoor to turn off
    all light in the house when leaving a home, or one switch next the bed to turn
    off all lights when I go sleep.
  domain: automation
  input:
    switch_master:
      name: Master Switch
      selector:
        entity:
          domain:
          - switch
          multiple: false
    switch_slaves:
      name: Slave Switches
      selector:
        entity:
          domain:
          - switch
          multiple: true
          
variables:
  switch_master: !input switch_master
  switch_slaves: !input switch_slaves
  slaves_on: >
    {{ switch_slaves | select('is_state', 'on') | list | count > 0 }}
  master_on: >
    {{ is_state(switch_master, 'on') }}
  
## TRIGGERS ##
trigger:
- id: master
  platform: state
  entity_id: !input switch_master
  from: 'on'
  to: 'off'
  
- id: slave
  platform: state
  entity_id: !input switch_slaves
  from: 'off'
  to: 'on'

- id: slave
  platform: state
  entity_id: !input switch_slaves
  from: 'on'
  to: 'off'

action:
- if: "{{ trigger.id == 'master' and slaves_on }}"
  then:
  - service: switch.turn_off
    target:
      entity_id: !input switch_slaves
  else:
  - if: >
      {{ trigger.id == 'slave' and ((slaves_on and not master_on) or (not slaves_on and master_on)) }}
    then:
    - service: switch.turn_{{ 'on' if slaves_on else 'off' }}
      target:
        entity_id: !input switch_master

EDIT: Disclaimer, it may not work with 1 slave switch, minor alteration for that…

blueprint:
  name: Master switch to off other switches
  description: Master switch to control other slave switches (when master turned off,
    then all slaves switches goes off as well. When any slave switch is "on" then
    master switch is on as well. For example one switch next to outdoor to turn off
    all light in the house when leaving a home, or one switch next the bed to turn
    off all lights when I go sleep.
  domain: automation
  input:
    switch_master:
      name: Master Switch
      selector:
        entity:
          domain:
          - switch
          multiple: false
    input_slaves:
      name: Slave Switches
      selector:
        entity:
          domain:
          - switch
          multiple: true
          
variables:
  switch_master: !input switch_master
  input_slaves: !input switch_slaves
  slave_switches: >
    {{ [ input_slaves ] if input_slaves is string else input_slaves }}
  slaves_on: >
    {{ switch_slaves | select('is_state', 'on') | list | count > 0 }}
  master_on: >
    {{ is_state(switch_master, 'on') }}
  
## TRIGGERS ##
trigger:
- id: master
  platform: state
  entity_id: !input switch_master
  from: 'on'
  to: 'off'
  
- id: slave
  platform: state
  entity_id: !input switch_slaves
  from: 'off'
  to: 'on'

- id: slave
  platform: state
  entity_id: !input switch_slaves
  from: 'on'
  to: 'off'

action:
- if: "{{ trigger.id == 'master' and slaves_on }}"
  then:
  - service: switch.turn_off
    target:
      entity_id: !input switch_slaves
  else:
  - if: >
      {{ trigger.id == 'slave' and ((slaves_on and not master_on) or (not slaves_on and master_on)) }}
    then:
    - service: switch.turn_{{ 'on' if slaves_on else 'off' }}
      target:
        entity_id: !input switch_master

Thanks so much Petro, that works perfectly.