Automation with If statments

Hi All

I need some help setting up an automation which will include an if statement (i don’t know how to write them)

My automation currently is set to turn the living room lights on to a preset scene 15 mins before sunset time only if the tv is on and the living lights are already off. (See below automation yaml)

alias: TV Lights on when Sun Sets and TV is on
description: ''
trigger:
  - platform: sun
    event: sunset
    offset: '- 00:15:00'
condition:
  - condition: state
    entity_id: media_player.living_room_tv
    state: 'on'
  - condition: device
    type: is_off
    device_id: 53e92772311d7655d84a2bd4f38c9a27
    entity_id: light.floor_lamp_1
    domain: light
action:
  - scene: scene.evening
mode: single

What i want to do is change the scene depending on weather the Plex is playing or not.

if Plex is playing then scene.movie_mode if not then scene.evening_mode

Hope this makes sense.

You can use the choose type action. Please try the following.

alias: TV Lights on when Sun Sets and TV is on
description: ''
mode: single
trigger:
  - platform: sun
    event: sunset
    offset: '- 00:15:00'
condition:
  - condition: state
    entity_id: media_player.living_room_tv
    state: 'on'
  - condition: device
    type: is_off
    device_id: 53e92772311d7655d84a2bd4f38c9a27
    entity_id: light.floor_lamp_1
    domain: light
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: media_player.plex
            state: playing
        sequence:
          - service: scene.apply
            data:
              entities: scene.movie_mode
      - conditions:
          - condition: state
            entity_id: media_player.plex
            state: idle
        sequence:
          - service: scene.apply
            data:
              entities: scene.evening_mode
    default: []

Please make sure that the media player entity_id matches your plex player and also the states.

1 Like

Excellent… i never even considered “Choose” this will save me creating so many automations - i’ll see tonight if it works :slight_smile:

Thanks