Sunset/Sunrise issues

Hey Everyone,
I’m having issues with the fallow Automation. It will work if I tell it to run between specific times but as night comes at different times id like the automation to auto adjust during the year but when i put in the sunset and sunrise options it just stops working all together.

 - alias: Bedroom Nightlight ON
      trigger:
    platform: state
    entity_id: binary_sensor.bedroom_motion_
    from: 'off'
    to: 'on'
      condition:
    condition: and
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
      - condition: state
        entity_id: device_tracker.owntracks_iphone
        state: home 
      action:
    - service: homeassistant.turn_on
      entity_id: light.bedside_left

I had a similar issue as you, and changed to using the Sun elevation instead of Sunrise/Sunset.

Use the elevation value of the Sun as a trigger;

# SUNSET - WHEN HOME #
- alias: Sunset - When Home
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    below: 8.0
  condition:
    condition: state
    entity_id: group.family
    state: 'home'
  action:
    service: homeassistant.turn_on
    entity_id:
      - switch.fish_tank
      - light.hall

or use it as a condition;

# LEAVING HOME - CHANGE THE STEPS & TABLE #
- alias: Leaving Home - Change the Steps & Table
  trigger:
    platform: state
    entity_id: group.family
    from: 'home'
    to: 'not_home'
    for:
      seconds: 65
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: group.family
        state: 'not_home'
      - condition: template
        value_template: "{{ states.sun.sun.attributes.elevation  | int < 8 }}"
action:
  service: scene.turn_on
  entity_id: scene.white_lights

You can’t have sunrise AND sunset you can have OR but not ‘and’ for obvious reasons.

You have to OR the conditions. Here’s a complete working example from my personal setup.

- alias: 'arrival: turn on aloha lights and greatroom when needed'
  trigger:
    platform: state
    entity_id: group.aloha_mode
    from: 'off'
    to: 'on' # any member is on
  condition:
    condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: "-00:15:00" # near or after sunset
      - condition: sun
        before: sunrise
  action:
    - service: light.turn_on
      data:
        entity_id: group.alohalights
        brightness: 127
    - service: light.turn_on
      data:
        entity_id: light.greatroom_lights_level
        brightness: 25
1 Like

After sunset and before sunrise are the same condition.

It see that it’s documented that way, but it didn’t seem to work that way. I had a single incident around 1:30 AM when this automation should have triggered but did not. It’s possible that there was some other reason why it didn’t trigger, and I haven’t had an opportunity to re-test after making the condition change. I should probably do some non-disruptive automatic testing to see if the added condition does anything in practice, despite what the documentation says.

Yes, the “after sunset” and “before sunrise” conditions will work as expected only before midnight. Because the sunset will change to the new day’s sunset after midnight so that, even before sunrise, you’re also before sunset (of the next day)…

I ended using the sun elevation, which is nicer because it’s not only the time sun sets or rises, but also the light there is in the house that you can use as a condition.

1 Like

Please raise a bug if it’s not working as expected.

Bug filed.

FYI, according to a project contributor commenting in github’s issue tracker, post-sunset and pre-sunrise are not the same. I’ll submit a documentation change.

1 Like

The documentation describes them as different only in the way the offsets are assigned.