Motion detected -> lights on. no motion within 10mins, turn lights off

On/off are just the default states for my sensors. If yours are different you can just use

trigger:
    platform: state
    entity_id: your_sensor
    to: 'active'

yeah my defaults as active and inactive, i wanted to group them so i could say if group “active” then blah, but as a group it says unknown. argh well here’s mine as they currently stand

##########################################################################
################# Bathroom Motion Lights On ##################################
##########################################################################

- alias: "Motion Bathroom Lights On"
  trigger:
   platform: state
   entity_id: 'sensor.smartthings_xiaomi_bathroom_motion_sensor_detection'
   to: 'active'
  condition:
   condition: and
   conditions:
   - condition: state
     entity_id: light.bathroom
     state: 'off'
   - condition: or
     conditions:
      - condition: sun
        after: sunset
        after_offset: "-2:00:00"
      - condition: state
        entity_id: sun.sun
        state: below_horizon
      - condition: numeric_state
        entity_id: sensor.smartthings_xiaomi_bathroom_motion_sensor_lux
        below: 13
 action:
  - service: light.turn_on
    entity_id: light.bathroom

#########################################################################

- alias: "Motion Bathroom Lights Off"
  trigger:
   platform: state
   entity_id: 'sensor.smartthings_xiaomi_bathroom_motion_sensor_detection'
   to: 'inactive'
   for: 00:00:15
  condition:
   condition: state
   entity_id: light.bathroom
   state: 'on'
  action:
   service: light.turn_off
   entity_id: light.bathroomindent preformatted text by 4 spaces

this is something I’ve been meaning to find out… would you be willing to share the code for these turn on/off switches?
Thanks!
Marius

You mean to turn off the “turn off automation” from the frontend?

I just made a group that show the lamp controlled by the automation, and the two automation that handle turning ON and turning OFF:

office_light_viewing_group:
  control: hidden
  entities:
    - switch.table_lamp
    - switch.column_lamp
    - automation.turn_on_office_lights_when_there_is_movement
    - automation.turn_off_office_lights_at_end_of_timer

Here you can see the complete automation:

Click to see the complete automation and a screenshot of the frontend group
automation:
  - alias: Turn on office lights when there is movement
    trigger:
      - platform: mqtt
        topic: home/433toMQTT
        payload: 16026762
      - platform: mqtt
        topic: homesense/433toMQTT
        payload: 16026762
    condition:
      condition: and
      conditions:
        - condition: or
          conditions:
          - condition: sun
            after: sunset
            after_offset: "-0:30:00"
          - condition: sun
            before: sunrise
            before_offset: "0:30:00"
        - condition: or
          conditions:
          - condition: state
            entity_id: device_tracker.michaela
            state: 'home'
          - condition: state
            entity_id: device_tracker.me
            state: 'home'
    action:
      - service: timer.start
        entity_id: timer.timer_office
      - service: switch.turn_on
        entity_id: group.office_light
  - alias: Turn off office lights at end of timer
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.timer_office
    action:
      service: switch.turn_off
      entity_id: group.office_light

timer:
  timer_office:
    duration: '00:03:00'

And the customize part:

customize:
  automation.turn_on_office_lights_when_there_is_movement:
    friendly_name: Turn on automaticaly
    icon: mdi:lightbulb-on-outline
  automation.turn_off_office_lights_at_end_of_timer:
    friendly_name: Turn off automaticaly
    icon: mdi:lightbulb-outline

To achieve this entry:

And you can simply call this service, from another automation, to avoid having your light turning themselves off:

  - service: automation.turn_off
    entity_id: automation.turn_off_office_lights_at_end_of_timer
1 Like

a yes, the power of groups. So easy to forget that. thanks!

i just tried your method and it worked flawlessly! first i added this section of your code to my automations.yaml file

- alias: Light on if motion
  trigger:
    - platform: state
      entity_id: binary_sensor.motion
      to: 'on'
  action:
    - service: timer.start
      entity_id: timer.timer_lamp
    - service: switch.turn_on
      entity_id: switch.light
