Automation Feedback: exterior lighting automations w/ nested conditions and multiple trigger types

I just finished writing a series of automations that control multiple exterior lights using a combination of time/sun and state triggers.

Overall, I wanted to see what you guys think of how I approached my automation goals (outlined below); would you have done it differently/ are there any opportunities for me to improve my code, etc. More specifically, I want to confirm if my conditional statements for the motion automations are nested correctly.

Four Exterior Lights:

  1. Patio String Lights
    • main light source for the backyard
    • switch.isy_patio_string_lights
  2. Tree Accent Lights
    • secondary accent lights
    • switch.isy_tree_string_lights
  3. Turkish Lamp
    • secondary accent light
    • switch.isy_turkish_lamp
  4. Front Door Light
    • main light source for entryway
    • switch.isy_front_door_light

Goals:

  • Turn on all exterior lights at sunset
  • Turn off Patio String Lights at 10 pm on weekdays, and at 1 am on weekends
  • Turn on Patio String Lights for 10 minutes if motion is detected after the turn-off times from previous automation
  • Turn off all exterior lights at sunrise

I just checked my configuration and I’m getting the error below, I think it’s because I’m using the time platform plus the sun platform under one trigger but

Invalid config for [automation]: Invalid time specified: sunrise for dictionary value @ data['condition'][0]['conditions'][0]['conditions'][1]['before']. Got None. (See ?, line ?). 
Invalid config for [automation]: Invalid time specified: sunrise for dictionary value @ data['condition'][0]['conditions'][0]['conditions'][1]['before']. Got None. (See ?, line ?). 

Now for the automations:

# TURN ON ALL EXTERIOR LIGHTS AT SUNSET
  - id: all_outside_lights_on
    alias: 'Backyard Lights On at Sunset'
    trigger:
      platform: sun
      event: sunset
      offset: "-00:45:00"
    action:
      - service: switch.turn_on
        entity_id: switch.isy_patio_string_lights
      - service: switch.turn_on
        entity_id: switch.isy_tree_string_lights
      - service: switch.turn_on
        entity_id: switch.isy_turkish_lamp
      - service: switch.turn_on
        entity_id: switch.isy_front_door_light

# TURN OFF PATIO STRING LIGHTS:
  ## Weekdays at 10pm
  - id: weekday_patio_string_lights_off_at_10pm
    alias: 'Patio Lights Off at 10pm'
    trigger:
      platform: time
      at: "22:00:00"
    condition:
      condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - sun
    action:
      - service: switch.turn_off
        entity_id: switch.isy_patio_string_lights

  ## Weekends at 1am
  - id: weekend_patio_string_lights_off_at_1am
    alias: 'Patio Lights Off at 1am'
    trigger:
      platform: time
      at: "01:00:00"
    condition:
      condition: time
      weekday:
        - fri
        - sat
    action:
      - service: switch.turn_off
        entity_id: switch.isy_patio_string_lights



# TURN ON PATIO STRING LIIGHTS IF MOTION IS DETECTED 
  - id: turn_on_outside_lights_after_motion
    alias: ' Motion Outside Lights'
    trigger:
      platform: state
      entity_id: binary_sensor.hue_outside_sensor_motion
      to: 'on'
    condition:
      condition: or
      conditions:
        - condition: and
          conditions:
            - condition: time
              weekday:
                - mon
                - tue
                - wed
                - thu
                - sun
            - condition: time
              after: '22:00:00'
              before: sunrise
        - condition: and
          conditions:
            - condition: time
              weekday:
                - fri
                - sat
            - condition: time
              after: '00:02:00'
              before: sunrise
    action:
      - service: switch.turn_on
        entity_id: switch.isy_patio_string_lights

# TURN OFF PATIO STRING LIIGHTS IF LIGHT HAS BEEN ON FOR 10 MINUTES AND NO MOTION IS DETECTED 
  - id: turn_off_outside_lights_after_no_motion
    alias: 'No Motion Outside Lights Off'
    trigger:
      platform: state
      entity_id: switch.isy_patio_string_lights
      to: 'on'
      from: 'off'
      for:
        hours: 0
        minutes: 10
        seconds: 0
    condition:
      condition: or
      conditions:
        - condition: and
          conditions:
            - condition: time
              weekday:
                - mon
                - tue
                - wed
                - thu
                - sun
            - condition: time
              after: '22:00:00'
              before: sunrise
            - condition: state
              entity_id: binary_sensor.hue_outside_sensor_motion
              state: 'off'
        - condition: and
          conditions:
            - condition: time
              weekday:
                - fri
                - sat
            - condition: time
              after: '00:02:00'
              before: sunrise
            - condition: state
              entity_id: binary_sensor.hue_outside_sensor_motion
              state: 'off'
    action:
      - service: switch.turn_off
        entity_id: switch.isy_patio_string_lights

