Basic on / off switch in 1 automation

Am after a basic automation that when on changes selected lights to 100% then when switched off turns them to 30%.

This is what i have ATM, it switches them all to 100% but when switched off nothing happens?

alias: 1 - SHM - Ambient 100%
description: ''
trigger:
  - platform: state
    entity_id: switch.living_room_st_virtual
    to: 'on'
condition: []
action:
  - service: light.turn_on
    entity_id: light.hue_go
    data_template:
      brightness_pct: 100
  - service: light.turn_on
    entity_id: light.hue_play_left
    data_template:
      brightness_pct: 100
  - service: light.turn_on
    entity_id: light.hue_play_right
    data_template:
      brightness_pct: 100
  - service: light.turn_on
    entity_id: light.hue_play_centre
    data_template:
      brightness_pct: 100
  - wait_for_trigger:
      - platform: state
        entity_id: switch.living_room_st_virtual
        to: 'off'
  - service: light.turn_off
    entity_id: light.hue_go
    data_template:
      brightness_pct: 30
  - service: light.turn_off
    entity_id: light.hue_play_left
    data_template:
      brightness_pct: 30
  - service: light.turn_off
    entity_id: light.hue_play_right
    data_template:
      brightness_pct: 30
  - service: light.turn_off
    entity_id: light.hue_play_centre
    data_template:
      brightness_pct: 30
mode: single

alias: 1 - SHM - Ambient 100%
description: ''
trigger:
  - platform: state
    entity_id: switch.living_room_st_virtual
    to:
action:
  - service: light.turn_on
    target:
      entity_id: 
      - light.hue_go
      - light.hue_play_left
      - light.hue_play_right
      - light.hue_play_centre
    data:
      brightness: "{{ 30 if trigger.to_state.state == 'off' else 100 }}"
mode: single
1 Like

thank you, is this the best way for a simple if then else

There are other ways and they are all equally valid. e.g.

Using the immediate-if is slightly more compact but harder to read at a glance:

    data:
      brightness: "{{ iif( trigger.to_state.state == 'off', 30, 100 ) }}"

Multi-line templates are easy to read but a bit verbose:

    data:
      brightness: >
        {% if trigger.to_state.state == 'off' %}
          30
        {% else %}
          100
        {% endif %}
1 Like