Trying to light a light when I get home

Hello all,
After a week of struggeling I hope some can help me with my first automation. I’m trying to convince Home Assistant to turn on al lamp in my garden when I come home after sunset, and to turn off the light after 5 minutes. I read a lot but I cannot get it to work. First, this is my code:

  • alias: ‘lamp tuinkast aan bij thuiskomst’
    initial_state: true
    hide_entity: false
    trigger:
    • platform: state
      entity_id: device_tracker.ruud_van_gurp
      to: ‘home’
      condition:
      condition: and
      conditions:
    • platform: sun
      after: sunset
      action:
      service: light.turn_on
      entity_id: light.lamp.tuinkast
      delay: ‘00:05:00’
      service: light.turn_off
      entity_id: light.lamp.tuinkast

When i try to validate it, I get this error:

Invalid config for [automation]: expected a dictionary @ data[‘condition’][0]. Got None
extra keys not allowed @ data[‘action’][0][‘delay’]. Got None
extra keys not allowed @ data[‘conditions’]. Got None
not a valid value for dictionary value @ data[‘action’][0][‘entity_id’]. Got None. (See /config/configuration.yaml, line 105). Please check the docs at Automation - Home Assistant

When I look in configuration.yal, it’s just the standard code at line 105:
105 group: !include groups.yaml
106 automation: !include automations.yaml
107 script: !include scripts.yaml

So, I’m at a loss here. I am fairly new to HA, but eager to learn…. Can someone please help me?

this is mine… I have a solar angle as a sensor, but basically the same as sunset. Double condition as I check to see if the light is already off before trying to turn it on.

- alias: Hallway on when one of us arrives home
  initial_state: 'on'
  hide_entity: False
  trigger:
  - platform: state
    entity_id: group.helen
    from: 'not_home'
    to: 'home'
  - platform: state
    entity_id: group.mark
    from: 'not_home'
    to: 'home'
  condition:
    condition: and
    conditions:
    - condition: numeric_state
      entity_id: sensor.solar_angle
      below: -1.0
    - condition: state
      entity_id: light.hallway
      state: 'off'
  action:
     - service: light.turn_on
       entity_id: light.hallway
       data:
         brightness: 200
     - delay: '00:10:00'
     - service: light.turn_off
       entity_id: light.hallway

The hyphens are most likely the problem. With strings following are accepted:

"string1", 'string2'

