Sunrise Sunset automation

Hi all, I’m totally new on HA and I am trying to create an automation for the lights of my backyard.
The first question is if I must paste the code in automations.yaml or configuration.yaml
This is the code that I have copied from other post:

[

  • alias: Lights on with the Sunset
    trigger:
    platform: sun
    event: sunset
    offset: ‘+00:15:00’
    action:

    • service: switch.turn_on
      entity_id: switch.luces_patio
  • alias: Lights off with the sunrise
    trigger:
    platform: sun
    event: sunrise
    offset: ‘+00:15:00’
    action:

    • service: switch.turn_off
      entity_id: switch.luces_patio

    ]

I have pasted this in automations.yaml and I have an error that says:

missed comma between flow collection entries at line 3, column 1:
- alias: Lights on with the Sunset
^

Thanks so much in advance to anyone who can help me!

Much easier to set up automations through the UI to start with (Configuration | Automations).

1 Like

Your formatting is wrong. You can place it in automations.yaml only if you have a line in your configuration.yaml like automation: !include automations.yaml. In either case, your rules should look like this:

- alias: Lights on with the Sunset
  trigger:
    platform: sun
    event: sunset
    offset: '+00:15:00'
  action:
    service: switch.turn_on
    entity_id: switch.luces_patio

- alias: Lights off with the sunrise
  trigger:
    platform: sun
    event: sunrise
    offset: '+00:15:00'
  action:
    service: switch.turn_off
    entity_id: switch.luces_patio

If you are adding these to configuration.yaml, you’ll want to add automation: as the first line.

Thanks for the response! I will try and share to know if I am doing well.

1 Like

Remove the two existing automations in the automations.yaml file and replace them with this single automation and save the file:

- alias: 'Lights Sunset Sunrise'
  trigger:
  - platform: sun
    event: sunset
    offset: '+00:15:00'
  - platform: sun
    event: sunrise
    offset: '+00:15:00'
  action:
  - service: "switch.turn_{{'on' if trigger.event == 'sunset' else 'off'}}"
    entity_id: switch.luces_patio

Then execute Configuration > Server Controls > Check Configuration.

  • If it fails the test, let us know and we will help you solve the problem.
  • If it passes the test, execute Configuration > Server Controls > Reload Automations. Now the new automation is ready for use.
1 Like

Thanks for your meesage! look at the capture, this is what I have in configurations.yaml

image

You’re welcome! Ok, since that is there, you can place your rules in automations.yaml.

I have pasted you code but the system is giving me an error that says:
missed comma between flow collection entries at line 3, column 5:
- alias: ‘Lights Sunset Sunrise’
^

Look very carefully at the screenshot you posted and what I posted. Do they look exactly the same?

No they don’t.

In your version:

  • Remove the leading and trailing square brackets [ ] that surround the automation.
  • Remove the four spaces that preface - alias:

It should look like this:

Screenshot 2020-10-13 115212

2 Likes

Remove the [ and ] and fix your spacing. YAML is VERY picky about spacing. Your code should look like this:

- alias: 'Lights Sunset Sunrise'
  trigger:
  - platform: sun
    event: sunset
    offset: '+00:15:00'
  - platform: sun
    event: sunrise
    offset: '+00:15:00'
  action:
  - service: "switch.turn_{{'on' if trigger.event == 'sunset' else 'off'}}"
    entity_id: switch.luces_patio

I used your much better example @123 lol

2 Likes

genius! thank so much for your help now I am able to save the config!
Now I will try to add a timer for my grass irrigation :slight_smile:

Thanks so much for share a little bit of your time to help me! :slight_smile:

1 Like

For future reference, it’s the custom of this community to assign “Solution” to the first, or best, post that provides, or leads to, the solution.

What you have done is mark your own post as the Solution, yet it is literally a copy of a solution offered to you.

2 Likes

Thanks so much for teach me :wink: I have changed the solution tick.

2 Likes

Hello,

New Home Assistant user in the house. I want to thank all of you for that great work. As I am planning to migrate from ST to HA how can I use this automation to turn ON/OFF not one light but all my outdoor lights (6 in total)?

Thanks

Yes, the easiest way to control them all at once is to put them in a group

I appreciate your quick reply, I will try it later and will keep you posted. Having a second HA server for testing purposes or just relying on the backups which one is better? Also, can you suggest to me some good tiny PCs for HA? Thanks.

Those questions are off topic for this thread and already covered in many other threads. Please use the search function or start new topics instead of asking them here.

Sorry, will do that.

Working like a charm. Thank you again!