Example to condition with offset before sunset and after sunrise

Can anyone provide me with an example using both sunrise and sunset as a condition and both with offsets? In the end I have given up and used:

 - alias: Movement Lounge on Light
   trigger:
     - platform: state
       entity_id: binary_sensor.motion_sensor_158d00013fb2d7
       to: 'on'
   condition:
     - condition: numeric_state
       entity_id: sun.sun
       value_template: '{{ state.attributes.elevation }}'
       below: -2
   action:
     - service: homeassistant.turn_on
       entity_id: script.timed_lamp

This may be a simpler way but I am still interested in how to do it with sunrise and sunset.

Here’s mine.

Take a peek through my Repo for more. Be sure to star it since I update often.

There is an offset and also a random delay to give randomness.

1 Like

Just realized you asked for a condition. I usually use above_horizon or below_horizon as a condition.

Thanks I think I am over complicating things and above or below horizon assuming you can use them with an offset or elevation is the way to go.

Just thinking outside the box, worst case you cAn always use input booleans.

Automation that triggers at sunrise/sunset with the offset and then sets an input_boolean.

Then use the input Boolean as a condition for your other automations.

Then at midnight, reset the booleans.

Hey @kiwinol

Here is my working solution:

- alias: Turn on kitchen light when movement
  trigger:
    platform: state
    entity_id: binary_sensor.pir_hall
    to: 'on'
  condition:
  - condition: state
    entity_id: light.kitchen
    state: 'off'
  - condition: or
    conditions:
    - condition: sun
      after: sunset
      after_offset: "-1:00:00"
    - condition: sun
      before: sunrise
      before_offset: "1:00:00"
  action:
  - service: light.turn_on
    data:
      entity_id: light.kitchen
      rgb_color: [255,255,255]
10 Likes