Optimized Code for Kitchen Lights Automation

Hi Guys,
here’s my automation for my Kitchen Counter Lights. Setup: Two Motion Sensors and two Switches that lights up the counters on the Left and right side of the Kitchen.
Wondering if there is a better way to achieve the same, like maybe optimize the code, use templates scripts, etc. That makes it faster and efficient.
I am still learning. Looking for some knowledge sharing from the pros. Thank you guys.

- alias: Counter Lights ON
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_1
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.motion_sensor_2
    from: 'off'
    to: 'on'
  condition:
    condition: numeric_state
    entity_id: sensor.illumination_1
    below: 10
  action:
    - service: switch.turn_on
      data:
        entity_id:
          - switch.plug_1
          - switch.plug_2

- alias: Counter Lights OFF 
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_1
    to: 'off'
    for:
      minutes: 2
  - platform: state
    entity_id: binary_sensor.motion_sensor_2
    to: 'off'
    for:
      minutes: 2
  - platform: time_pattern
    hours: "/1"
  condition:
    - condition: state
      entity_id: switch.plug_1
      state: 'on'
    - condition: state
      entity_id: switch.plug_2
      state: 'on' 
  action:
    - service: switch.turn_off
      data:
        entity_id:
          - switch.plug_1
          - switch.plug_2

First automation:

You can put both entities in one trigger and you don’t need “data” when you are only defining the entity ids.

Something like this:

- alias: Counter Lights ON
  trigger:
  - platform: state
    entity_id: 
      - binary_sensor.motion_sensor_1
      - binary_sensor.motion_sensor_2
    from: 'off'
    to: 'on'
  condition:
    condition: numeric_state
    entity_id: sensor.illumination_1
    below: 10
  action:
    - service: switch.turn_on
      entity_id:
        - switch.plug_1
        - switch.plug_2

The same goes for the second automation, you can combine the two triggers into one. I don’t understand why you are using a time pattern here to trigger the automation every hour? Let’s say at 14:58 motion was detected and light was turned on. Then at 15:00 the turn of automation triggers due to the time pattern trigger and turns off the lights, I don’t think that’s expected behaviour, is it?

- alias: Counter Lights OFF 
  trigger:
  - platform: state
    entity_id: 
      - binary_sensor.motion_sensor_1
      - binary_sensor.motion_sensor_2
    to: 'off'
    for:
      minutes: 2
  condition:
    - condition: state
      entity_id: switch.plug_1
      state: 'on'
    - condition: state
      entity_id: switch.plug_2
      state: 'on' 
  action:
    - service: switch.turn_off
      entity_id:
          - switch.plug_1
          - switch.plug_2

I’d also suggest giving your motion sensors and lights some more meaningful names instead of plug 1, plug 2 etc.

Thank you!!!
Iam using the time pattern as an additional step where due to some glitch if the lights are not turned off they are checked and turned off, like sometimes i noticed my kitchen lights were on all night.

It’s possible that your lights were left on because only one of the two plugs was on for some reason. By making a separate automation for each plug you may be able to get rid of the time pattern.

Another option is to make a trigger on the light being on for an hour (or whatever) and add a condition of the motion sensors being off. That way, at most, the lights will only run for an hour.

There are also some plugs that don’t always respond on the first command. Another thing you can do to ENSURE they turn off is to add a delay after turning them off, and then turn them off again just in case.

The only lights I have that sometimes end up left on when they should not be are my Hue lights. This is due to connectivity issues I have with the hue hub.

2 Likes

thank you, shall try this.
any chance you could write that piece of code. Cheers.