# TURN OFF ALL EXTERIOR LIGHTS AT SUNRISE
  - id: all_outside_lights_off
    alias: 'Backyard Lights Off at Sunrise'
    trigger:
      platform: sun
      event: sunrise
      offset: "-00:45:00"
    action:
      - service: switch.turn_off
        entity_id: switch.isy_patio_string_lights
      - service: switch.turn_off
        entity_id: switch.isy_tree_string_lights
      - service: switch.turn_off
        entity_id: switch.isy_turkish_lamp
      - service: switch.turn_off
        entity_id: switch.isy_front_door_light

You shouldn’t include hours and seconds here:

    trigger:
      platform: state
      entity_id: switch.isy_patio_string_lights
      to: 'on'
      from: 'off'
      for:
        hours: 0
        minutes: 10
        seconds: 0

Just the minutes.

Other than that it looks good.

1 Like

Thanks! I removed the h & s.

I was able to fix the configuration error I was receiving by removing the “before: sunrise” condition that was after the time condition, and adding a new sun state condition before the time.

# TURN ON PATIO STRING LIGHTS IF MOTION IS DETECTED 
  - id: turn_on_outside_lights_after_motion
    alias: ' Motion Outside Lights'
    trigger:
      platform: state
      entity_id: binary_sensor.hue_outside_sensor_motion
      to: 'on'
    condition:
      condition: or
      conditions:
        - condition: and
          conditions:
            - condition: time
              weekday:
                - mon
                - tue
                - wed
                - thu
                - sun
            - condition: state
              entity_id: sun.sun
              state: 'below_horizon'
            - condition: time
              after: '22:00:00'
        - condition: and
          conditions:
            - condition: time
              weekday:
                - fri
                - sat
            - condition: state
              entity_id: sun.sun
              state: 'below_horizon'
            - condition: time
              after: '00:02:00'
    action:
      - service: switch.turn_on
        entity_id: switch.isy_patio_string_lights

# TURN OFF PATIO STRING LIGHTS IF LIGHT HAS BEEN ON FOR 10 MINUTES AND NO MOTION IS DETECTED 
  - id: turn_off_outside_lights_after_no_motion
    alias: 'No Motion Outside Lights Off'
    trigger:
      platform: state
      entity_id: switch.isy_patio_string_lights
      to: 'on'
      from: 'off'
      for:
        minutes: 10
    condition:
      condition: or
      conditions:
        - condition: and
          conditions:
            - condition: time
              weekday:
                - mon
                - tue
                - wed
                - thu
                - sun
            - condition: state
              entity_id: sun.sun
              state: 'below_horizon'
            - condition: time
              after: '22:00:00'
            - condition: state
              entity_id: binary_sensor.hue_outside_sensor_motion
              state: 'off'
        - condition: and
          conditions:
            - condition: time
              weekday:
                - fri
                - sat
            - condition: state
              entity_id: sun.sun
              state: 'below_horizon'
            - condition: time
              after: '00:02:00'
            - condition: state
              entity_id: binary_sensor.hue_outside_sensor_motion
              state: 'off'
    action:
      - service: switch.turn_off
        entity_id: switch.isy_patio_string_lights

Does this make sense to do it this way? At least it’s passing the configuration checker now.

If you want to, you can reduce this:

    action:
      - service: switch.turn_on
        entity_id: switch.isy_patio_string_lights
      - service: switch.turn_on
        entity_id: switch.isy_tree_string_lights
      - service: switch.turn_on
        entity_id: switch.isy_turkish_lamp
      - service: switch.turn_on
        entity_id: switch.isy_front_door_light

to this:

    action:
      - service: switch.turn_on
        entity_id: 
          - switch.isy_patio_string_lights
          - switch.isy_tree_string_lights
          - switch.isy_turkish_lamp
          - switch.isy_front_door_light
1 Like

@123 Exactly the type of feedback I was looking for!

Thank you!