Lights activated by motion or by switch

Doing a search, I found a bunch of threads discussing this problem, but none seemed to have satisfactory solutions so I decided to post mine.

I have a set of lights out the front, and they can be turned on/off from a switch inside. I also installed a motion sensor, and wanted them to turn on with motion, then turn off after 5 minutes of no motion.

The other thing is, that if the lights are turned on from the switch, I don’t want them to turn off based on no motion.

Here is my solution, basically I keep a input boolean to hold state if the light is turned on by motion, and then only turn the lights off with no motion if this boolean is true.

- alias: Front motion - lights on
  trigger:
    platform: state
    entity_id: binary_sensor.front_motion
    state: 'on'
  condition:
    condition: and
    conditions:
      - condition: sun
        after: sunset
        after_offset: -00:45:00
      - condition: state
        entity_id: light.entry_lights
        state: 'off'
  action:
    service: homeassistant.turn_on
    entity_id: 
      - input_boolean.entrance_light_motion_activated
      - light.entry_lights

- alias: Front motion - lights off
  trigger:
    platform: state
    entity_id: binary_sensor.front_motion
    state: 'off'
    for:
      minutes: 5
  condition:
    condition: state
    entity_id: input_boolean.entrance_light_motion_activated
    state: 'on'
  action:
    service: homeassistant.turn_off
    entity_id: 
      - light.entry_lights
      - input_boolean.entrance_light_motion_activated
      
- alias: Front light - lights off
  trigger:
    platform: state
    entity_id: light.entry_lights
    state: 'off'
  action:
    service: homeassistant.turn_off
    entity_id: input_boolean.entrance_light_motion_activated
17 Likes

An explanation of the automations:

“Front motion - lights on”

  • Turns the lights on after sunset, and assuming the lights arent already on
  • Reasoning for the condition of lights not being on, is because I don’t want to trigger if someone manually hits the switch because then the lights would turn off

“Front motion - lights off”

  • Turns the lights off with no motion for 5 minutes.
  • With with a condition that the light was turned on by motion, so it doesnt trigger if someone manually turned on the lights.

“Front light - lights off”

  • This is triggered if someone manually switches off the lights.
  • I clear the motion mode in case the lights were turned on with motion and not manually.
3 Likes

just want to say thank you, this works perfectly!

Glad it worked out @jansepke. I am still running this logic and am super happy with the way it works. The only issue I have is the motion sensor seems to have a couple of seconds delay so the lights aren’t as snappy as I want them.

Hi thanks for your contribution, I am trying to achieve the same automation but having problems. I have added the following to the configuration.yaml:

input_boolean:
  entrance_light_motion_activated:
    name: Entrance Light Motion Activated
    initial: off
    icon: mdi:spotlight

Also I have changed the entitys to match my devices in the automation.yaml:

PIR
binary_sensor.front_motion
binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor

Light
light.entry_lights
light.bar

- alias: Front motion - lights on
  trigger:
    platform: state
    entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
    state: 'on'
  condition:
    condition: and
    conditions:
      - condition: sun
        after: sunset
        after_offset: -00:45:00
      - condition: state
        entity_id: light.bar
        state: 'off'
  action:
    service: homeassistant.turn_on
    entity_id: 
      - input_boolean.entrance_light_motion_activated
      - light.bar

- alias: Front motion - lights off
  trigger:
    platform: state
    entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
    state: 'off'
    for:
      minutes: 5
  condition:
    condition: state
    entity_id: input_boolean.entrance_light_motion_activated
    state: 'on'
  action:
    service: homeassistant.turn_off
    entity_id: 
      - light.bar
      - input_boolean.entrance_light_motion_activated
      
- alias: Front light - lights off
  trigger:
    platform: state
    entity_id: light.bar
    state: 'off'
  action:
    service: homeassistant.turn_off
    entity_id: input_boolean.entrance_light_motion_activated

Is there anything obvious I am doing wrong?

Thanks!

It what way doesnt it work? Do the lights not go on when motion is detected, lights not go off automatically, or the manual settings doesnt work?

At first glance it looks good, the only issue I could see is that the motion binary sensor isn’t toggling, but you can check that by looking in the logs.

I get errors in the logs, I have tried to work it out all day without success! I even removed all other automatons to rule that out.

