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

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.

You might want to declare a separate timer.yaml file if the automation.yaml file was included as automation.

I moved the following into a file called timer.yaml

timer:
  timer_lamp:
    duration: '00:00:25'

The timer.yaml file is found under the config folder along with the configuration.yaml and automation.yaml files.

When looking into the logs since the time light wasn’t turning off after 25 seconds where no motion had been seen within that timeframe, I am seeing an error message of:

2018-11-10 15:31:02 WARNING (MainThread) [homeassistant.core] Unable to find service timer/start

The automation for that is associated with the timer looks like:

- alias: "Motion Detected"
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d0001d571ad
      to: 'on'
  action:
    - service: timer.start
      entity_id: timer.timer_lamp
    - service: light.turn_on
      entity_id: light.ge_14294_inwall_smart_dimmer_level
- alias: "Turn off lights at end of timer"
  trigger: 
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.timer_lamp
  action:
    service: light.turn_off
    entity_id: light.ge_14294_inwall_smart_dimmer_level

Is there something I need to put in the configuration.yaml file to add a timer component in order to call that in the automation?

Thanks for the help all!

I was actually able to solve the issue I encountered by moving the time code snippet into the configuration.yaml file.

1 Like

Yes that work too. If you want to keep it in a separate file, just look in your config file how you included the automation file and do the same with a timer file.

:+1:

Thanks again for all the help.

Now to figure out how to adjust the Xiaomi Motion Sensor polling issue.

1 Like

Hi Code works perfectly just want to add one important condition in my automation.

if switch is manually on (without motion) then it should not turn off after X minutes. Can some one help me how to achieve the same in my existing automation. Below is my code.

alias: Fully automated garden lights
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.network_video_recorder_motion_2
    to: 'on'
  - platform: state
    entity_id: binary_sensor.network_video_recorder_motion_2
    to: 'off'
    for:
      hours: 0
      minutes: 3
      seconds: 0
condition:
  - condition: time
    after: '18:00:00'
    before: '05:30:00'
action:
  - service_template: switch.turn_{{ trigger.to_state.state }}
    entity_id: switch.sonoff_10010ebac5
mode: single