Sunset not working as condition

Hi All,
I have been tinkering with this for allmost 3 weeks now and i think it is time to ask for help…
I have been trying to turn on lights but only when it is after sunset and before sunrise.
Below is my automation.

  alias: Verlichting woonkamer inschakelen
  trigger:
  - entity_id: binary_sensor.motion_sensor_158d00015a7998
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: light.sfeerlamp_woonkamer
    state: 'off'
  - condition: state
    entity_id: light.sfeerlamp_woonkamer_2
    state: 'off'
  - condition: sun
    before: sunrise
  - condition: sun
    after: sunset
  action:
  - data:
      entity_id: light.sfeerlamp_woonkamer_2
    service: light.turn_on
  - delay: 00:00:01
  - data:
      entity_id: light.sfeerlamp_woonkamer
    service: light.turn_on
  - data:
      message: HA test message
      title: Motion detected by Xiaomi motion sensor.
    service: notify.home_assistant

I also tried doing this via the UI.

  alias: Sunset test
  trigger:
  - at: '23:00'
    platform: time
  condition: 
  - condition: sun
    after: sunset
  - condition: sun
    before: sunrise
  action:
  - data:
      message: 'Het is nu 23:00, de zon is onder en de mail wordt goed verzonden. '
      title: Sunset test script.
    service: notify.home_assistant

When testing this automation the action runs as expected. When removing the sunset/sunrise stuff the automation runs but in some way the automation does not run when adding the sunset and sunrise stuff…

Could somebody point out to me what i am doing wrong?

Geert

Have a look at the boolean tod sensor, it’s much simpler for things like this.

  - platform: tod
    name: Lights On
    after: sunset
    after_offset: '00:15'
    before: sunrise
    before_offset: '-00:15'
1 Like

Why not use state for sun.sun or for a more granular control the numeric_state.

- condition: state
  entity_id: sun.sun
  state: below_horizon
  - condition: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    below: 2.0
3 Likes

have a look at the sun condition docs https://www.home-assistant.io/docs/scripts/conditions/#sun-state-condition

it says to use

condition:
    condition: state  # from sunset until sunrise
    entity_id: sun.sun
    state: 'below_horizon'

for your wishes.

and change the spacing there to:

condition:
  condition: state  # from sunset until sunrise
  entity_id: sun.sun
  state: 'below_horizon'

Thank you all! Will try the solution Calle and Marius offered. (below horizon state of sun.sun) If i am correct i cannot use any offset there, so i might need to go to the numeric_state function Calle suggested of the TOD sensor Patrick suggested.
Hope it works tonight :slight_smile:

Thank you all!

I think it would have worked using your original conditions but only if you understand how the sunset/sunrise events are processed.

“after sunset” is any time between sunset and the following midnight. So if sunset is at 6 pm the “after: sunset” condition is true for any time between 6pm and midnight.

Likewise, “before sunrise” is any time between the previous midnight and sunrise.

By default all conditions are ‘and’. so in your original automation you are requiring that the time be both between sunset and midnight, and also between midnight and sunrise simultaneously. I think you should now see why it never worked.

to get it to work correctly you needed to make those two conditions as ‘or’. So then it would have worked if the time was either between sunset and midnight or between midnight and sunrise. Effectively making the condition being between sunset and the next sunrise which is what you ultimately wanted.

2 Likes

Could you tel me how to make the two conditions to “or”?

Isnt that what the condition {{is_state('sun.sun','below_horizon')}} checks? No reason to complicate things… :wink:

It’s explained in the documentation: OR conditions

You’ll need to add this:

condition:
  condition: or
  conditions:

before your existing conditions and then you must indent all your existing conditions.

Yes, but I took the opportunity for a teaching moment.

More information is never a bad thing.

what if the check wasn’t for the state of the sun? what if the check was for a certain time - like say between 11pm & 5am? then the same limitations hold true and you need the two checks because you can’t do that with just one.

And it gave the OP the opportunity to learn how to use the ‘or’ conditions as well.

3 Likes

Thank you all! The lights are turning on correct but a little late.
As there is no offset to the sun.sun entity i am now testing the OR condition. (also for learning purposes. )

