Automate Light On with Aqara Sensor

I’m new to Hassio and am trying to get my head wrapped around a couple of automations. I’ve got Zigbee2MQTT configured and my Xiaomi items are all connected. I’ve managed to get a button click to toggle one of my lights, but I’ve also got a rtcgq11lm Aqara human body movement and illuminance sensor that I want to automate that if it’s dark and the room light is off and it detects motion to turn the light on.

This is what I have so far, but I can’t for the life of me get it to work. I know the state is coming in when I check the zigbee2mqtt logs, but the automation just isn’t running. Any help would be much appreciated please. :smile:

- id: 'triggerroomlight'
  hide_entity: true
  alias: Turn on room light if dark and sensor tripped
  trigger:
    # Start automation on trigger of HBMIS-01
  - platform: mqtt
    topic: zigbee2mqtt/0x00158d00034d003e

  condition:
    condition: and # link with and
    conditions: # Multiple conditions
      # Condition #1: HBMIS-01 illuminance is less than 50
      - condition: template
        value_template: '{{ trigger.payload_json.illuminance < 50 }}'
      # Condition #2: HBMIS-01 occupancy is true
      - condition: template
        value_template: '{{ trigger.payload_json.occupancy == "true" }}'
      # Condition #3: 'light.upstairs_bedroom' is off
      - condition: state
        entity_id: light.upstairs_bedroom
        state: 'off'

  action:
    # Action #1: Turn 'light.upstairs_bedroom' on
    - service: light.turn_on
      data:
        entity_id: light.upstairs_bedroom
    # Action #2: Start timer to turn light off again after 60 seconds
    - service: timer.start
      data:
        entity_id: timer.temporarylight

First thing to be aware of is that the software you’re using is Home Assistant, the installation method you’ve used is what’s called Hass.io.

If you’ve set up MQTT discovery as the Zigbee2MQTT docs show you’ll have a number of sensors, and a binary sensor, for that motion sensor. Doing that will make your life significantly easier here, rather than trying to use MQTT topics.

1 Like

Ah thank you. I’m never sure what to call it. Good to know. :smile:

That makes so much sense to use the binary_sensor! I never thought of that. Thanks. I’ve done that, but it’s still not running for some reason? This is my code now

- id: 'triggerupstairsbedroomlight'
  hide_entity: true
  alias: Enable room light if dark and sensor tripped
  trigger:
    # Start automation on trigger of HBMIS-01
    platform: state
    entity_id: binary_sensor.0x00158d00034d003e_occupancy
    to: 'on'

  condition:
    condition: and # link with and
    conditions: # Multiple conditions
      # Condition #1: HBMIS-01 illuminance is less than 50
      - condition: template
        value_template: '{{ "states.sensor.0x00158d00034d003e_illuminance.state" < 50 }}'
      # Condition #2: 'light.upstairs_bedroom' is off
      - condition: state
        entity_id: light.upstairs_bedroom
        state: 'off'

  action:
    # Action #1: Turn 'light.upstairs_bedroom' on
    - service: light.turn_on
      data:
        entity_id: light.upstairs_bedroom

I’m not sure if I’m calling the illuminance state correctly or not or if I can even put it in double quotes like that. I tried without but it errors out with the sensor name saying it found the end of line unexpectedly. Thanks for that example link by the way, that was very helpful.

I’m not even sure if it’s trying to trigger or not. If I check my automations it says it’s never been triggered.

The first condition should convert illuminance to a numeric value (either integer or float) before checking if the resulting value is less than 50.

      # Condition #1: HBMIS-01 illuminance is less than 50
      - condition: template
        value_template: "{{ states('sensor.0x00158d00034d003e_illuminance') | int  < 50 }}"
1 Like

So much easier just to use a numeric state condition :wink:

- condition: numeric_state
  entity_id: sensor.0x00158d00034d003e_illuminance
  below: 50
1 Like

to-MAY-to
to-MAH-to

:wink:

… but, yeah, numeric_state is easier. :slight_smile:

Thank you so much! You guys are legends. Back to tinkering and learning now. Cheers.