All other combinations of ` and whatever there are, are prohibitet.

Also if changing those does not help, please check the blue banner on top and lets see the code indentation.

I might be missing somthing with the formatting, but it looks like you have blank conditions.

Here’s a copy of my only presence based light automation.

# Turns on Christmas Tree lights
- alias: 'Christmas Tree on'
  trigger:
  - platform: time
    at: '5:00:00'
  - platform: state
    entity_id: group.people
    from: 'not_home'
    to: 'home'
  - platform: state
    entity_id: input_boolean.holiday_mode
    from: 'off'
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: group.people
        state: 'home'
      - condition: state
        entity_id: input_boolean.holiday_mode
        state: 'on'
      - condition: state
        entity_id: group.tree
        state: 'off'
      - condition: time
        before: '22:30'
  action:
    service: homeassistant.turn_on
    entity_id: group.tree
#

Thanks for the help, I rewrote my code so it looks like this (thanks to MarkR):

    - alias: 'lamp tuinkast aan bij thuiskomst'
initial_state: true  
hide_entity: false   
trigger:
- platform: state
  entity_id: device_tracker.ruud_van_gurp
  from: 'not_home'
  to: 'home'
condition:
  - condition: and
  conditions:
     platform: sun
     after: sunset
 - condition: state
     entity_id: light.lamp_tuinkast
     state: 'off'
action:
 - service: light.turn_on
   entity_id: light.lamp_voordeur
   delay: '00:05:00'
 - service: light.turn_off
   entity_id: light.lamp_voordeur

Still getting errors though:

Error loading /config/configuration.yaml: while parsing a block collection in “/config/automations.yaml”, line 74, column 7 expected <block end>, but found ‘?’ in “/config/automations.yaml”, line 75, column 7

So, like taikapnu suggested, there might be something wrong in the indention that I’m not aware of; I must admit I have a hard time getting my head around this YAML thing. Still trying to learn…

Hope to hear from you all!

Are you using a text editor that knows how to correctly format YAML? If you’re not, I suggest you do otherwise editing your configuration files will continue to be a headache.

Your example contains several errors:

  • Indenting is incorrect in several places.
  • The first condition is incorrect (platform: sun).
  • In action: the delay is entered incorrectly.

Try this (it passes Config Check on my system):

- alias: 'lamp tuinkast aan bij thuiskomst'
  initial_state: true  
  hide_entity: false   
  trigger:
  - platform: state
    entity_id: device_tracker.ruud_van_gurp
    from: 'not_home'
    to: 'home'
  condition:
    condition: and
    conditions:
    - condition: sun
      after: sunset
    - condition: state
      entity_id: light.lamp_tuinkast
      state: 'off'
  action:
  - service: light.turn_on
    entity_id: light.lamp_voordeur
  - delay: '00:05:00'
  - service: light.turn_off
    entity_id: light.lamp_voordeur

Yes, I use Notepad++ and Cloud9 IDE (a Hassio add-on). The problem is more that I’m not really sure where to indent what, so it’s a bit trial and error for me. I copied your code, and still got errors. But after adding two spaces in front of every line it worked! :partying_face:
If anyone has a simple manual on indenting I think I might have less headache in the futere. For now: thanks all (and especially 123) for helping me out!

Congrats! Figuring out yaml and getting your first automation working is a great accomplishment. I know for me, it felt like I had conquered the world.

Tip:

The indenting for automations will be slightly different if they are stored in configuration.yaml or if they are in a separate file called automations.yaml. The example I gave you was created within an automations.yaml file (and passed Config Check).

In the documentation’s Automations Example, you’ll notice all automations under automation: are indented by at least two spaces. If they were in an automations.yaml file, it wouldn’t be necessary to do that.

Study the examples shown in the Cookbook.

Yes, I know exactly what you mean. I don’t think it will be a breeze from now on, but I’m hooked for sure!

The indenting for automations will be slightly different if they are stored in configuration.yaml or if they are in a separate file called automations.yaml .

Yes, I found that out the hard way :sunglasses: I use automations.yaml, but everything works only when I use to spaces in front of every line. Else it does’nt passthe config check. For me problems begin below the first line :sweat_smile: , where (for my feeling) you sometimes use indentions, and sometimes go back to normal indention.

Study the examples shown in the Cookbook.

Will do that. I just glanced over it, hoping to sort it out myself, but it is a lot more strict then I realized.
Thanks all for helping this newbie out!

That’s interesting. I didn’t have to do that (for the example I gave you). If you have existing automations that are already indented by two spaces, all additional automations will also have to include the same indenting.

Example 1: No leading indentation (notice the hyphens are not indented).

- alias: "Automation 1"
  trigger:
  condition:
  action:

- alias: "Automation 2"
  trigger:
  condition:
  action:

Example 2: With leading indentation (notice the hyphens are indented).

  - alias: "Automation 1"
    trigger:
    condition:
    action:

  - alias: "Automation 2"
    trigger:
    condition:
    action:

Example 3: Mixed indentation (mixing the indentation will fail on Config Check).

  - alias: "Automation 1"
    trigger:
    condition:
    action:

- alias: "Automation 2"
  trigger:
  condition:
  action:

Well, I do have a first attempt in automation .yaml which have 2 indents at the beginning. I’ll try to remove them and see what the config checker says. Thanks for the explanation, it helps in understanding what I’m doing…