If Else automation for motion and turn on switch/light

Hi. N00b here. Trying to do this via the Automation GUI but something is not right. When motion is cleared, it doesn’t trigger, only when motion is active. Thoughts? and Thanks in advance.

alias: Auto - See Kitchen in Dark
description:
trigger:
 - platform: state
   entity_id:
     - binary_sensor.ms_somewhere_motion_detection
condition:
 - condition: time
   before: "15:00:00"
   after: "09:00:00"
action:
 - if:
     - condition: state
       entity_id: binary_sensor.ms_somewhere_motion_detection
       state: "on"
   then:
     - service: light.turn_on
       data:
         brightness_pct: 20
       target:
         entity_id: light.under_cabinet_light
   else:
     - if:
         - condition: state
           entity_id: binary_sensor.ms_somewhere_motion_detection
           state: "off"
         - condition: and
           conditions:
             - condition: state
               entity_id: binary_sensor.ms_somewhere_motion_detection
               state: "off"
               for:
                 hours: 0
                 minutes: 0
                 seconds: 59
       then:
         - service: light.turn_off
           data: {}
           target:
             entity_id: light.under_cabinet_light
mode: single

It will only fire when motion is detected in the trigger. You could also add when motion is cleared in the trigger and handle that in your action-if section.

I think i follow but with the GUI tools available, i’m not sure how to implement that.


alias: Auto - See Kitchen in Dark
description:
trigger:
 - platform: state
   entity_id:
     - binary_sensor.ms_somewhere_motion_detection
condition:
 - condition: time
   before: "15:00:00"
   after: "09:00:00"
action:
 - if:
     - condition: state
       entity_id: binary_sensor.ms_somewhere_motion_detection
       state: "off"
       for:
         seconds: 59
   then:
     - service: light.turn_off
       target:
         entity_id: light.under_cabinet_light

   else:
     - service: light.turn_on
       data:
         brightness_pct: 20
       target:
         entity_id: light.under_cabinet_light


An if-then-else isn’t mandatory for this application.

alias: Auto - See Kitchen in Dark
description:
trigger:
 - platform: state
   entity_id: binary_sensor.ms_somewhere_motion_detection
   from: 'off'
   to: 'on'
 - platform: state
   entity_id: binary_sensor.ms_somewhere_motion_detection
   from: 'on'
   to: 'off'
   for:
     seconds: 59
condition:
 - condition: time
   before: "15:00:00"
   after: "09:00:00"
action:
 - service: light.turn_on
   data:
     brightness_pct: "{{ 20 if trigger.to_state.state == 'on' else 0 }}"
   target:
     entity_id: light.under_cabinet_light
mode: single
1 Like

Well, you have the experts above :slight_smile:

  1. While you are in the Automation Editor, click the overflow icon in the upper right corner (three vertical dots) and switch the Editor from Visual to YAML mode.

  2. Copy the example posted above and paste it into the Editor (overwrite whatever YAML may already be there)

  3. Click the Save button.

  4. Optionally switch the Editor back into Visual mode to see how the automation is presented.

1 Like

Thanks for that… but question:

So I noticed the two triggers are defined, but only 1 action is defined to turn on lights when “on” has occurred on the motion detector. How does the light turn off? I would assume an action for “off” must be defined.

Trying to understand the logic ofit all. I could be missing something. Appreciate the help.

When light.turn_on sets brightness_pct to 0.

Most lights turn off when their brightness is set to zero. If your light doesn’t then the automation will need an explicit light.turn_off service call.

1 Like