- alias: Turn off lights at end of timer
  trigger:
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.timer_lamp
  action:
    service: switch.turn_off
    entity_id: switch.light

then i added this section to my configuration.yaml

timer:
  timer_lamp:
    duration: '00:10:00'

restarted HA and it worked. amazing!
at first i was skeptical, because everything in my automations.yaml file starts out with - action
while yours started out with - alias
but as long as it works, it’s great!

many thanks!

1 Like

now that i understand how my motion sensor, lights, and timer works with HA on my Raspberry Pi, i will proceed with swapping out the codes for using HDMI CEC to power on the TV that’s connected to my Odroid C2. Hopefully this guide https://www.home-assistant.io/components/hdmi_cec/ works.

i have to install HA onto my C2 as well right? i was thinking maybe HA on the rpi will send the command to the C2 to power on the TV. but with my amateur coding skill, it’s probably best to just start over with another install of HA on C2.

The way I have mine set up, is if motion is detected in a room, the lights triggered in that automation will stay on until another motion sensor is triggered in a different room, so my lights follow me around the house. Or the sun rises or I leave the house, or whatever reason.

This is pure gold, thanks!

Personally I like my lights automated with just one automation for turning on and off, here’s mine:

- id: Automatic_kitchen_light
  alias: Automatic kitchen light
  trigger:
  - platform: state
    entity_id: binary_sensor.motionsensor_kitchen
    to: 'on'
  - platform: state
    entity_id: binary_sensor.motionsensor_kitchen
    to: 'off'
    for:
      minutes: 1
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: group.all_devices
      state: 'home'
    - condition: or
      conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: binary_sensor.motionsensor_kitchen
          state: 'on'
        - condition: numeric_state
          entity_id: sensor.luxsensor_kitchen
          below: 100
      - condition: state
        entity_id: binary_sensor.motionsensor_kitchen
        state: 'off'
  action:
    service_template: >
      {% if trigger.to_state.state == 'on' %}
      light.turn_on
      {% elif trigger.to_state.state == 'off' %}
      light.turn_off
      {% endif %}
    data_template:
      transition: 1
    entity_id: light.kitchen

This automation will:

  • turn on my kitchen lights when: motion is detected, someones at home, light level below 100 lux.
  • turn off my kitchen lights when: no motion is detected for 1 minute

I use service templates a lot to have multiple automations in one.

might be safer to use:

  action:
    service_template: >
      {% if trigger.to_state.state == 'on' %}
      light.turn_on
      {% else %}
      light.turn_off
      {% endif %}

to be sure no unknown or no state stops the automation .

you might even try:

  action:
    service_template: >
      light.turn_{{ trigger.to_state.state}}

and add a condition_template for the trigger.to_state.state to be in ['on,'off] (again to assure a correct state value)

1 Like

Thanks, I will be using the light.turn_{{ trigger.to_state.state}}. I wanted to use that method for other automations but couldn’t get it working but your example worked!

I have this weird hype when I use as few lines as possible for automations

I just put together a single script that controls motion lights for 5 rooms. I’m pretty happy to condense the number of automations.

  - id: motion_lights
    alias: Motion Lights
    trigger:
      - platform: state
        entity_id: 
          - binary_sensor.computer_room_motion
          - binary_sensor.living_room_motion
          - binary_sensor.kitchen_motion_sensor
          - binary_sensor.dining_room_motion_sensor
          - binary_sensor.craft_room_motion_sensor
        to: 'on'
      - platform: state
        entity_id:
          - binary_sensor.computer_room_motion
          - binary_sensor.living_room_motion
          - binary_sensor.kitchen_motion_sensor
          - binary_sensor.dining_room_motion_sensor
          - binary_sensor.craft_room_motion_sensor
        to: 'off'
        for:
          minutes: 15
    condition:
      ### Ensure Motion Lights aren't disabled
      - condition: state
        entity_id: input_boolean.block_all_motion_lights
        state: 'off'
    action:
      ### Determine light/group that goes with motion detector
      - service_template: 'homeassistant.turn_{{trigger.to_state.state}}'
        data_template:
          entity_id: >
            {% set trigger_entity = trigger.entity_id %}
            {% if trigger_entity == 'binary_sensor.computer_room_motion' %}
              group.computer_room
            {% elif trigger_entity == 'binary_sensor.living_room_motion' %}
              switch.living_room_light
            {% elif trigger_entity == 'binary_sensor.kitchen_motion_sensor' %}
              group.kitchen
            {% elif trigger_entity == 'binary_sensor.dining_room_motion_sensor' %}
              group.dining_room
            {% elif trigger_entity == 'binary_sensor.craft_room_motion_sensor' %} 
              group.craft_room
            {% else %}
              light.gateway_light
            {% endif %}
