Splitting out my Automation yaml First Automation

Hey Guys,

First automation try with HA. I am just trying to turn on my porch lights based on sun and turn them off based on sun with an offset on both. I am doing a separate automations.yaml right away as I want to keep this clean from the beginning without having to redo it later as things get more complicated. I have the following in the automations.yaml:

 #Porch Lights
- alias: Turn on porch lights when sun sets
  trigger:
    platform: sun
    event: sunset
    offset: "+00:15:00"
  action:
    service: switch.turn_on
    entity_id: switch.ge_14292_inwall_smart_toggle_switch_switch

- alias: Turn off porch lights when sun rises
  trigger:
    platform: sun
    event: sunrise
    offset: "-00:15:00"
  action:
    service: switch.turn_off
    entity_id: switch.ge_14292_inwall_smart_toggle_switch_switch

When I run a check on the configuration I get the following error:

Configuration invalid

extra keys not allowed @ data[ā€˜automationā€™]

If I comment out the
automation: !include automations.yaml

The error goes away but obviously the automation doesnā€™t work.

Itā€™s probably a spacing issue. I would look up an example of automations.yaml in someoneā€™s Github. And check spaces. Sorry donā€™t have access to my own examples right now.

I believe this has to do with the two ā€œoffset:ā€ lines in your automation.

Sun conditions need to be either ā€œbefore_offsetā€ or ā€œafter_offsetā€ according to this table: https://www.home-assistant.io/docs/scripts/conditions/#sun-condition

Here is one of my light on automation at sunset that has a delay on it to help you out,

- alias: SunSet Lights on
  trigger:
  - event: sunset
    offset: -00:45:00
    platform: sun
  condition: []
  action:
  - data:
      entity_id: scene.away_scene
    service: scene.turn_on

So yours should look something like this,

- alias: Turn on porch lights when sun sets
  trigger:
  - event: sunset
    offset: +00:15:00
    platform: sun
  condition: []
  action:
  - data:
      entity_id: switch.ge_14292_inwall_smart_toggle_switch_switch
    service: switch.turn_on

You should be able to work out the off automation from there.

Your automations look good. Nothing out of the ordinary. What does your configuration look like around:

automation: !include automations.yaml
1 Like

ā€¦ And also please post your entire automations.yaml - I suspect the key automation: appears in it somewhere when it shouldnā€™t.

2 Likes

I suspect you are correct but that is my entire automations.yaml file. Are you talking about configuration.yaml?

homeassistant:
  # Name of the location where Home Assistant is running
  name: Name
  # Location required to calculate the time the sun rises and sets
  latitude: 43
  longitude: -88
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: 500
  # metric for Metric, imperial for Imperial
  unit_system: imperial
  # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  time_zone: America/Chicago
  # Customization file
  customize: !include customize.yaml
  # Automation File
  automation: !include automations.yaml

# Show links to resources in log and frontend
introduction:

# Enables the frontend
frontend:

# Enables configuration UI
config:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

# Checks for available updates
# Note: This component will send some information about your system to
# the developers to assist with development of Home Assistant.
# For more information, please see:
# https://home-assistant.io/blog/2016/10/25/explaining-the-updater/
updater:
  # Optional, allows Home Assistant developers to focus on popular components.
  # include_used_components: true

# Discover some devices automatically
discovery:

# Allows you to issue voice commands from the frontend in enabled browsers
conversation:

# Enables support for tracking state changes over time
history:

# View all events in a logbook
logbook:

# Enables a map showing the location of tracked devices
map:

# Track the sun
sun:

# Weather prediction
sensor:
  - platform: yweather
    name: weather_current
    woeid: 12780788
    monitored_conditions:
      - weather
      - weather_current
      - temp_min
      - temp_max
      - wind_speed
      - pressure
      - visibility
      - humidity
      - temperature

group:
  weather_current:
    name: Weather Current
    view: no
    entities:
      - sensor.weather_current_condition
      - sensor.weather_current_current
      - sensor.weather_current_humidity
      - sensor.weather_current_pressure
      - sensor.weather_current_temperature
      - sensor.weather_current_temperature_max
      - sensor.weather_current_temperature_min
      - sensor.weather_current_visibility
      - sensor.weather_current_wind_speed
      - sun.sun

 
#automation:
#  - alias: Turn on porch lights when sun sets
#    trigger:
#      platform: sun
#      event: sunset
#      offset: "+00:15:00"
#    action:
#      service: switch.turn_on
#      entity_id: switch.ge_14292_inwall_smart_toggle_switch_switch

#  - alias: Turn off porch lights when sun rises
#    trigger:
#      platform: sun
#      event: sunrise
#      offset: "-00:15:00"
#    action:
#      service: switch.turn_off
#      entity_id: switch.ge_14292_inwall_smart_toggle_switch_switch


# Text to speech
tts:
  - platform: google
  
# Z-Wave
zwave:
  usb_path: /dev/ttyACM0
  
# Configurator
panel_iframe:
  configurator:
    title: Configurator
    icon: mdi:wrench
    url: http://hassio.local:3218

# Cloud
#cloud:

#group: !include groups.yaml
#automation: !include automations.yaml
script: !include scripts.yaml

Move these two lines (15 and 16)

# Automation File
automation: !include automations.yaml

Back two spaces (all the way to the left margin)

That got rid of the config error. Now weā€™ll see if it works tonight. So, why does the line:

  # Customization file
  customize: !include customize.yaml

not give me problems being indented two spaces? Is that file link treated differently than automation.yaml? I didnā€™t glean that info from the configuration documents if that is the case.

I remember my C++ classes 20 years ago and it was always syntax, syntax, syntax! This is giving me flashbacks.

Because customize: is a part of the homeassistant core component, automation: is a separate component in its own right.

Thatā€™s for the push in the right direction. Iā€™ll let you guys know if my lights function properly now.

1 Like

I was reading someone elseā€™s post about this and there seemed to be some confusion over whether the sun is being used as a trigger or a condition. I took it to mean you are correct if I am using sun as a condition but what I did is supposed to be correct as a trigger. I could be very wrong on that but Iā€™ll know more tonight when the automation works or not.

Good news it has been working the past several days. I will post what I am using so others can see.

 #Porch Lights
- alias: Turn on porch lights when sun sets
  trigger:
    platform: sun
    event: sunset
    offset: "+00:15:00"
  action:
    service: switch.turn_on
    entity_id: switch.ge_14292_inwall_smart_toggle_switch_switch

- alias: Turn off porch lights when sun rises
  trigger:
    platform: sun
    event: sunrise
    offset: "-00:15:00"
  action:
    service: switch.turn_off
    entity_id: switch.ge_14292_inwall_smart_toggle_switch_switch