Xiaomi motion sensor / HUE / Turn lights on motion and dim after time period

Hi all, newb Home assistant user here and trying to set up my first automations.

Have used the Xiaomi / Aqara gateway and sensors for some time with a few Yeelights, but want to dive further in home automation and also control my Philips Hue lights with these nice Xiaomi sensors.

So I’ve set up a Raspberry Pi with Home Assistant and got a few automations running correct after finding a few samples here on the forum.

What I’m trying to do is this:

  1. Have one or more lights turn on after movement, auto turn off after a few minutes of no movement.

  2. Have one or more lights turn on after movement, auto dim the lights after a few minutes of no movement.

So I have the first option running correct as far as I can tell, the lights go on when I enter the room and turn off just fine after a few minutes. Even if I use my Hue remote to change the White colour temperature (the lights always turn on than) they will return to an off state in time.

I’ve had to use 2 automations for this, maybe there’s a better way but at least it’s working:

- alias: Group berging ON motion
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d0001d9267e
      to: 'on'
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d0001d9267e
      to: 'off'
      for:
        minutes: 1
        seconds: 30
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      homeassistant.turn_on
      {% elif trigger.to_state.state == "off" %}
      homeassistant.turn_off
      {% endif %}
    entity_id: light.berging
    data_template:
      entity_id: light.berging
      brightness: 254

And:

- alias: Turn off group berging
  trigger:
    platform: state
    entity_id: light.berging
    to: 'on'
    for:
      minutes: 5
  action:
    service: homeassistant.turn_off
    entity_id: light.berging

Now the second option that I would like for other rooms - > So the lights should not turn off, but dim instead:

This example that I managed to put together (lol) seems to work fine for 1 or 2 times, but as soon as I use the Hue remote for example to change the White color temperature for a second, I get the problem that the lights will turn ON and stay in that state. So they do not dim to “1” again as I would like.

- alias: Group hal 1 ON motion
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d0001644e02
      to: 'on'
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d0001644e02
      to: 'off'
      for:
        minutes: 1
        seconds: 30
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      homeassistant.turn_on
      {% elif trigger.to_state.state == "off" %}
      homeassistant.turn_off
      {% endif %}
    entity_id: light.hal_spot_1
    data_template:
      entity_id: light.hal_spot_1
      brightness: 254

And:

- alias: Set level light hal 1
  trigger:
    platform: state
    entity_id: light.hal_spot_1
    to: 'on'
    for:
      minutes: 5
  action:
    service: homeassistant.turn_on
    entity_id: light.hal_spot_1
    data:
      brightness: 1

Could any of you HASSIO experts look at this and maybe give me a few directions on how to properly set this up?

Again I’m total newbee here so any help is highly appreciated !!

Cheers,’

H4ns

Oh yeah, I do also get a few errors in the Home Assistant log:

Invalid service data for light.turn_off: extra keys not allowed @ data[‘brightness’]. Got '254

Invalid service data for light.turn_off: extra keys not allowed @ data[‘brightness’]. Got '200

Something worth noting is that the Xiaomi Aqara motion sensors have a 2 minute time out on last motion detected, so if you want an automation to act after 1 min 30 sec of no motion, due to the timeout of the sensor, that will end up being 3 min 30 secs.

A total stab in the dark here, but adding a condition into Set level light hal 1 might help.

condition:
  platform: state
  entity_id: binary_sensor.motion_sensor_158d0001644e02
  state: 'off'

I would also suggest using scenes to control the changes in the light brightness, color temp etc. Makes automations easier when you can just call a scene.

- name: Evening Lights
  entities:
    light.steps:
      state: on
      brightness: 60
      rgb_color: [225,167,255]
      transition: 3
    light.table:
      state: on
      brightness: 60
      rgb_color: [225,167,255]
      transition: 3

- name: White Lights
  entities:
    light.steps:
      state: on
      brightness: 75
      color_temp: 222
      transition: 3
    light.table:
      state: on
      brightness: 75
      color_temp: 222
      transition: 3

Then you call scene.turn_on in your automations.

You always use light.turn_on to call color changes, brightness level etc.