Automation trouble

I’m trying to write my first automation.

I’ve only just started using home assistant and I’m sure I’m missing a load of information about usage.

I’ve tried using the automation editor built into the ui but have found it to be too restrictive with it’s options.

Essentially I’m trying to turn the porch light on automatically from sunset to 21:30 when either my wife or I return home if the light is off.

 - id: 'one'
      alias: 'Porch light auto on'
      initial_state: true
      trigger:
        - platform: sun
          event: sunset
          offset: '-00:30:00'
          - condition: or
            conditions:
             - condition: state
               entity_id: device_tracker.honor_view10_cba3a944ee4b
               state: home
             - condition: state
               entity_id: device_tracker.honor_9_45989bdec533b55e
               state: home
        - condition: time
            before: '21:30:00'
      action:
          service: light.turn_on
          entity_id: light.porch_light

I’m getting errors with this code… is there a tool that can check code and highlight or suggest fixes? I’m struggling to work out the syntax required with the spaces and the dashes between statements.

thanks

Looking at your code, it looks like an issue with the indenting which is very sensitive…
Try the below

- id: 'one'
  alias: 'Porch light auto on'
  initial_state: true
  trigger:
    - platform: sun
      event: sunset
      offset: '-00:30:00'
  condition: or
    conditions:
    - condition: state
      entity_id: device_tracker.honor_view10_cba3a944ee4b
      state: home
    - condition: state
      entity_id: device_tracker.honor_9_45989bdec533b55e
      state: home
    - condition: time
      before: '21:30:00'
  action:
    service: light.turn_on
    entity_id: light.porch_light

thanks for the quick response. I’m struggling a bit with the requirements of spacing, but it’s early days yet.

That’s helped, although it’s giving me another fault now

Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/automations.yaml”, line 9, column 15

nevermind, I might of fixed it. I took out the conditions: or

I don’t think I need it.

You should swap the triggers with the conditions.
Note this before I continue:
Triggers have always the OR logic. So if you have multiple triggers, if one of them is true, it will trigger the automation.
Conditions can be changed with OR or AND logic if you specify it like you did, but if you dont, they work as AND.

That being said, your automation right now, will ALWAYS turn on the light 30 mins before sunset if you AND your wife is at home.
To fix this, you have to use your locations as TRIGGERS and the time as condition.
That will make it work and it will turn on the light when you or your wife comes home and only if it is 30 mins before sunset. :wink:

EDIT: I just noticed you want this to be 30 minutes before sunset and until, 21:30.
This doesnt change much. Just add both to the conditions… I just want to be specific :slight_smile:

argykaraz that’s very helpful thank you.

I’ll re-arrange it.

No it won’t. It Will do it if either or both are home. Did you not see this?

condition: or

I took
condition: **or**
out as it was giving an error…

I’ll re-write it and repost later.

Thanks all :slight_smile:

@tom_l Yes, I did… But I also saw this…

1 Like

Just thought I’d update this with the final automation. I’ll edit it down to a more simplified version at some point, I’d expanded it out for trouble shooting an issue I had and was hashing out the components of it until I could find what was causing the error.
apparently ‘not home’ is completely different from ‘not_home’ … that was a fun 30mins finding that one… :smile:

- id: 'one'
  alias: 'Porch light auto on'
  initial_state: true
  trigger:
    - platform: sun
      event: sunset
      offset: '-01:00:00'
  condition:
    condition: state
    entity_id: group.all_devices
    state: home
  action:
    service: light.turn_on
    entity_id: light.porch_light
    
