My take on motion/light automation with Xiaomi sensor

Hello everyone!

I would like to share with you my approach on light/sensor automation that I use here on my home.

The main idea is:

  1. turn the light on and off automatically
  2. wait until a programmable time is reached to automate the light
  3. if light is already on (manual input), do not turn off automatically (manual mode override)
  4. if light is turned off manually the automation stops and wait for the next trigger
  5. everything is on a single automation

This script uses the 'No motion since' attribute from the Xiaomi sensor.

I have a couple of routines automation using sunset and sunrise that turns on and off these lights automation created for every room.

Here is everything on a single package file:

automation:

  - alias: Bedroom Light
    trigger:
      platform: state
      entity_id: binary_sensor.motion_sensor
      to: 'on'
    condition:
      condition: state
      entity_id: light.bed_light
      state: 'off'
    action:
      - service: light.turn_on
        entity_id: light.bed_light
      - delay: 00:00:05 #delays five seconds so wait template can start properly
      - wait_template: >
          {% set light = "light.bed_light" -%} #set light entity
          {%- set timmer_input = "input_select.bedroom" -%} #set input select entity
          {%- set motion_sensor = "binary_sensor.motion_sensor" -%} #set motion sensor entity

          {%- if is_state(light , 'off') or (state_attr(motion_sensor, 'No motion since') | int >= states(timmer_input) | int and is_state(motion_sensor , 'off')) -%}true{%- else -%}false{%- endif %} #if light is off or no motion equals timmer value
      - condition: state #checks if light is still on
        entity_id: light.bed_light
        state: 'on'
      - service: light.turn_off
        entity_id: light.bed_light

  - alias: Routine - Sunset
    trigger:
      platform: sun
      event: sunset
      offset: '-00:15:00'
    action:
      - service: homeassistant.turn_on
        entity_id:
          - automation.bedroom_light
  
  - alias: Routine - Sunrise
    trigger:
      platform: sun
      event: sunrise
      offset: '-00:15:00'
    action:
      - service: homeassistant.turn_off
        entity_id:
          - automation.bedroom_light

input_select:

  bed_light_timmer:
    name: 'Bedroom'
    options:
     - 120
     - 180
     - 300
     - 600
     - 1200
    initial: 180
    icon: mdi:timer

Hope it helps!

9 Likes

This is great thanks for the share, you should really look into node red for these types of automations will make your automating life a lot easier to adjust if things need tweeting.

Thanks mate! I really have to learn node red… Have a great one!

  - alias: Routine - Sunset
    trigger:
      platform: numeric_state
      entity_id: sun.sun
      value_template: '{{ state.attributes.elevation | int }}'
      below: 3
    action:
      - service: homeassistant.turn_on
        entity_id:
          - automation.bedroom_light
  
  - alias: Routine - Sunrise
    trigger:
      platform: numeric_state
      entity_id: sun.sun
      value_template: '{{ state.attributes.elevation | int }}'
      above: 1
    action:
      - service: homeassistant.turn_off
        entity_id:
          - automation.bedroom_light

This doesn’t specify if the value was higher of lower before reaching the value_template. So what happens when the value reaches “2”? Both conditions are true in that case so both automations will be trigerred right?

It makes a lot of sense what you said. What I can say is that both automations never gave me any problems so far! Using it for months now. Very interesting.

Hello @Bob_NL!

Maybe this could work better?

  - alias: Routine - Sunset
    trigger:
      platform: sun
      event: sunset
      offset: '-00:15:00'
    action:
      - service: homeassistant.turn_on
        entity_id:
          - automation.bedroom_light
  
  - alias: Routine - Sunrise
    trigger:
      platform: sun
      event: sunrise
      offset: '-00:15:00'
    action:
      - service: homeassistant.turn_off
        entity_id:
          - automation.bedroom_light

Thanks.