Automation question abount conditional actions

Hi everybody,

I started not long time ago configuring my home assistant.
I really like it but I feel a little lost during selecting automations for the scene I generated.

Basically the big question for me is:
I would like to write an automation for controlling lights. actually my configuration is something like this:

- alias: 'Media player playing Light 1'
  trigger:
    - platform: state
      entity_id: media_player.kodi
      to: 'playing'
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    - condition: 
      after: '21:00:00'
      before: '19:00:00'
  action:
    service: scene.turn_on
    entity_id: scene.Movies_light1

- alias: 'Media player playing Light 2'
  trigger:
    - platform: state
      entity_id: media_player.kodi
      to: 'playing'
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
  action:
    service: scene.turn_on
    entity_id: scene.Movies_light2

As you can see the code is mostly the same, it is working but… well I don’t like to repeat myself.
The condition for light 1 basically is a “if I am having dinner while watching tv don’t turn off the light on the table”.

Ideally I would write something like:

- alias: 'Media player playing Light 1'
  trigger:
    - platform: state
      entity_id: media_player.kodi
      to: 'playing'
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
   
  action:
   - condition: 
       after: '21:00:00'
       before: '19:00:00'
       service: scene.turn_on
       entity_id: scene.Movies_light1
action:
  service: scene.turn_on
  entity_id: scene.Movies_light2

but I don’t think is something valid and I don’t see any example that do something like this.

can you confirm it is not possible?
or can you point me to some alternatives?

This should put you on the right track:

- alias: 'Media player playing Light'
  trigger:
    - platform: state
      entity_id: media_player.kodi
      to: 'playing'
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
  action:
    - service: scene.turn_on
      entity_id: scene.Movies_light2
    - condition: time
      after: '21:00:00'
      before: '19:00:00'
    - service: scene.turn_on
      entity_id: scene.Movies_light1

The idea is the first action will fire no matter what. The second action (and anything after it), however, will not if the condition is false.

I must ask though…does your time condition actually work? First of all, you left out the time keyword. And secondly, it can’t be after 21:00 and before 19:00 at the same time (as midnight is the reference point).

1 Like

At least I wasn’t aware, that conditions in actions could be used like that (doing one thing but not the other). Thanks for pointing it out. The documentation really should mention this.

It does, have a look at this page.

It does mention conditions, but the example makes it look like the action as a whole depends on the condition, not just the stuff following it. In your example you turn on the scene, then check the condition, and depending on that do more or not.

You do have a point there, so I just sent in a PR to add a bit of explanation on that page :slight_smile:

2 Likes

@fanaticDavid thank you for the example.

I will try it when I’ll get home.
Regarding the time constraints yes it works. When I checked it. It is installed from the last weekend so it is not really tested in all scenarios.
I found this solution here on the forum. I forgot the source thread and author, by the way thanks to him.

Just as concluding the discussion on the condition inserted in the actions… Someone is aware of some if then else construct that can be inserted…

Like every programming algorithm…
If a
Do something
Else if b
Do something else
Else
Do c
?