Will see tonight if my automation runs as expected.

  alias: Sunset test
  trigger:
  - at: '23:00'
    platform: time
  condition:      
    condition: and
    conditions:
      - condition: state
        entity_id: light.sfeerlamp_woonkamer
        state: 'off'
      - condition: state
        entity_id: light.sfeerlamp_woonkamer_2
        state: 'off'
      - condition: or
        conditions:
            - condition: sun
              before: sunrise
            - condition: sun
              after: sunset
  action:
  - data:
      message: 'Het is nu 23:00, de zon is onder, de sfeerlampen in de woonkamer staan uit en de mail wordt goed verzonden. '
      title: Sunset test script.
    service: notify.home_assistant

not wanting to be a pita, but, when you trigger an automation in our hemisphere, the sun is always under at 23:00… so these conditions won’t be of any added value.

for styling purpose: you could (I daren’t say should) take out 2 spaces before the individual condition dashes, like:

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

and add 2 spaces in the trigger and action blocks, like:

  action:
    - data:
        message: 'Het is nu 23:00, de zon is onder, de sfeerlampen in de woonkamer staan uit en de mail wordt goed verzonden. '
        title: Sunset test script.
      service: notify.home_assistant

i’d even suggest using:

  action:
    - service: notify.home_assistant
      data:
        message: 'Het is nu 23:00, de zon is onder, de sfeerlampen in de woonkamer staan uit en de mail wordt goed verzonden. '
        title: Sunset test script.

to have it be more readable.

that way your complete automation has the same indentation.

Hi Marius,

I am using this trigger to test if the condition is working correctly for test purposes only.
Right now, i know i have to recieve the e-mail at 23:00. When working like i want it to i can use the condition part in other automations where there is a different trigger.

Will look into the styling part. :slight_smile:
Geert

Because I live in a heavily wooded area, sunrise and sunset are not practical triggers for automation. With ESPhome, a Wemos D1 Mini, and BH1750 ambient light sensor, I read daylight conditions, and trigger outdoor lights on when sensor Lux reading is < 40.

- id: '0010'
  alias: Outdoor lights ON if dark
  trigger:
  - below: '40.0'
    entity_id: sensor.bh1750_illuminance
    for:
      minutes: 2
    platform: numeric_state
  action:
  - entity_id: switch.Front_Porch, switch.Garage_Lights
    service: homeassistant.turn_on
  initial_state: true

cool, thought as much :wink: but you should have a second automation at a time when the sun is still up, to be able to determine if the condition works for you.

Right now, you’re only testing the trigger…

True. Did not add that in the test because the e-mail was not sending.
Will test that one as well :slight_smile:

@FredTheFrog, nice! Might add that one later. It could be nice to have the lighting come up slow and dim slow when there is not enough sunlight.

Geert

Nicest thing about the ambient daylight sensor is the ridiculously low expense:

I’m not at home, or I’d embed my ESPHome configuration yaml file for the device. The other nice thing is it’s very small, and can be easily placed in a window. It’s also automagically discovered/configured by HA since it’s an ESPHome device.

This looks really cool. Do you have a write-up, or is there somewhere I can learn more about how to set one up?

It’s an ESPHome sensor.
It connects via I2C, using pins D1 and D2.
Here’s the YAML for the device:

esphome:
  name: ambientlight
  platform: ESP8266
  board: d1_mini_pro
wifi:
  fast_connect: false
  domain: mydomain.local
  power_save_mode: NONE
  reboot_timeout: 5min
  networks:
  - password: !secret
    ssid: my_wifi_SSID
i2c:
  sda: D2
  scl: D1
  scan: True
sensor:
  - platform: bh1750
    name: "BH1750 Illuminance"
    address: 0x23
    update_interval: 15s
logger:
  level: DEBUG
api:
  password: !secret
  port: 6053
ota:
  password: !secret
  safe_mode: true
  port: 8266

I have the following automation based on the comments here above it uses state: below_horizon

id: '1664013193828'
alias: Sunset
description: ''
trigger:
  - platform: sun
    event: sunset
    offset: 0
  - platform: device
    device_id: 2a544d0ea4b2cd6ef5a62274e9b5be06
    domain: device_tracker
    entity_id: device_tracker.wiz_d796ad
    type: enters
    zone: zone.home
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - service: scene.turn_on
    target:
      entity_id: scene.scene_sunset
    metadata: {}
mode: single

it triggered fine, but the condition state was wrong:

Result:
result: false
state: above_horizon
wanted_state: below_horizon

any idea why state is above_horizon when event sunset has triggered?