Sun state: below_horizon - how to deal with offset?

Hi,

when using a sun trigger with an offset - is there a way to deal with this offset if there’s also a condition using the sun?

For example, I have this automation that turns on some fancy lights when

  • I’m watching a movie and the sun sets OR

  • it’s already dark and I start watching a movie
    So I have two triggers and some conditions to keep them in check:

    alias: ‘Movie Lights After Sunset’
    trigger:
    - platform: sun
    event: sunset
    offset: ‘-00:15:00’
    - platform: state
    entity_id: media_player.kodi_wetek
    from: ‘off’
    condition:
    - condition: and
    conditions:
    - condition: state
    entity_id: sun.sun
    state: below_horizon
    - condition: or
    conditions:
    - condition: state
    entity_id: media_player.kodi_wetek
    state: ‘playing’
    - condition: state
    entity_id: media_player.kodi_wetek
    state: ‘idle’
    - condition: state
    entity_id: media_player.kodi_wetek
    state: ‘paused’
    action:
    service: scene.turn_on
    entity_id: scene.wz_watching_movie

I had the media player running before sunset today, so at 15 minutes before sunset, the lights should have turned on. Only they didn’t.
Looking at the automation, it’s probably because when the sunset trigger fired 15 minutes before the actual event, but the sun’s state was still above_horizon.
So this automation would only work when there is no offset for the sun trigger.

I could imagine that it would work using a template as the condition and then subtracting the offset from (or adding to) the next_setting attribute.
This sounds like a pain, though.

Does anyone have a better idea?

TIA,
Sebastian

How about breaking that into two different automations? Rather than one with two triggers? Could that make it more simple… I really don’t understand what you’re trying to accomplish exactly, so it might not work. In the past though I’ve been hung up with trying to make one automation policy that fits everything that I want when making two ends up being easier.

I’d also suggest to split this automation into two separate ones with a single trigger each. I’ve just done that with an automation combining motion and sunset, i.e. trigger when there’s motion after sunset, and trigger when the sun sets with ongoing motion detected.
Also, you can change the condition as proposed in the manual to allow for an offset:

condition: sun
after: sunset
# Optional offset value
after_offset: "-00:15:00"
1 Like

You’re both right in that it would be easier to split this up into two separate automations.
I just don’t really like to have multiple automations for basically the same thing (“turn on the lights when it’s dark and I’m watching a movie”).
That being said, expressing the condition with after: sunset and after_offset might just be the solution I was looking for.
Thanks!

Sebastian

I’m also (overly…) particular, and recently faced the same dilemma. I resolved it by using a template trigger. My scenario was that I want the interior lights to turn off when I get home after dark, or, if I’m home when it gets dark. So here’s my automation trigger:

automation 10:
  alias: Evening Scene On
  hide_entity: True
  trigger:
	platform: template
	value_template: "{% if is_state('group.all_devices', 'home') and is_state('sun.sun', 'below_horizon') %}true{% endif %}" 

Now, the value template is evaluated any time there is a state change on one of those entities (or any entity?), and triggers as soon as this statement is true: if device tracker is home, and the sun is below the horizon.

It works a treat, and doesn’t require two automations or excess conditions!

I did find that the house was already a bit dark by sunset, so I modified the value_template to actually render true once the sun was less than six degrees above the horizon. I’m not sure how I’d use a time offset. I suppose I’d use the next_setting state attribute of the sun, but that sounds like more work. Also, hopefully, by using the sun angle I’ll get more consistent results throughout the year, but I’m not sure. :man_shrugging:

New trigger:

trigger:
	platform: template
	value_template: "{% if is_state('group.all_devices', 'home') and (states.sun.sun.attributes.elevation) | float < 6 %}true{% endif %}" 

P.S. I feel slightly guilty about bumping such an old thread, but I had this exact same issue and then eventually resolved it to my satisfaction, and I don’t think that any of the pertinent facts (e.g. version dependence or new features) have changed since this was posted.

5 Likes