Help with my first automation - XMas Tree

Hi Guys! - I have managed to get Home Assistant and OZWCP successfully up and running and now I am attempting to configure my first automation script. I would like to do the following:

AM: Turn on switch.christmas_tree_switch_3_0 at sunrise and turn off at 9:20am
PM: Turn on switch.christmas_tree_switch_3_0 at sunset and turn off at 11:00pm

With the help of SkyCryer on reddit he advised me of this script but it does not function. In fact, I have to comment out “platform: sun… etc” to get HASS to start after a restart. I’m sure there is something incorrectly formatted with the white space.

Can someone please explain the logic behind the script below and give some advice on how to get it working to the requirements above. Thanks in advanced!

  automation:
     alias: xmas tree on 
     hide_entity: false 
     trigger: 
       platform: time 
       after: '09:20:00'    
       #platform: sun      
       #event: sunset      
       #offset: "-00:45:00" 
     action: 
       service: switch.turn_on 
       entity_id: switch.christmas_tree_switch_3_0

I’m new here also but since no one else has replied yet I’ll put in my 2 cents. I think you might want to set the trigger as after sunset and then user a condition of after 9:20 am in your example above.

You need to break this into at least 2 automations one with the action to turn_on and one with the action to turn_off

Something like this: (with the correct yaml spacing…)

# Turns on Christmas Tree lights
  • alias: ‘Christmas Tree on’
    trigger:
    • platform: sun
      event: sunrise
    • platform: sun
      event: sunset
      event: sun
      action:
      service: homeassistant.turn_on
      entity_id: group.tree

and a similar one to turn the tree off at the appropriate times.

So you do need two separate automations for this. One to turn on and one to turn off.

To turn it on at sunset and sunrise…

  - alias: 'Turn On Tree Lights'
    trigger:
      - platform: sun
        event: sunrise
      - platform: sun
        event: sunset
    action:
      - service: homeassistant.turn_on
        entity_id: switch.christmas_tree_switch_3_0

To turn off the tree at 9:20 AM and 11:00 PM

  - alias: 'Turn Off Christmas Tree'
    trigger:
      - platform: time
        after: '09:20:00'
      - platform: time
        after: '23:00:00'
    action:
      - service: homeassistant.turn_off
        entity_id: switch.christmas_tree_switch_3_0

Note that for the sunrise and sunset components to work you need to have sun: in your configuration file. It is entered with no leading spaces. Also since you are adding multiple automations make sure they are both under the automation: heading in your config file. automation: is also entered with no leading spaces, however, each of the above automation examples need to be indented by two spaces to start. So two spaces and then - alias.

edit: The sunset option in your original automation didn’t work because you have multiple platforms under the trigger. When you do this you have to indent them two spaces and lead with a hyphen as I did above.

Thanks for the help everyone! One question - I understand why you would use the " - " for the multiple platform but why is it used for alias and action?

Just for reference, below is the the code that achieved what I was looking for:

automation:
  - alias: xmas tree on
    hide_entity: false
    trigger:
      - platform: sun
        event: sunrise
      - platform: sun
        event: sunset
        offset: "-00:30:00"
    action:
      - service: homeassistant.turn_on
        entity_id: switch.christmas_tree_switch_3_0

  - alias: xmas tree off
    hide_entity: false
    trigger:
      - platform: time
        after: '09:20:00'
      - platform: time
        after: '23:00:00'
    action:
      - service: homeassistant.turn_off
        entity_id: switch.christmas_tree_switch_3_0

because you have multiple aliases in your automation section.

Honestly not sure on the alias. I think that was from an example I used once.

On the action it was due to me actually having that automation in my config. I call two actions so actually need that arrangement.