Trigger between Sunset and Sunrise

Yes, thats included as well as the lat/lon for my location. I just changed the sun condition to a luminance (which I get from the same sensor) condition. That seemed to work.

I will try the offset as you suggested. Who knows. maybe other factors prevented the light from coming on last night :frowning:

Try setting just one condition to the sun platform for every event.

automation 13:
  trigger:
    platform: state
    entity_id: binary_sensor.aeotec_multisensor_6_sensor_4
    state: 'on'
  condition:
    platform: sun
    after: sunset
  action:
    service: homeassistant.turn_on
    entity_id: switch.foyer_outlet_switch_7
automation 14:
  trigger:
    platform: state
    entity_id: binary_sensor.aeotec_multisensor_6_sensor_4
    state: 'on'
  condition:
    platform: sun
    before: sunrise
  action:
    service: homeassistant.turn_on
    entity_id: switch.foyer_outlet_switch_7

Also, double check your indentation, as YAML is creepy with the indentation :sweat_smile:

1 Like

That did the trick…

1 Like

Yo can also try:

condition_type: or
condition:
- condition: sun
after: sunset
- condition: sun
before: sunrise

The problem I guess, is because between sunset and sunrise there is a day change.

2 Likes

To test automations without waiting for sunset you could add an ‘or’ condition to your automation, for example…

- condition: state
  entity_id: input_boolean.after_sunset
  state: 'on'

Then, set up an input_boolean.yaml…

  after_sunset:
    name: 'After Sunset'
    initial: off

Then, if it is before sunset as long as you turn this input boolean switch to ‘on’, the automation will still trigger for you to test.

1 Like

Sorry for being a thread necromancer, but I figured out a solution for HASS 0.44.1 and wanted to post it here in case anyone else was wondering in the future:

I got an automation working for my own purposes with the following OR statement:

# Turn on porch light when I arrive if it's dark outside
  - alias: 'Lit porch on arrival'
    trigger:
      - platform: state
        entity_id: device_tracker.xxxxxxxxxxxxx
        state: 'home'
    condition:
      #You can test your automations before and after sunrise by setting your lat/long under "homeassistant:" for an area that is about to have sunrise or sunset (civil twilight). Go to https://in-the-sky.org to learn of an area about to undergo civil twilight, place a marker on the map at this link ( https://in-the-sky.org/location.php? ), then read the lat/long coordinates.  Add the coordinates to homeassitant: , then restart HomeAssistant. Clicking on the Sun badge in HomeAssistant will tell you how long until "sunrise" or "sunset."  Test your automations now.
      #OR statement looks strange but is correct https://home-assistant.io/docs/scripts/conditions/#or-condition
      #Using "before: sunset" and "after: sunrise" causes light to come on when arriving during the day.
      #"after_offset:" appears to have to be used with "after:" and "before_offset:" appears to have to be used with "before:" since the trigger doesn't fire correctly using "after: ... before_offset: ..." or "before: ... after_offset: ..." in my experience. Positive or negative offset values only seem to matter on the after_offset (but produce the same outcomes for the before_offset).
      condition: or
      conditions:
        #Condition beginning 30 min before sunset (and including after sunset): https://home-assistant.io/docs/scripts/conditions/#sun-condition
        - condition: sun
          after: sunset
          after_offset: "-0:30:00"
        #Condition ending 30 min after sunrise (and including before sunrise)
        - condition: sun
          before: sunrise
          before_offset: "-0:30:00"
    action:
      service: switch.turn_on
      entity_id: switch.porch_light

Edits: I attempted to fine-tune this condition, but ended up using the code I posted originally anyway. I have, however, annotated the things I learned about the sun condition in the code’s comments.