Log Details (ERROR)
Tue Jan 02 2018 18:55:55 GMT+0000 (GMT Standard Time)

Testing configuration at /config
2018-01-02 18:55:53 INFO (MainThread) [homeassistant.setup] Setting up introduction
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain introduction took 0.8 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up recorder
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up http
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain recorder took 0.0 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain http took 0.0 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up history
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up system_log
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up websocket_api
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up api
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain history took 0.0 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain system_log took 0.0 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain websocket_api took 0.0 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain api took 0.0 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up frontend
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain frontend took 0.0 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up discovery
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up group
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up telegram_bot
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up emulated_hue
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up input_boolean
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up map
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up zwave
2018-01-02 18:55:54 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: [state] is an invalid option for [automation]. Check: automation->trigger->0->state. (See /config/configuration.yaml, line 114). Please check the docs at https://home-assistant.io/components/automation/
2018-01-02 18:55:54 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: [state] is an invalid option for [automation]. Check: automation->trigger->0->state. (See /config/configuration.yaml, line 114). Please check the docs at https://home-assistant.io/components/automation/
2018-01-02 18:55:54 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: [state] is an invalid option for [automation]. Check: automation->trigger->0->state. (See /config/configuration.yaml, line 114). Please check the docs at https://home-assistant.io/components/automation/
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up sun
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up input_select
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up xiaomi_aqara
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up updater
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up sensor
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setting up notify
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain discovery took 0.3 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain group took 0.3 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain telegram_bot took 0.2 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain emulated_hue took 0.2 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain input_boolean took 0.2 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain map took 0.2 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain zwave took 0.1 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain sun took 0.1 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain input_select took 0.1 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain xiaomi_aqara took 0.1 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain updater took 0.1 seconds.
2018-01-02 18:55:54 INFO (MainThread) [homeassistant.setup] Setup of domain sensor took 0.1 seconds.
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setup of domain notify took 0.1 seconds.
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setting up tts
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setting up config
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setting up logbook
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setting up camera
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setting up conversation
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setting up remote
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setting up script
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setting up automation
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setting up light
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setup of domain tts took 0.0 seconds.
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setup of domain config took 0.0 seconds.
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setup of domain logbook took 0.0 seconds.
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setup of domain camera took 0.0 seconds.
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setup of domain conversation took 0.0 seconds.
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setup of domain remote took 0.0 seconds.
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setup of domain script took 0.0 seconds.
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setup of domain automation took 0.0 seconds.
2018-01-02 18:55:55 INFO (MainThread) [homeassistant.setup] Setup of domain light took 0.0 seconds.
Failed config
  automation: 
    - action: [source /config/automations.yaml:15]
        entity_id: [source /config/automations.yaml:17]
          - input_boolean.entrance_light_motion_activated
          - light.bar
        service: homeassistant.turn_on
      alias: Front motion - lights on
      condition: [source /config/automations.yaml:6]
        condition: and
        conditions: [source /config/automations.yaml:8]
          - after: sunset
            after_offset: -00:45:00
            condition: sun
          - condition: state
            entity_id: light.bar
            state: off
      trigger: [source /config/automations.yaml:2]
        platform: state
        entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
        state: on
    - action: [source /config/automations.yaml:32]
        entity_id: [source /config/automations.yaml:34]
          - light.bar
          - input_boolean.entrance_light_motion_activated
        service: homeassistant.turn_off
      alias: Front motion - lights off
      condition: [source /config/automations.yaml:28]
        condition: state
        entity_id: input_boolean.entrance_light_motion_activated
        state: on
      trigger: [source /config/automations.yaml:22]
        platform: state
        entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
        for: [source /config/automations.yaml:26]
          minutes: 5
        state: off
    - action: [source /config/automations.yaml:43]
        entity_id: input_boolean.entrance_light_motion_activated
        service: homeassistant.turn_off
      alias: Front light - lights off
      trigger: [source /config/automations.yaml:39]
        platform: state
        entity_id: light.bar
        state: off

Successful config (partial)
  automation:

Ah, there was a change in the scripting language, here is my modified version to fix that error. I think specifically the trigger for the front_motion_lights_off needs updating:

