How to configure an action based on sunset?

I have an automation set up for a light as follows: It turns on the earlier of sunset or 7:45 pm. The problem is that once DST ends, it turns on too early. So, what I would like is this:

If sunset is later than 6:30 pm
  turn on the earlier of sunset or 7:45 pm
-- else --
  turn on the later of sunset or 6:30 pm

It was suggested that adding the condition “time > 6:30 pm” would make this happen, but it doesn’t. I suspect that the reason is that when the trigger occurs, the condition is not met; and when the condition is met, there is no trigger.

So, my question is this: How to I configure the automation to make the desired behavior happen?

Can we see the yaml?

1 Like

Here is the portion of the yaml that works:

- id: '1647102114032'
  alias: Kitchen Sink Light On
  description: ''
  trigger:
  - platform: sun
    event: sunset
    offset: 0
  - platform: time
    at: '19:45:00'
  condition: []
  action:
  - type: turn_on
    device_id: b00a5fc57fcb525d5ff2ed9f00e1e59d
    entity_id: switch.shelly_2
    domain: switch
  mode: single

This is the condition that was added that did not work:

condition:
- condition: time
  after: '18:30:00'

You could use sun’s elevation instead of sunset+time, elevation always works and is DST independent :wink:

3 Likes

But ofc, offset would do the same

aceindy - Using the sun’s elevation is a good idea, but I am still not sure how to configure the automation. It seems that to turn on the light, I would want the following IF statement to be true:

If sun.elevation < 0 and time > 6:30
 or
Time > 7:45

Can you provide a suggestion?

automation:
  trigger:
    - platform: template
      value_template: {{ state_attr("sun.sun","elevation") < 0 and now() > today_at("06:30") }}
    - platform: time
      at: '07:45:00'

Thank you - I will test it out

I think I did as you suggested:

- id: '1647102114032'
  alias: Kitchen Sink Light On
  description: ''
  trigger:
  - platform: template
    value_template: {{ state_attr("sun.sun","elevation") < 0 and now() > today_at("18:30") }} [[ line 35 ]]
  - platform: time
    at: '19:45:00'
  condition: []
  action:
  - type: turn_on
    device_id: b00a5fc57fcb525d5ff2ed9f00e1e59d
    entity_id: switch.shelly_2
    domain: switch
  mode: single

However, when I tried to restart HA, I saw this:

Failed to restart Home Assistant

The system cannot restart because the configuration is not valid: Error loading /config/configuration.yaml: while parsing a flow mapping in "/config/automations.yaml", line 35, column 22 expected ',' or '}', but got '<scalar>' in "/config/automations.yaml", line 35, column 56

Sorry…I copied from developer tools, forgot about the quotes…

- id: '1647102114032'
  alias: Kitchen Sink Light On
  mode: single
  description: ''
  trigger:
    - platform: template
      value_template: "{{ state_attr('sun.sun','elevation') < 0 and now() > today_at('18:30') }}"
    - platform: time
      at: '19:45:00'
  condition: []
  action:
    - service: switch.turn_on
      target:
        entity_id:
          - switch.shelly_2
      data: {}

And I recommend to use the service switch.turn_on instead of using the device_id.
It will save you the hassle to update your automation(s) when you replace the device :wink:
And please check your indentation…in what you pasted it is wrong :wink:

Thank you.

This time, I was able to restart HA without issue.

Yes, I need to figure out the service switch turn_on. I noticed that your indentation is different from mine, but mine seems to work - odd.

aceindy - Thanks for your reply. The automation you provided works as expected; and as a bonus, I learned more about configuring automations.