Bonus info about testing automations / simulating sunrise/sunset:

  • Go to https://in-the-sky.org to learn of an area that has just undergone daytime, place a marker on the map (at this link https://in-the-sky.org/location.php? ), then read the lat/long coordinates.
  • Add those coordinates to your configration.yaml 's homeassitant: area, then restart HomeAssistant (Developer Tools > Services > homeassistant , restart > Call Service).
  • Clicking on the Sun badge in HomeAssistant will tell you how long until the next “sunrise” or “sunset.”
  • Pretend to be “home” or “not_home” by setting those states for your device tracker (Developer Tools > States > device_tracker.###### > Set State). HASS detects the change in state, so simulate coming home by switching “home” to “not_home”, then switch back to “home”.
7 Likes

or you could create a solar angle sensor. this in the configuratio.yaml

  - platform: template
    sensors:
      solar_angle:
        value_template: '{{ "%+.1f"|format(states.sun.sun.attributes.elevation) }}'
        friendly_name: 'Sun Angle'
        unit_of_measurement: '°'

then a condition of

condition:
  - condition: numeric_state
    entity_id: sensor.solar_angle
    below: -1.0

etc etc
this doesn’t reset at midnight like sunrise/sunset

3 Likes

Hi,

Here is how I solved that one:

  - alias: Turn ON Dany's lamp when movement
    trigger:
      platform: state
      entity_id: binary_sensor.motion_sensor_158d00015a8786
      to: 'on'
    condition:
      condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    action:
      service: homeassistant.turn_on
      entity_id: switch.dannys_night_light__jackson1

  - alias: Turn OFF Dany's lamp after last movement
    trigger:
      platform: state
      entity_id: binary_sensor.motion_sensor_158d00015a8786

      to: 'off'
      for:
        minutes: 15
    action:
      service: homeassistant.turn_off
      entity_id: switch.dannys_night_light__jackson1
5 Likes

brilliant!

This works perfectly. There seems to be a bug when using the built in automation editor when selecting (after sunset and before sunrise at the same time) I’m guessing that it’s using an AND to check between sunset and sunrise rather than an OR

2 Likes

Shouldn’t the before_offset not be a + value? In my understanding it s the sunrise time minus 30 minutes.
Please confirm.

You can make it with one autiomation like the below.

  • alias: Auto Dany’s lamp when movement
    trigger:
    • platform: state
      entity_id: binary_sensor.motion_sensor_158d00015a8786
      from: ‘off’
      to: ‘on’
    • platform: state
      entity_id: binary_sensor.motion_sensor_158d00015a8786
      from: ‘on’
      to: ‘off’
      for:
      minutes: 15
      condition:
      condition: state
      entity_id: sun.sun
      state: ‘below_horizon’
      action:
      service: switch.turn_{{ states.binary_sensor.motion_sensor_158d00015a8786.state }}
      entity_id: switch.dannys_night_light__jackson1

Hey, great question. This confused me too. I addressed this in another post as what I believe to be a bug

Hello there.

Im trying to get my lights to turn on at sunset and turn off at sunrise.
But the turn on condition i would like to get it to work with the hue motion/light sensor (lux). Is this going to work?

###################################################

LIGHTS - SUNSET

###################################################

- alias: Lights on at Sunset
  trigger:
    - platform: sun
      event: sunset
      offset: '+01:00:00'
  action:
    service: homeassistant.turn_on
    entity_id: switch.vitrinskap
    condition:
  - condition: numeric_state
    entity_id: sensor.hue_motion_sensor_1_light_level
    below: 5 
    - platform: sun
      event: sunrise
      offset: '-01:00:00'
  action:
    service: homeassistant.turn_off
    entity_id: switch.vitrinskap

Please correctly format your code

Corrected like you wished.

/Jerry

I think you’re missing a few lines in there, two automations have merged into one? And you might want to check some other examples from the forum as you seem to have indentations wrong. These are vitally important in YAML (eg the conditions in your example).

Take a look at my example below:

- alias: "Switch on kitchen lights at sunset if somone is home"
  trigger:
    - platform: sun
      event: sunset
      offset: "-00:30:00"
  condition: 
    condition: and
    conditions:
      - condition: state							#if smart lighting automations are enabled via the frontend
        entity_id: input_boolean.smart_lighting  
        state: 'on'
      - condition: state							#if someone is actually home
        entity_id: group.all_devices
        state: 'home'
  action: 
    - entity_id: light.piano_lamp
      service: homeassistant.turn_on
    - entity_id: light.kitchen_corner
      service: homeassistant.turn_on

Thanks, will take a look at this.

also, conditions are logic ‘AND’ by default so you don’t need to include that code

I include it more for my own benefit… I’m often checking my own work months later to figure out why a complex automation is doing something the way it is and this helps me out