2 Likes

I (still newbee) like to use your script.

What is your final ‘action:’ part now?

if you like shorter:

  - service_template: 'homeassistant.turn_{{trigger.to_state.state}}'
    data_template:
      entity_id: >
        {% set t = trigger.entity_id %}
        {% set mapper = {'binary_sensor.computer_room_motion' : 'group.computer_room',
               'binary_sensor.living_room_motion' : 'switch.living_room_light',
               'binary_sensor.kitchen_motion_sensor' : 'group.kitchen',
               'binary_sensor.dining_room_motion_sensor':'group.dining_room',
               'binary_sensor.craft_room_motion_sensor': group.craft_room} 
          %} 
        {{ mapper[t] if t  in mapper else 'light.gateway_light'}}

not sure if the name trigger_entity isnt a bit confusing, too close to the reserved trigger.entity_id, and not much shorter. Better name it t then :wink: way shorter…

–edit-- missed a %} in the template above, edited it to be complete.

2 Likes

HI,
He could use the same as in his original action. I only suggested shortening the service part of it, the data_template remains the same

@Mariusthvdb: Thanks.

@maxdaniel: I learn best by examples, like your approach.
Would you like to share your final solution (with the, for you, working suggestions)?

Thank you all very much!!

Well the examples given probably work best and also allow for multiple motion sensors but I currently only have one room light automated. I just used @Mariusthvdb service template so my automation now looks like:

- id: Automatic_kitchen_light
  alias: Automatic kitchen light
  trigger:
  - platform: state
    entity_id: binary_sensor.motionsensor_kitchen
    to: 'on'
  - platform: state
    entity_id: binary_sensor.motionsensor_kitchen
    to: 'off'
    for:
      minutes: 1
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: group.all_devices
      state: 'home'
    - condition: or
      conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: binary_sensor.motionsensor_kitchen
          state: 'on'
        - condition: numeric_state
          entity_id: sensor.luxsensor_kitchen
          below: 100
      - condition: state
        entity_id: binary_sensor.motionsensor_kitchen
        state: 'off'
  action:
    service_template: >
      light.turn_{{ trigger.to_state.state}}
    data_template:
      transition: 1
    entity_id: light.kitchen

for conditions: I need someone to be home because I have pets that trigger motion sensors when no one is home. Also sometimes when the light in the kitchen turns on the light sensor gives a high light value so I need to makes sure it will turn off even with higher light levels. So thats why I have so many conditions.

Perfect! Thanks a lot!!

@touliloup - Thanks for all the feedback provided. Been very helpful.

I’m still new to writing automation routines and have all my routines in the automation.yaml file which totals over 15 now.

When I incorporate the timer bit of code, there is an error in the homeassistant.log file.

ERROR:homeassistant.util.yaml:while parsing a block collection
  in "/config/automation.yaml", line 4, column 1
expected <block end>, but found '?'
  in "/config/automation.yaml", line 162, column 1
Failed config
  General Errors: 
- Error loading /config/configuration.yaml: while parsing a block collection
  in "/config/automation.yaml", line 4, column 1
expected <block end>, but found '?'
  in "/config/automation.yaml", line 162, column 1
Successful config (partial) 

Line 4 is the start of the first automation routine that starts with
- alias
and line 162 is the timer code
timer:

Do you or anyone else have any ideas on how to workaround this error?

Thanks in advance.