Trigger within x minutes before sunset

Seen 2017 similar inquiry with no solution.
I would like to trigger blinds closing within i.e. 45 minutes before sunset, if the light sensor triggers. So say sunset at 16:55, but due to rain the light sensor triggers 16:45, it is within those 45 minutes and such allows to trigger to close the blinds. Ideal to wait five minutes to avoid a dark cloud, but I’m not even at step one.

“Offset” to my understanding (and how my automation triggered) 16:55, offset -00:45, works at 16:10. But not at 16:45.
The light sensor (from shellyparts) triggers simply 1=daylight, 2=dark.

So I would like to set trigger something like
if lightsensor triggers dark AND sunset is within 45 minutes
Though I tend to think this is a bit more complex than a simple if/and :exploding_head:
Any ideas? :pray:

Post what yaml you have, properly formatted, and we’ll help you fix it. You need two triggers (one for light and one for sunset with offset) and two conditions, doing basically the same. That is because both conditions can become true in any order, and you want them both to be true at the same time for the action to happen.

triggers:
- < INSERT LIGHT TRIGGER >
conditions:
    # after 45 minutes before sunset
  - condition: sun
    after: sunset
    after_offset: "-00:45:00"
    # before sunset
  - condition: sun
    before: sunset
actions:
- < CLOSE BLINDS >

Thanks @petro - Immediately tried and it worked “out of the box”. I hope it’ll work the same in real life from now on. For Dummies: Why the offset “after” instead of “before”? Using

- condition: sun
  before: sunset
  before_offset: "00:45:00"

it didn’t work reliably. I also tried the Sunset as trigger and the light sensor and the 45 minutes before as conditions.
I just happen to fail to understand why it workd after:sunset/after_offset. (with negative offset)? Simply didn’t get to the idea to use after (with a negative value). :face_with_peeking_eye:

@Edwin_D As just written, I tried several variants, all not doing what I expected them to do. The “after” seemingly working with a negative offset doing the trick…?
I had a sunset today 20:11. I covered the sensor at 19:33 and after a three minute delay it closed the blind at 19:36. Just as I wanted for several weeks trying over and again (but no “after”).
My last semi-functioning try worked only if minimum 45 minutes prior sunset - but then also in the morning… I think it might have to do with the ''after_offset: “-00:45:00”``, though I’m still puzzled, trying to understand what the difference is.

@evugar @CCOSTAN you had the issue back in 2017, just in case this might help you too still :innocent:
[edit: Replacement of the deleted reply which I wrote German, whoopsie.]

The semantics of this condition can be confusing because of the reference points for the time spans. Take a minute to look at the graphic provided in the docs for this condition. The after configuration variable with a negative offset includes the span of time from the offset until midnight. The before configuration variable without an offset includes the span of time from midnight until the event. Combining them as petro has shown yields a time span that contains the times that are common to both of the original spans.

Thank you @jbarthel and @petro !

Try the Scheduler from HACS. It makes this kind of thing available in the GUI.

The conditions listed above are available in the ui

Just FYI advance, didn’t work yesterday, no closing at sunset, light sensor was “off” more than 90 minutes prior (Shelly shows) thanks to incoming rain, positively off 45 minutes before sunset. Light rain started after sunset, but awning had remained open and had to be closed manually :cry:

Will need to look into it. And tend to make two automations, one for the light sensor and one for the sunset :roll_eyes:

Review your Automation traces to understand what went wrong

I think you have a fundamental miss understanding of triggers and how they work.

Triggers start the automation. Conditions halt the automation if they are not met. Actions are what are ran when the conditions are met.

All that was provided to you was conditions. You have to come up with triggers.

If you want the automation to run exactly 45 minutes before sunset, you need to add that trigger.

If you want the automation to run exactly at sunset, you need to add that trigger.

If you want he automation to run anytime between those times, you need to add those triggers.

I recommend posting your automation as well as a synopsis of your overall goal.

1 Like

Hello @petro, I did post my need in the initial inquiry here :face_with_peeking_eye:
And yes, I may fundamentally misunderstand, that’s why I ask here :sunglasses:

If you want he automation to run anytime between those times, you need to add those triggers.

That’s what I need.

The traces are not available, as there was a HA update I think, which I believe disrupted the tracing, it only shows last night, when it did work. I only recognized in the morning “after” when the awning was still out (and wet). Fortunately a sunny day.

id: '1744392346639'
alias: Sunset Early Dark - Semi-Close Blinds, Close Awning
description: ''
triggers:
  - type: turned_off
    device_id: d85328a336a7f67cdfb1ffae3862d5e9
    entity_id: 571032955aef72e25f73c58f4ccb1e29
    domain: switch
    trigger: device
    for:
      hours: 0
      minutes: 3
      seconds: 0
conditions:
  - condition: sun
    after: sunset
    after_offset: '-00:45:00'
  - condition: sun
    before: sunset
actions:
  - device_id: 8c5e2aa19952fdb233a846283c764308
    domain: cover
    entity_id: aec6b13e58f89fd86d90db3366605ef5
    type: set_position
    position: 22
  - device_id: bc57df3907e66ec0abbe162a6bbf710a
    domain: cover
    entity_id: dabd12b420a1c9cace09117432ca3bea
    type: set_position
    position: 10
  - action: cover.close_cover
    metadata: {}
    data: {}
    target:
      entity_id: cover.awning
mode: single

Create a duplicate Automaton to test the logic. Remove the conditions to start, and when the actions are successful, add the conditions back. Take it one step at time to debug.

You can also manually trigger the Automation to view the Traces

Exactly my approach now :pray:
And reason I wrote Just FYI advance :sunglasses:
It triggered on testing perfectly, yesterday too. But failed two days ago. So it’s a bit difficult to claim the reasoning. Especially as I am novice and no pro.

What is this switch device?

triggers:
  - type: turned_off
    device_id: d85328a336a7f67cdfb1ffae3862d5e9
    entity_id: 571032955aef72e25f73c58f4ccb1e29
    domain: switch
    trigger: device
    for:
      hours: 0
      minutes: 3
      seconds: 0

I am not seeing the Shelly light sensor as a trigger or a condition in your automation

Please share the States details from Developer Tools for what you are calling the light sensor.

Hier die zum YAML gehörende “Visual Editor” Darstellung

Wait why after sunset and before sunset conditions?

The building blocks have to be consolidated to one And with an Or logic.

How can the sun status be after sunset and before sunset?

-45 minutes after sunset = 45 minutes before sunset

1 Like

Thank you, I should have been more clear. Doesn’t the When need to be

conditions:
  - condition: sun
    after: sunset
    after_offset: '-00:45:00'
  - condition: sun
    before: sunset

and the condition need to be the switch and define multiple triggers?

No, that’s a 45 minute span before sunset. Edit, missed post by Francis

1 Like