Set max time for sunset trigger

Hi

is there a way to set a maximum time? the actions in the automation may take place at the sunset trigger until it is, for example, 9 pm, then the actions must be carried out anyway

description: ""
mode: single
trigger:
  - platform: sun
    event: sunset
    offset: "-00:20:00"
condition: []
action:
  - service: button.press
    metadata: {}
    data: {}

Add 9pm as an additional trigger. If you don’t want the actions run twice, you can check the last trigger time. Here, it checks the automation hasn’t run in the last five hours (18,000s). Change ENTITY_ID for the actual entity ID of your automation.

description: ""
mode: single
trigger:
  - platform: sun
    event: sunset
    offset: "-00:20:00"
  - platform: time
    at: "21:00"
condition:
  - "{{ now()|as_timestamp - state_attr('automation.ENTITY_ID','last_triggered')|as_timestamp(0) > 18000 }}"
action:
...

That will run at 20 mins before sunset or at 9pm, whichever comes first.

3 Likes

@troon has the right answer. but a small tweak on his, i think you can use the “this” object which will make this portable and not need to replace the entity_id.

description: ""
mode: single
trigger:
  - platform: sun
    event: sunset
    offset: "-00:20:00"
  - platform: time
    at: "21:00"
condition:
  - "{{ now()|as_timestamp - this.attributes.last_triggered|as_timestamp(0) > 18000 }}"
action:
...

Have you tried it? The docs suggest it might not work:

The variable this is the state object of the automation at the moment of triggering the actions.

If you can prove it does work earlier than the action block, perhaps suggest a revision to the docs.

Definitely works. I use it extensively.

  condition:
    condition: template
    value_template: "{{ now() - this.attributes.last_triggered|default(as_datetime(0),1) > timedelta(minutes=1) }}"
3 Likes

@Troon, sorry, i’m not clear how it suggests it might not work? i don’t see that in the docs. but maybe i’m missing something.

we need to check for the first run case where last_triggered isn’t set at all?

@tom_l is your code a better way to check if it’s run already? apologies, i’m not following how it would do that. i may be misreading it, but it looks like it’s checking if it was run more than 1 minute ago. or are you just showing a case where you use it yourself?

The docs suggest that this might only be valid at the start of the action block (“at the moment of triggering the actions”), implying it might not be available earlier on in the condition block where we need it. Tom is confident that it is available earlier, so perhaps the docs need clarifying.

That’s what the (0) in the as_timestamp() does. If it can’t convert the attribute to a timestamp, it returns 0, which is definitely more than five hours ago.

1 Like

Yeah just an example of using this in a condition. It is used throttle an automation to run at most once per minute.

1 Like

ah… subtle. i did not pick up on that. (clearly!) :slight_smile:

Shouldn’t it not be the < instead of > ?

condition:
  - "{{ now()|as_timestamp - state_attr('automation.ENTITY_ID','last_triggered')|as_timestamp(0) < 18000 }}"

You want now minus the time it last ran to be greater than five hours (for example).

If it isn’t — if it has run within the last five hours — the condition will fail and the action won’t run.

That’ll stop it running twice in an evening. Depending on your latitude, five hours may not be enough.

I have similar requirement but for the morning.

To open shutters at max ( 7:00 , sunrise-10 minutes )

In former system, I did:

AT max(time_op(#[Organisation][Sun times][sunrise_code]#,-10),700)

In Home assistant, first I tried to create an helper to get max of the times, but didn’t manage.
So it seems the easiest way is the double trigger and several conditions to trigger only once a day and for the right trigger.

In my case:

Triggers:
- At sunrise-600s
- AND At 07:00

Conditions:
- First execution of the day
- Current time >= 07:00 (to kill early execution when sunrise < 7:00)
- Current time >= sunrise (to kill 7:00 execution when sunrise > 7:00 so I still need to know how to get max of that 2 times)

If you found something more straightforward, please let me know !

did you do this? if so, i think it’s correct and i don’t think it’s too complicated… i don’t understand the purpose of a helper or the other things you asked about. i think this works just as it is.

trigger:
  - platform: sun
    event: sunrise
    offset: "-00:10:00"
  - platform: time
    at: "07:00:00"
condition:
  - condition: sun
    after: sunrise
    after_offset: "-00:10:00"
  - condition: time
    after: "07:00:00"
action: []
1 Like

Thank you for your answer, I found similar advise in another post and I can already confirm it is working well !