Switch on Light if TV is running

Hi, i have a question about automation of my light behind TV.

  1. I want my backlite to be on if TV is runnning and sun is behind horizont.
    2 I want to go auto off if i switch off my TV

This sounds simple but got complicated for me when i have tried to put it in 1 or 2 automations.

alias: TV Licht auto ON
description: Turn on TV Backlite if TV on and sun goes down
trigger:
  - platform: sun
    event: sunset
    offset: 0
  - platform: homeassistant
    event: start
condition:
  - condition: state
    entity_id: media_player.samsung_6_series_75
    state: 'on'
action:
  - service: light.turn_on
    target:
      entity_id: light.light_led_stripe_tv
    data: {}
mode: single

my second automation is

alias: TV Licht auto ON  (with TV)
description: Turn on TV Backlite if TV gets on ans sun is down
trigger:
  - platform: device
    device_id: 9e601bac89a4e1724278d420223da9f2
    domain: media_player
    entity_id: media_player.samsung_6_series_75
    type: turned_on
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizont
action:
  - service: light.turn_on
    target:
      entity_id: light.light_led_stripe_tv
    data: {}
mode: single

if i put the condition from second automation to first i get problems with the sunset trigger…

alias: TV Licht auto ON (Test)
description: Turn on TV Backlite if TV on and sun goes down
trigger:
  - platform: sun
    event: sunset
    offset: 0
  - platform: homeassistant
    event: start
  - platform: device
    device_id: 9e601bac89a4e1724278d420223da9f2
    domain: media_player
    entity_id: media_player.samsung_6_series_75
    type: turned_on
    for:
      hours: 0
      minutes: 0
      seconds: 2
condition:
  - condition: state
    entity_id: media_player.samsung_6_series_75
    state: 'on'
  - condition: sun
    before: sunrise
    after: sunset
    after_offset: '-15'
action:
  - service: light.turn_on
    target:
      entity_id: light.light_led_stripe_tv
    data: {}
mode: single

the off automation is simple i just switch off if TV goes off.

Is there a way to put it in one automation?

Because the condition contains:

after_offset: '-15'

which is 15 minutes after sunset but the trigger occurs at sunset.

This option doesn’t appear in your first two automations, only in the third automation that attempts to combine the other two automations. Why did you add after_offset?

alias: TV Licht auto ON
description: Control TV Backlite
trigger:
  - platform: sun
    event: sunset
    offset: 0
  - platform: homeassistant
    event: start
  - platform: device
    device_id: 9e601bac89a4e1724278d420223da9f2
    domain: media_player
    entity_id: media_player.samsung_6_series_75
    type: turned_on
condition:
  - condition: state
    entity_id: media_player.samsung_6_series_75
    state: 'on'
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - service: light.turn_on
    target:
      entity_id: light.light_led_stripe_tv
    data: {}
mode: single

Thanks for this I was trying to get my sub woofer to power on when the Denon amp is switched on and Vis versa when it’s switched off…

taking this code as a basis it now works. I just set up two scripts one for on and the other for off

Great stuff

You can do this in a single automation with ‘choose’, see

Here is one that operates all lights and multiple motion sensors. (A large room)

- id: 'Bar Lights On and Off based on motion and switch'
  alias: Bar Lights - Turn on and off based on stairwell light.
  description: Turn on and off all the lights in the bar area
  trigger:
    - id: 'bar_on'
      platform: state
      entity_id: binary_sensor.bar_stairwell_motion_sensor_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: binary_sensor.shellymotionsensor_60a42395fc6a_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: binary_sensor.bar_main_motion_sensor_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: binary_sensor.bar_ne_corner_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: switch.stairwell_light
      from: 'off'
      to: 'on'
    - id: 'bar_off1'
      platform: state
      entity_id: binary_sensor.bar_ne_corner_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'
    - id: 'bar_off1'
      platform: state
      entity_id: binary_sensor.bar_main_motion_sensor_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'
    - id: 'bar_off'
      platform: state
      entity_id: switch.stairwell_light
      from: 'on'
      to: 'off'
    - id: 'bar_off1'
      platform: state
      entity_id: binary_sensor.bar_stairwell_motion_sensor_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'
    - id: 'bar_off1'
      platform: state
      entity_id: binary_sensor.shellymotionsensor_60a42395fc6a_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'  
    - id: 'bar_off1'
      platform: state
      entity_id: binary_sensor.bar_ne_corner_motion
      from: 'on'
      to: 'off'
      for: '00:01:30' 
    - id: 'bar_off1'
      platform: state
      entity_id: binary_sensor.bar_main_motion_sensor_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'
  condition: []
  action:
  - choose:
    - conditions:
        - "{{ trigger.id == 'bar_on' }}"
      sequence:
        - service: switch.turn_on
          target:
            entity_id:
            - switch.stairwell_light
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.area_cans
        - service: light.turn_on
          target:
            entity_id:
            - light.bar_pendant_light
            - light.bar_back_bar_cans
          data:
            brightness_pct: 50
    - conditions:
        - "{{ trigger.id == 'bar_off' }}"
      sequence:
        - service: switch.turn_off
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_off
          target:
            entity_id:
            - light.bar_back_bar_cans
            - light.bar_pendant_light
    - conditions:
        - "{{ trigger.id == 'bar_off1' }}"
        - "{{ states('binary_sensor.bar_main_motion_sensor_motion') == 'off' }}"
        - "{{ states('binary_sensor.shellymotionsensor_60a42395fc6a_motion') == 'off' }}"
        - "{{ states('binary_sensor.bar_ne_corner_motion') == 'off' }}" 
        - "{{ states('binary_sensor.bar_stairwell_motion_sensor_motion') == 'off' }}"
      sequence:
        - service: switch.turn_off
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_off
          target:
            entity_id:
            - light.bar_back_bar_cans
            - light.bar_pendant_light
    default: []
  mode: queued

I did this after the post and adding motion sensors.