- id: front_motion_lights_on
  alias: Front motion - lights on
  trigger:
    - platform: state
      entity_id: binary_sensor.front_motion
      to: 'on'
  condition:
    condition: and
    conditions:
    - condition: sun
      after: sunset
      after_offset: -00:30:00
    - condition: state
      entity_id: light.front_lights
      state: 'off'
  action:
    service: homeassistant.turn_on
    entity_id:
      - light.front_lights
      - input_boolean.entrance_light_motion_activated

- id: front_motion_lights_off
  alias: Front motion - lights off
  trigger:
    platform: state
    entity_id: binary_sensor.front_motion
    to: 'off'
    for:
      minutes: 5
  condition:
    condition: state
    entity_id: input_boolean.entrance_light_motion_activated
    state: 'on'
  action:
    service: homeassistant.turn_off
    entity_id:
      - light.front_lights
      - input_boolean.entrance_light_motion_activated
    
- id: front_light_off
  alias: Front light - lights off
  trigger:
    platform: state
    entity_id: light.front_lights
    to: 'off'
  action:
    service: homeassistant.turn_off
    entity_id: input_boolean.entrance_light_motion_activated
1 Like

Thanks so much for your help! It seems to be working :slight_smile: I was having some problems initially until I worked out after sunset is only until midnight. I was up late at the time trying to get it to work at until 2am lol

I guess I will to change it to this

conditions:
    - condition: sun
      after: sunset
      after_offset: -00:30:00
      - condition: sun
        before: sunrise
    - condition: state
      entity_id: light.front_lights
      state: 'off'

Hi @leafkicker,

I’m not sure what you are trying to do with the additional condition. The current condition means that it will only trigger if its later than 30 minutes before sunset, basically I only want it to happen at night. It automatically stops working at sunrise so the additional condition isn’t needed.

Read here and look at the sun conditions table.

Definitlely get it working with the simplest logic first (so remove the conditional statements). Then you can add them in once you know the basics are working. Took me a while to get my head around things.

Check this out:

“After sunset is from sunset to midnight. Before sunrise is from midnight to sunrise.”

From documentation, they are both the same thing, from sunset to sunrise. I don’t know if I have ever been out after midnight to test this though :slight_smile:

command night at sunrise daytime at sunset
after: sunset True False
before: sunrise True False

From what I found it does not work after midnight :grinning:

1 Like

I went with this in the end, works perfectly :slight_smile:

- id: front_motion_lights_on
  alias: Front motion - lights on
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d0001e43b31
      to: 'on'
  condition:
    condition: and
    conditions:
    - condition: template
      value_template: '{{ states.sun.sun.attributes.elevation < 0 }}'
    - condition: state
      entity_id: light.fibaro_system_fgd212_dimmer_2_level
      state: 'off'
  action:
    service: homeassistant.turn_on
    entity_id:
      - light.fibaro_system_fgd212_dimmer_2_level
      - input_boolean.entrance_light_motion_activated

- id: front_motion_lights_off
  alias: Front motion - lights off
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d0001e43b31
    to: 'off'
    for:
      minutes: 2
  condition:
    condition: state
    entity_id: input_boolean.entrance_light_motion_activated
    state: 'on'
  action:
    service: homeassistant.turn_off
    entity_id:
      - light.fibaro_system_fgd212_dimmer_2_level
      - input_boolean.entrance_light_motion_activated
    
- id: front_light_off
  alias: Front light - lights off
  trigger:
    platform: state
    entity_id: light.fibaro_system_fgd212_dimmer_2_level
    to: 'off'
  action:
    service: homeassistant.turn_off
    entity_id: input_boolean.entrance_light_motion_activated
1 Like

I have a similar twist on this problem here…

This is what I have:
Garage where it does not matter if sun are shining or not.
Qubino Relay for controlling the lights (from a push-button).
Door-sensor.

Now, my idea was to automatically turn on the light for 10 min after door opens. But I want to have a way to make the lights stay on OR off, if I manually push the button… But I come to realize it isn’t so easy to do this…

When The light are on (by door sensor) I can push the button and turn it off immediately, turning it on can also be achieved (setting a input_bool to keep track of manually or automatic changes), but it will require to turn the light off and then on…