- id: 'onea'
  alias: 'arrival home after sunset'
  initial_state: true
  trigger:
    - platform: state
      entity_id: device_tracker.honor_view10_cba3a944ee4b
      from: 'not_home'
      to: 'home'
    - platform: state
      entity_id: device_tracker.honor_9_45989bdec533b55e
      from: 'not_home'
      to: 'home'
    - platform: state
      entity_id: device_tracker.greg_s_phone
      from: 'not_home'
      to: 'home'
    - platform: state
      entity_id: device_tracker.clare_s_phone
      from: 'not_home'
      to: 'home'      
  condition:
    - condition: sun
      after: sunset
      after_offset: '-01:00:00'
    - condition: time
      before: '21:45:00'
  action:
    service: light.turn_on
    entity_id: light.porch_light 

Anyway this works for now.

So, basically you need only the first automation, since it turns on the light everyday 1 hour before sunset… The second one will be useless since the light will be already on from the first one, right? Unless you somehow turn the light off manually after the sunset.

Edit: But I just realized you turn it on when you are at home… So if you are away, it wont turn on…
So you need the second one, in case you come home after sunset… Yup… Good job.

I thought I should share my entrance light automations.
I dont have a yard. I love on the 3rd floor of a building with no main entrance. So anyone can come upstairs to my front door. There are no lights on the stairwell, except just outside my door. Turning on the light from sunset till morning means I will pay more on the bills, I had to come up with something to turn it on only when necessary.
So… here it is:

- alias: '[LIGHTS] Turn on Light'
  trigger:
  - platform: state
    entity_id: binary_sensor.main_entrance
    to: 'on'             //This will turn on the light when the entrance is open
  - platform: template
    value_template: '{% if states.proximity.argyristohome.attributes.dir_of_travel == "towards" and states.proximity.argyristohome.state|int < 1000 %}true{%else%}false{%endif%}'            //This will turn on when I am closer than 1km and going towards my house.
  - platform: template
    value_template: '{% if states.proximity.fotinitohome.attributes.dir_of_travel == "towards" and states.proximity.fotinitohome.state|int < 1000 %}true{%else%}false{%endif%}'    //Same here for my wife
  - platform: state
    entity_id:
      - device_tracker.1
      - device_tracker.2
      - device_tracker.3
      - device_tracker.4
      - device_tracker.5
      - device_tracker.6 // This is a whole list of my guests mobiles. If they have their wifi on and it connects to my network, it will turn on the light. Just in case I havent turned it on manually.
    to: 'home'
  condition:
    - condition: time
      after: '17:00:00'
      before: '09:00:00'
  action:
  - service: light.turn_on
    entity_id: light.entrance_light

And here is my automation to turn off the light.

- alias: '[LIGHTS] Turn off Light'
  trigger:
  - platform: time
    at: '09:00:00'
  - platform: state
    entity_id: alarm_control_panel.house
    to: 'armed_home'
  - platform: state
    entity_id: alarm_control_panel.house
    to: 'away'
  - platform: state
    entity_id: device_tracker.google_maps_1019
    from: 'home'
    to: 'not_home'
  - platform: state
    entity_id: device_tracker.google_maps_1168
    from: 'home'
    to: 'not_home'
  - platform: state
    entity_id: device_tracker.google_maps_1019
    from: 'not_home'
    to: 'home'
  - platform: state
    entity_id: device_tracker.google_maps_1168
    from: 'not_home'
    to: 'home'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: '{% if states.proximity.argyristohome.attributes.dir_of_travel != "towards" and states.proximity.argyristohome.state > "1000" %}true{%else%}false{%endif%}'
      - condition: template
        value_template: '{% if states.proximity.fotinitohome.attributes.dir_of_travel != "towards" and states.proximity.fotinitohome.state > "1000" %}true{%else%}false{%endif%}'
      - condition: state
        entity_id: binary_sensor.main_entrance
        state: 'off'
  action:
  - delay: '00:05:00'
  - service: light.turn_off

So basically, this will turn off the light five minutes after me or my wife get back home.
There are some exceptions. For example it wont turn off if my wife gets home, but I am on the way close to home. It will remain on until I get home.

I like the idea of the template that switches on 1km away, very nice. :smile:

Thanks for sharing the code.