Turn on only if off

Can anyone please tell me what I need to add to the below automation to only run the script if the lights are already off.

Automation:

- alias: Sunset Living Room
  trigger:
    platform: sun
    event: sunset
    offset: '-00:30:00'
  action:
    - service: script.sunset_living_room

Or do I need to add something to the script instead?

Script:


sunset_living_room:
  alias: Living Room Sunset
  sequence:
    - service: light.turn_on
      data:
        entity_id: 
        - light.play_bar_1
        - light.play_bar_2
        - light.play_bar_3
        brightness: 80
        rgb_color:
        - 245
        - 107
        - 22
    - service: light.turn_on
      data:
        entity_id: 
        - light.tv_l
        - light.tv_r
        brightness: 12
        rgb_color:
        - 245
        - 81
        - 22
    - service: light.turn_off
      entity_id: light.lounge_light

This should only run if all the lights are off.

- alias: Sunset Living Room
  trigger:
    platform: sun
    event: sunset
    offset: '-00:30:00'
  condition:
    - condition: state
      entity_id:
        - light.play_bar_1
        - light.play_bar_2
        - light.play_bar_3
        - light.tv_l
        - light.tv_r
      state: 'off'
  action:
    - service: script.turn_on
      entity_id: script.sunset_living_room

Edit: I think your action needs to be changed as well.

Ok, I’ll try that, thanks Brian.

Regarding the action, I previously had all of them in the same format as you’ve suggested but someone mentioned in another thread that it can be simplified as in the way I have it currently. And it works, so less lines of code required.

Interesting, I’ve never tried it or seen it documented that way, but hey, if it works why not.

1 Like

There are differences in what happens when you run a script either of those two ways.

2 Likes

If you create the following Light Groups:

light:
  - platform: group
    name: play_bar
    entities:
      - light.play_bar_1
      - light.play_bar_2
      - light.play_bar_3
  - platform: group
    name: tv
    entities:
      - light.tv_l
      - light.tv_r
  - platform: group
    name: tv_play_bar
    entities:
      - light.play_bar
      - light.tv

then your script can be simplified like this:

sunset_living_room:
  alias: Living Room Sunset
  sequence:
    - service: light.turn_on
      data:
        entity_id: light.play_bar
        brightness: 80
        rgb_color:
        - 245
        - 107
        - 22
    - service: light.turn_on
      data:
        entity_id: light.tv
        brightness: 12
        rgb_color:
        - 245
        - 81
        - 22
    - service: light.turn_off
      entity_id: light.lounge_light

and your automation’s condition can simply be this (assuming you are using 0.115):

- alias: Sunset Living Room
  trigger:
    platform: sun
    event: sunset
    offset: '-00:30:00'
  condition: "{{ is_state('light.tv_play_bar', 'off') }}"
  action:
    - service: script.sunset_living_room
2 Likes

Okay, that’s interesting. I’ll have to have a read of that later for definite. Thanks Tom.

I’m yet to decide about how I want to group all my lights and other entities but that’s a good start so thank you.

Be advised that what I suggested uses the Light Group integration, not the Group integration. The Light Group integration effectively creates a virtual light that supports requests to change brightness, color, etc. The Group integration simply supports turning lights on/off (but supports other things like switches, locks, sensors, etc). Keep this distinction in mind when you begin to organize your lights.

1 Like

I will remember, thanks for the advice.