Lights on only if TV is on at Sunset

 - alias: 'Light on if Tv is on at Sunset'
trigger:
  - platform: sun
    event: sunset
    offset: '-01:00:00'

condition:
  condition: and
  conditions:
  - condition: state
    entity_id: sun.sun 
    state: 'below_horizon'
  - condition: state
    entity_id: media_player.living_room_un65ks9500
    state: 'on'
  - condition: time
    after: '17:00:00'
    before: '20:00:00'

action:
    service: light.turn_on
    entity_id: group.all_lights

I am not sure what I am doing wrong but nothing happens

Looks to me like it won’t be triggered because the condition is sunset but the trigger is one hour before sunset.

1 Like

I would like it to trigger about an hour prior to sunset. What would I need to change to make it work?

remove the sunset condition - it’s superfluous.

2 Likes

I got also some problems with this kind of automation, lights should turn on when turning on TV after sunset AND if the TV is already on at sunset, but it’s not working for me, what am I doing wrong? Or do I’ve to set up two different automations for this?

alias: 'Wohnzimmer: LED an wenn TV an'
  trigger:
  - event: sunset
    platform: sun
  condition:
  - condition: state
    entity_id: media_player.vu_zero4k
    state: 'on'
  - after: sunset
    condition: sun
  action:
  - data:
      entity_id:
      - light.yeelight_strip1_7811dce2c50d
      - light.yeelight_strip1_7811dcfb701f
      - light.lowboard
    service: light.turn_on

Here is one of mine. I would recommend that you use the sun elevation, rather than a time before sunset. The suns angle changes through the year, so I find it works better through the seasons.

# SUNSET - DRIVEWAY & DOWNSTAIRS #
- alias: Sunset - Turn on Driveway & Downstairs
  initial_state: true
  hide_entity: true
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    below: 8.0
  action:
    service: homeassistant.turn_on
    entity_id:
      - switch.driveway
      - light.downstairs

This one is a little more involved, checking if we are home, and running different scripts based on who is home.

# SUNSET - WHEN HOME #
- alias: Sunset - When Home
  initial_state: true
  hide_entity: true
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    below: 10
  condition:
    condition: state
    entity_id: group.family
    state: 'home'
  action:
    - service: homeassistant.turn_on
      entity_id:
        - light.hall
        - script.kitchen_normal
    - service: script.turn_on
      data_template:
        entity_id: >
          {% if is_state('device_tracker.jason', 'home') %}
            script.blue_lights
          {% elif is_state('device_tracker.kym', 'home') %}
            script.pink_lights
          {% elif is_state('device_tracker.carolyn', 'home') %}
            script.white_lights
          {% endif %}

@Matt112 This should work for you, depending on your location, adjust the sun angle higher or lower to get it close to 1 hr before Sunset.

add sun: to your configuration.yaml file.

- alias: 'Light on if Tv is on at Sunset'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    below: 10.0
  condition:
    condition: state
    entity_id: media_player.living_room_un65ks9500
    state: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: group.all_lights