A challenge: can you reduce this automation?

This single automation turns lights on and off with a switch and 2 motion sensors.

Can you reduce it any further while still retaining the functionality?

- alias: Office switch
  trigger:
    platform: state
    entity_id: switch.office_fan_light, binary_sensor.office_motion, binary_sensor.office_sensor_8
  action:
    service_template: >
      {% if trigger.to_state.state == 'on' %}
        script.office_lights_on
      {% elif 'switch' in trigger.to_state.entity_id %}
        script.office_lights_off
      {% else %}
        script.noop
      {% endif %}

It uses a Wemo light switch, an Aeotec MultiSensor, and a Wemo Motion Sensor to turn on and off my office lights.

Turning on and off the switch will turn on and off a bunch of lights controlled by the office_lights_on and office_lights_off scripts.

When motion is sensed, it starts a timer from the office_lights_on script, but the Wemo Motion Sensor quickly goes into the ‘off’ state, which we don’t want to turn of the lights.

The noop script is needed to prevent errors from the service_template potentially not resolving to anything at all. Currently it looks like this:

noop:
  alias: "noop"
  sequence:
    delay: 00:00:00

Is there an actual built-in NOOP service that could be used instead?

3 Likes

45 views, and nobody can optimize this so it’s even smaller or more efficient? Come on guys and gals…

I think what you have is as optimized as it gets using YAML for your automation, hence the lack of replies.:wink:

BTW, love you usage of the noop script to prevent log errors, I’m going to have to borrow that as I have looked into the best way to handle those situations before without any luck.

1 Like

That’s clever. Thanks for sharing, @forbin. :slight_smile: