Trying to get the hang of automations

So I’m at the point where I’ve set up groups and views, dns, encryption and so on. Thanks to many great guides and this forum. So now I’m kind of stuck in the automation section. I made my first automation that was pretty easy to set up.
Trigger was time both for on and off. The easy part was that it included only one switch.
Now I’m trying to add more switches in my second automation that is gonna be sun-triggerd.

If you look at what I’m trying to achieve:

Call service

alias sunset

service: switch.turn_on

{
“entity_id”: “switch.bokhyllor_arbetsrum”,
“entity_id”: “switch.dags_fonsterlampa”,
“entity_id”: "“entity_id”: “switch.dags_fonsterlampa”
}

What am I doing wrong?

I see an extra entity_id that you don’t need on the last line.

Are you trying just to test an automation or are you trying to build one in an automation.yaml file? For that you’ll need to do something different.

You need something like the following…

 - alias: 'Turn off Garden Cams at night' <------ change alias to whatever you like
   initial_state: 'on'
   trigger:
     - platform: sun
       event: sunset
   action:
     - service: switch.turn_off <------- Change to turn_on
       entity_id: switch.rfl_garden_cam_rhd, switch.rfl_garden_cam_lhd <----- put your own entity_id's here separated by ',' commas

All my automations are in a separate automations.yaml file called fromthe configuration.yaml with

automation old: !include automation.yaml

It’s not a test, its many switches I want to turn on and the question is how I separate them correctly. If I do it in the web-tool I seem to need separate them differently from when I do it directly in the automation.yaml-file. Is that correct?

@insideout Yes, you are correct. In the web, it’s mostly for testing. I personally don’t use it often, I build my automations in it’s own yaml file.

So as @keithh666 pointed out, the format for automations is triggers, (conditions are optional), and actions. If you want to have the same service (in this case switch.turn_on) you list the switch entities under entity_id:

 - alias: 'Switch on at Sunset'
   initial_state: 'on'
   trigger:
     - platform: sun
       event: sunset
   action:
     - service: switch.turn_on
       entity_id: 
         - switch.bokhyllor_arbetsrum
         - switch.dags_fonsterlampa

so do each entity need a separate row or can I just keep them on the same and separate them with a “,” comma?

@insideout I think you can do both, but I’d use a new line since that’s how it’s officially documented, not to mention easier to read.

That might be a valid point - I’ll try and see. Another stupid question, what does the “inital state” -do. Isn’t the initial state off, since I want them to turn “on”?

@insideout Actually it’s not a stupid question. Initial_state: is completely optional, but I use it because on occasion, when I restart HA, automations don’t run because they are somehow turned “off”. Think of it like enabling/disabling automations rather than on/off. You want your automation enabled so that it can run when triggered. If it’s “off” its basically disabled from running even when triggered.

Ahh perfect…does this look right to you now or do I miss out at something?

  • action:
    • alias: Switch on at sunset
      initial state: on
      trigger:
      • platform: sun
        event: sunset
        offset: “-00:45:00”
        action:
      • service: switch.turn_on
        entity_id:
        • switch.bokhyllor_arbetsrum
        • switch.dags_fonsterlampa
        • switch.fonsterlampa_foraldrars_sovrum
        • switch.fonsterlampa_rubens_rum
        • switch.golvlampa_tv
        • switch.lampa_gymmet
        • switch.ph_golv_datarum
        • switch.ph_golv_vardagsrum
        • switch.ph_hall
        • switch.tvbank_lampor
        • light.dimbar_brytare_1

Well that didn’t work, the check-config didn’t like this for some reason :tired_face:

Very close…1) make sure the action is lined up with the trigger in terms of spacing (YAML is very picky.) and 2) you’ll want to use a different service for switches and lights. Alternatively, I think you could use home_assistant.turn_on for all of them together, but I haven’t tried it. Here it is properly formatted:

- alias: 'Switch on at sunset'
  initial_state: on
  trigger: 
    - platform: sun
      event: sunset
      offset: "-00:45:00"
  action:
    - service: switch.turn_on
      entity_id: 
        - switch.bokhyllor_arbetsrum
        - switch.dags_fonsterlampa
        - switch.fonsterlampa_foraldrars_sovrum
        - switch.fonsterlampa_rubens_rum
        - switch.golvlampa_tv
        - switch.lampa_gymmet
        - switch.ph_golv_datarum
        - switch.ph_golv_vardagsrum
        - switch.ph_hall
        - switch.tvbank_lampor
   - service: light.turn_on
     entity_id: light.dimbar_brytare_1

Check configuration - VALID - where do I send the bottle of wine?! :smiley::wine_glass:

LOL. Happy to help, as I’ve received lots of help myself over the last year, so I just try to help in return. HA is a little bit of a learning curve, but learning is half the fun!

Thats exactly how I think also…I’ll go in to next phase now and using “time” as the trigger instead…have a great week-end!

@insideout Thank you!. Have a great one yourself.

well - I guess I got too happy too fast. The system didn’t like it event thoug it said it was valid. I tried to check it with YAMLint and get this response - any ideas?

For readability, you can also group your switches.

However, line 2 is likely your problem - it should be:

initial_state: 'on'

Note both the underscore, and the quotes around the state

Something seems to be wrong in the first row? What can that be?

Can you share the raw text - using hastebin.com or similar?