Trigger between Sunset and Sunrise

I have a seemingly common requirement but cant seem to find any examples of what is needed in the config file. Basically, I have a motion sensor and some lights in my foyer. When motion is detected, turn on the lights. Turn off the lights when no motion for 5 minutes. Here is what I have (and working) so far:

automation 5:
 trigger:
    platform: state
    entity_id: binary_sensor.aeotec_multisensor_6_sensor_4
    state: 'on'
 action:
    service: homeassistant.turn_on
    entity_id: switch.leviton_unknown_type1d04_id0334_switch_6
    
  

automation 6:
 trigger:
    - platform: state
      entity_id: binary_sensor.aeotec_multisensor_6_sensor_4
      from: 'on'
      to: 'off'
      for:
        minutes: 5
 action:
    service: homeassistant.turn_off
    entity_id: switch.leviton_unknown_type1d04_id0334_switch_6

What is missing is the timeframe during which these lights should be turned on. The actions above should only occur between sunset and sunrise. What do I need to add to make the above work this way?

Think you need to add a condition, either a Time or Sun condition would probably work -> https://home-assistant.io/components/automation/#conditions

1 Like
condition:
    platform: sun
    after: sunset
    before: sunrise

Adding that should do it.

1 Like

Thank you all. That looks simple enough :slight_smile:

What am I doing wrong here?

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

This did not fire last night. The minute I remove the condition the light turns on when motion detected. Unfortunately, it turns on regards of the time.

I checked the Sun values and both the sunrise and sunset times are correct for my area. Is there something wrong in the way the condition is formatted??

BTW: Is there a way to simulate sunset/rise states? Hate to have to wait till tonight to see if what ever changes I make actually work

Have you tried to confirm your config syntax with -> http://www.yamllint.com/

Just looking at it here it doesn’t seemed to be aligned correctly and I know yaml is very picky. As far as testing you could try adding an offset for the number of hours until current time otherwise I am not aware of another way to simulate it.

Just checking but I also assume you have this in your config file:
'# Track the sun
sun:

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