Turn LED on or OFF Based on the sunset / sunrise

Hi all,

I have a small problem with a automation rule, that doesn’t seem to work as intended. It is 100% me that doesn’t understand the concept of the sunset / sunrise, but heeeeelp :slight_smile:

So I have a LED strip that I want to turn on when my TV state if going from Off to On. That part seems to be simple, but I only want that to happen when it is dark outside. So I used the “condition: sun” and set the “after: to sunset”
The only problem is, that it will only work when the sunset is triggered and until 12:00pm / 00:00am.

Here is my automation:

# Living Room
# LED Light TV
- alias: Living Room LED Light TV Turn ON
  trigger:
    platform: state
    entity_id: media_player.samsung_tv_remote
    from: 'off'
    to: 'on'
  condition:
    condition: sun
    after: sunset
  action:
  - service: light.turn_on
    entity_id: light.led_tv_mbel

- alias: Living Room LED Light TV Turn OFF
  trigger:
    platform: state
    entity_id: media_player.samsung_tv_remote
    from: 'on'
    to: 'off'
  condition:
    condition: sun
    after: sunset
  action:
  - service: light.turn_off
    entity_id: light.led_tv_mbel

What do I need to change if I want the light to be triggered between the sunset and sunrise?

Try using this as your condition:

  condition:
    condition: or
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
1 Like

I would use sunset as a trigger.

Can you elaborate?

This will also work:

    condition:
      condition: state
      entity_id: sun.sun
      state: 'below_horizon'
1 Like

Arh yes - that make sense.
So as soon as the sun changes to sunrise, the state will be above_horizon!

Thanks… and also for the other suggestions - much appreciated!

Hey guys,

I just realized one thing.
The Turn on and off works as it should, but then another problem came across.

Lets say someone turns the tv on in the morning and then the sun comes above the horizen. When the TV is turned off, the LED is still on and I will have to manually turn it off.
Would it be possible to do a double condition somehow?

- alias: Living Room LED Light TV Turn OFF
  trigger:
    platform: state
    entity_id: media_player.samsung_tv_remote
    from: 'on'
    to: 'off'
  condition:
    condition: sun
    after: sunset
  action:
  - service: light.turn_off
    entity_id: light.led_tv_mbel

You could just write another automation to turn the LEDs off at sunrise (if they’re on). Then that wouldn’t happen. It would also tell you exactly when the sun comes up if you’re somehow wrapped in morning telly with the blinds closed or lots of low cloud.

So something lke this should do it right?

- alias: Living Room LED Light TV Turn OFF at Sunrise
  trigger:
    platform: state
    entity_id: sun.sun
    event: sunrise
  action:
  - service: light.turn_off
    entity_id: light.led_tv_mbel

Yep, the only addition you could include would be a condition to check if the lights are actually on before executing the turn off action.

Exactly! Unfortunately I’m facing another issue with my Samsung TV (media player) it doesn’t get updated, so the other rules don’t work at the moment, but I already added that question in another thread :slight_smile:

Thanks again…