Motion sensor - lights - turn on, but only if not allready turned on

Hi all!
Ok - this might be a bit simple, but i am fairly new to HA automations - as i have previously build all my automations in nodered.

I have build a simple automation, that turns on the outside light - when a motion is triggered,
it then waits for some time - and turns off the light again.
i have a condition that the sun should be down, to save some energy, and avoid turning on the lights during the day.

the automation mode is restart, so if people stay, the lights will keep on.

now - i want to to able to turn on the lights manually - if i want to.
this works - until motion is detected, and then it will turn of the lights.

so - my initial thought is to add a condition - that the light should be off.
is this the “right” way achieving this?

br Ronni

Personally I’d tackle this by requiring the light to be off, and then using a wait_for_trigger for the motion sensor being off for a given time. That’ll be cleaner than having the automation restart.

Something roughly like this:

trigger:
- platform: state
  entity_id: binary_sensor.motion
  to: 'on'
condition:
- condition: state
  entity_id: light.outside
  state: 'off'
action:
- service: light.turn_on
  target:
    entity_id: light.outside
- wait_for_trigger:
  - platform: state
    entity_id: binary_sensor.motion
    to: 'off'
    for: '00:05:00'
- service: light.turn_off
  target:
    entity_id: light.outside

Here’s another way to do the same thing.

  • Turns on light only if it’s currently off.
  • Turns off light only if it’s currently on.
alias: example 2
trigger:
- platform: state
  entity_id: binary_sensor.your_motion
  to: 'on'
- platform: state
  entity_id: binary_sensor.your_motion
  to: 'off'
  for: '00:05:00'
condition: "{{ (trigger.to_state.state == 'on' and is_state(light.your_light', 'off')) or (trigger.to_state.state == 'off' and is_state(light.your_light', 'on')) }}"
action:
- service: "light.turn_{{ trigger.to_state.state }}"
  target:
    entity_id: light.your_light

I’ve been trying the Entity Controller integration from HACS, which is designed for exactly this and makes automations much more compact. Working well so far. Excellent docs too.

Hi Tinkerer!
Thats the excact solution i was lookking.
the wait for trigger - is much cleaner.

thanks alot :slight_smile:

br Ronni

Hi stiltkjack - that looks nice to.
I might look into that at some time. As of now - this is also a “learning progress” for me - so i will stick with some basic automations first :slight_smile: