After sunset OR before sunrise

I am very helpless to get a very simple automation running by configuring it just by clicking.
There is a PIR (motion sensor) installed and the light has to be turned on from sunset to sunrise with a little offset time.
I combined the sunset and sunrise condition with an OR.
First condition “sunset”, second condition “OR” with nested sunrise.

This is the created YAML:

alias: Nachts Küchenlicht an
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 71d03128e2337c0e9f7f4ca0a4eda047
    entity_id: binary_sensor.pir_kueche_occupancy
    domain: binary_sensor
condition:
  - condition: sun
    after: sunset
    after_offset: '-00:30:00'
  - condition: or
    conditions:
      - condition: sun
        before: sunrise
    enabled: true
action:
  - type: turn_on
    device_id: 01358ce094d47f3ef55aba5004e96228
    entity_id: switch.kuechenlicht
    domain: switch
mode: single
1 Like

Is this automation working for you? I’m assuming not as you have raised a query…

I use a YAML Time of Day (TOD) binary sensor to determine if it is day or night. They recently added Helpers for TOD to the UI but it cannot take the sun as reference, only time:

- platform: tod
  name: Night
  after: sunset
  before: sunrise

- platform: tod
  name: Day
  after: sunrise
  before: sunset

Anyway, I usually add these sensors as conditions in my automations to determine if its night or day. Hope that helps.

2 Likes

Please review the Conditions documentation. That is not how the or condition is used. This is:

condition:
  - condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: '-00:30:00'
      - condition: sun
        before: sunrise

Or if you want to use the new shorthand:

condition:
  - or:
      - condition: sun
        after: sunset
        after_offset: '-00:30:00'
      - condition: sun
        before: sunrise
4 Likes

And since you are using ‘sun’ … at some point during the night the sunset changes…so you also have to add a condition to deal with that .e.g alike: time < sunrise && sunset < sunrise

Many thanks to you all.
I now clicked a new automation and got this yaml:

alias: Im Dunkeln an
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 71d03128e2337c0e9f7f4ca0a4eda047
    entity_id: binary_sensor.pir_kueche_occupancy
    domain: binary_sensor
condition:
  - condition: or
    conditions:
      - condition: sun
        before: sunrise
      - condition: sun
        after: sunset
action:
  - type: turn_on
    device_id: 01358ce094d47f3ef55aba5004e96228
    entity_id: switch.kuechenlicht
    domain: switch
mode: single

Hardy can wait until tonight :slight_smile:

There is no need for that.
The sun only acts on ONE day, so at midnight both sunrise and sunset will change to the current values of that day. In other words sunset can never be earlier than sunrise.

2 Likes

Ah…yes… I made a small mistake when I cam using NEXT_sunrise in one of my automations, thanks for correction
Edit:…or … not… when it is 23:00 the next_sunset is still earlier that day (say 20:00) and a bit later will change to 20:01 the day after where next_sunrise does not change…anyhow just do with this info what you want :slight_smile:

Your midnight and the HAOS midnight might not be the same, but the sunrise and sunset will still change at the same time.
I guess it could be an issue, if sunrise or sunset is in that one hour window.

Personally I use a custom component called sun2, that list both yesterdays, todays and tomorrows value, so I have no issues like this.

It works!
Maybe other people have this problem, too.
What to click in the automatisation-page:
Click OR in the first option and then add all the options that you want to have ored.

1 Like

alias: Outside-Light-ON-OFF
description: Outside-Stair-Light-On/off
trigger:

  • platform: sun
    event: sunset
    condition:
  • condition: sun
    after: sunset
    after_offset: “-1:00:00”
  • condition: time
    after: “01:00:00”
    weekday:
    • thu
    • wed
    • tue
    • mon
    • sun
    • fri
    • sat
      action:
  • service: switch.turn_on
    data: {}
    target:
    entity_id: switch.e16s_output1
  • service: switch.turn_off
    data: {}
    target:
    entity_id: switch.e16s_output1
    mode: single

Is my Automation script good , will it work - Please correct if require

It’s difficult to say as you have not formatted it correctly for the forum. Please see: https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

Thanks for Highlight Actually I am new to Home Assistance, please find below the code

trigger:
- platform: sun
event: sunset
condition:
- condition: sun
after: sunset
after_offset: "-1:00:00"
- condition: time
after: "01:00:00"
action:
- service: switch.turn_on
entity_id: switch.e16s_output1
- service: switch.turn_off
entity_id: switch.e16s_output1
mode: single

Probably not the way you want… As configured, at sunset the switch will turn on then immediately turn off.

We don’t know what you are trying to accomplish… Please describe your goal and the requirements for your automation.

And that is still not formatted correctly.

Want to automate - Light “On” after 1 hr of Sunset and Light “OFF” in Midnight

There are multiple ways to do that, one way is as follows. Below, I am assuming (based on your original post’s condition) that you want the light to come on 1 hour before sunset. If that is not the case and you want it to turn on 1 hour after sunset, remove the negative from the offset value.

trigger:
  - platform: sun
    event: sunset
    offset: "-01:00:00"
    id: 'on'
  - platform: time
    at: "00:00:00"
    id: 'off'
condition: []
action:
  - service: switch.turn_{{trigger.id}}
    entity_id: switch.e16s_output1

Thanks for sharing the code, I will use and check it


light on before 10 min sunset

alias: sunset out light on time base off
description: “”

trigger:
  - platform: sun
    event: sunset
    offset: "-00:10:00"
    id: "1"
  - platform: time
    at: "23:00:00"
    id: "2"

I have tested the script- it working perfectly!
The only thing i have changed is my Zeo Location in HA - useful for Sunset timing.

Hi, my entity is not working properly, due to some issue in the Time Configuration
Would appreciate if anyone could help me

  1. In condition I want to add – time 2 to 4 am and 2 to 4 pm – switch.toggle
  2. In the If statement I want to add - time after 4 am to 2 pm & 5pm to 1 am — if the entity state “ON” – ACTION “switch.off”

The below code is not working correctly

trigger:
- platform: state
entity_id:
- binary_sensor.input_2
id: "on"
- platform: state
entity_id:
- binary_sensor.input_2
id: "off"
condition:
- condition: time
after: "18:40:00"
enabled: true
- condition: or
conditions:
- condition: time
after: "06:40:00"
action:
- service: switch.toggle
entity_id: switch.e16s_output2
- if:
- condition: state
state: "on"
entity_id: switch.e16s_output2
- condition: time
after: "08:35:00"
- condition: or
conditions:
- condition: time
after: "20:54:00"
then:
- service: switch.turn_off
entity_id: switch.e16s_output2
mode: single