Automation trigger at sunset, but latest at 20:00

I think you should be able to use a template and use the “min()” filter.
https://jinja.palletsprojects.com/en/latest/templates/#jinja-filters.min

This will not work, because in this case the automation will be kicked off both at sunset AND at 20:00. I know under some circumstances it may be OK, but I really only want it to run just once.

I am not saying I immediately see what you mean by the first look, but I will start with your clue. Many thanks!

I don’t immediately see how to to min() on an event, tbh.

I’d suggest to use @pedolsky solution with an additional check on when the automation was last triggered, i.e.

  condition:
  - condition: template # only notify once every day at most
    value_template: "{{ ( as_timestamp(now()) - as_timestamp(state_attr('automation.my_automation', 'last_triggered')) |int(0) ) > 28800 }}"

I assume 8h max between sunset and 20:00, should be ok unless you live at a pole :wink:
A full day (86400) won’t work because the sunset time varies every day.

1 Like

On second thought, me neither.
I was thinking about comparing the values, but timestamp and hour might be too difficult…

Ok, maybe you want have a look at this post, which explaines it very nice.

2 Likes

Hey @koying, this is looking a very smart solution! Will try to test it this weekend from all aspects.

Many thanks for helping out an old teammate; remember me from the team you retired? :slight_smile:

Thanks for looking at it, but the first part of the post is the other way around (logic is different at sunrise) and as I said before, for the sunset actions I only want the automation to run once. In the second part of the linked post there is a sunset automation, but there they don’t handle/ care that the automation run twice after each other.

Hahaha… .
Greetings. I didn’t make the link :slight_smile:

With at least @elupus and @da-anda around, that make a bunch of us :wink:

XBMC for life! :slight_smile:

1 Like

One thought here: Use an input_boolean as you thought. (You’ll also need to turn it off at another time (say sunrise?).
Write a script which does the bulk of the automation work, but only after checking that the input_boolean is off.
Write automations for sunset and time which simply call the script.

hey @elupus, good to see you :slight_smile:

If you believe an automation with some sort of ‘all in one’ trigger, without a condition, will “run” less, you’re mistaken.

An automation’s last_triggered attribute is updated if its trigger and condition evaluate to true. In other words, an automation “runs” when it executes its action. Therefore the suggestion to use two triggers (Sunset and Time) plus a condition is perfectly suited for this application.

1 Like

I can’t follow.

The statement you quoted from me was a reply to pedolsky (maybe you missed that?) who linked a solution in another thread triggering sunset and time without condition - resulting the automation to “run” twice after each other.

The solution to your first post has already been provided by the combination of pedolsky’s suggestion (Sunset and Time Triggers) and koying’s suggestion (Template Condition). The resulting automation will execute its action (i.e. “run”) once (per day).

Pfew…
I didn’t say it “won’t work”, did I?

I replied to pedolsky, declaring his solution (in its own) won’t work. Then he referenced another thread, where the same approach was suggested to set sunset and time as trigger without condition, hence I said again this is not ok for my use case.

In the meantime koying proposed his smart solution for which I replied I will test it.

You were jumping on a post where you haven’t checked to whom it was addressed to.

Actually, I indicated you potentially have a mistaken impression of what constitutes a “run” for an automation (regardless of which thread is the reference).

FWIW, if there is a pressing need to combine everything into a single Template Trigger then that would be feasible as well.

This:

and this:

action:
- service: homeassistant.turn_off
  entity_id: automation.this_automation
- delay: "06:00:00"
- service: homeassistant.turn_on
  entity_id: automation.this_automation

I tested your suggestion and it doesn’t work because the moment it turns itself off, that’s the end of everything. The automation does not proceed to execute the delay because it just turned itself off.

I used this to test it:

- alias: control self
  trigger:
  - platform: state
    entity_id: input_boolean.test
    to: 'on'
  action:
  - service: notify.persistent_notification
    data:
      title: Control Self
      message: Turn self off
  - service: automation.turn_off
    target:
      entity_id: automation.control_self
  - delay: '00:00:15'
  - service: automation.turn_on
    target:
      entity_id: automation.control_self
  - service: notify.persistent_notification
    data:
      title: Control Self
      message: Turn self on

It gets as far as sending the first notification and turning itself off.

Here’s another way. You needn’t turn off/on the automation so there’s no risk of restarting HA midway.

trigger:
  - platform: sun
    event: sunset
  - platform: time
    at: '20:00:00'
action:
- choose:
  - conditions:
    #If the next sunset is before 20:00
    - condition: template
      value_template: "{{state_attr('sun.sun', 'next_setting')[11:13]|int <= 19}}"
    sequence:
    - your actions here
  #else (if the next sunset is after 20:00)
  default:
  - wait_template: "{{is_state('sensor.time', '20:00')}}"
  - your actions here