MQTT Motion Sensor Automation Not Working

I recently made some motion sensors using ESP8266 modules and PIR sensors that publish their state to MQTT. When motion is detected, I can see it in HA, so I know that they are reporting properly, and HA is working fine with the broker. When I try to set up an automation to have lights turn on based on motion detection however, the lights do not turn on. I have other automations that do work, such as patio lights turning on at sunset and off at midnight, and lights dimming when media is played on my chromecast, so I am really not sure what I am doing wrong.

Here is the sensor configuration:

binary_sensor:
  platform: mqtt
  state_topic: "Hallway2Motion"
  name: "Hallway 2 (Motion)"
  qos: 0
  payload_on: "ON"
  payload_off: "OFF"

Here is the automation:

#----------------------------------------------------------------
# Turn on Hallway Lights When Motion Triggered
#----------------------------------------------------------------
- alias: "Hallway Motion"

  trigger:
    platform: state
    entity_id: binary_sensor.Hallway2Motion
    to: 'ON'

  action:
    service: scene.turn_on
    entity_id: scene.hallwayfull

#----------------------------------------------------------------
# Turn off hallway lights 10 Minutes After Motion
#----------------------------------------------------------------
- alias: "Hallway Timer"

  trigger:
    platform: state
    entity_id: binary_sensor.Hallway2Motion
    to: 'OFF'
    for:
      minutes: 10
    
  action:
    service: scene.turn_on
    entity_id: scene.hallwayoff

And the related scenes:

  - name: Hallwayfull
    entities:
        light.hallway_1:
            state: on
            transition: 2
            brightness: 255
        light.hallway_2:
            state: on
            transition: 2
            brightness: 255
        light.hallway_3:
            state: on
            transition: 2
            brightness: 255
        light.hallway_4:
            state: on
            transition: 2
            brightness: 255

  - name: Hallwayoff
    entities:
        light.hallway_1:
            state: off
        light.hallway_2:
            state: off
        light.hallway_3:
            state: off
        light.hallway_4:
            state: off

Are you sure that your entity_id is binary_sensor.Hallway2Motion ? From the name of the sensor I guess it will be slightly different. Check it at the dev tools page. ( < > ).

That actually didn’t fix the issue, after changing the entity_id to the correct one, the automation still does not work. I also tried changing

action:
    service: scene.turn_on
    entity_id: scene.hallwayfull

to

action:
    service: homeassistant.turn_on
    entity_id: group.hallway

with no luck either.

Anything else that looks wrong? Also, is there a way to manually trigger an automation, or see if an automation was triggered?

To check if it has triggered, take a look at the logbook.
For your scene…have you checked (again :wink: ) it’s the right name ?
Also, you can put here your whole automation to check if there’s any error.

Here are the logbook entries from when the motion sensor is triggered:

It looks like the automation is not being run?

Here is the automation:

#----------------------------------------------------------------
# Turn on Hallway Lights When Motion Triggered
#----------------------------------------------------------------
- alias: "Hallway Motion"

  trigger:
    platform: state
    entity_id: binary_sensor.hallway_2_motion
    to: 'ON'

  action:
    service: scene.turn_on
    entity_id: scene.hallwayfull

#----------------------------------------------------------------
# Turn off hallway lights 10 Minutes After Motion
#----------------------------------------------------------------
- alias: "Hallway Timer"

  trigger:
    platform: state
    entity_id: binary_sensor.hallway_2_motion
    to: 'OFF'
    for:
      minutes: 10
    
  action:
    service: scene.turn_on
    entity_id: scene.hallwayoff

Here is the scenes:

  - name: Hallwayfull
    entities:
        light.hallway_1:
            state: on
            transition: 2
            brightness: 255
        light.hallway_2:
            state: on
            transition: 2
            brightness: 255
        light.hallway_3:
            state: on
            transition: 2
            brightness: 255
        light.hallway_4:
            state: on
            transition: 2
            brightness: 255
            
  - name: Hallwayoff
    entities:
        light.hallway_1:
            state: off
        light.hallway_2:
            state: off
        light.hallway_3:
            state: off
        light.hallway_4:
            state: off

When I manually trigger the scenes, they do work, so I know that the lights are correct.

Does the binary sensor sets states to ON/OFF or on/off (capital or not capital)?

1 Like

I have a simple motion detection automation working, perhaps this will help. I had to use lowercase ‘on’ instead of ‘ON’, but I’m not sure if that is what got it working. I also added “-” in front of platform.

automation:
  alias: Motion turn on lamp
  trigger:
    - platform: state
      entity_id: binary_sensor.aeotec_multisensor_6_sensor_2
      state: 'on'  # from: 'off'  also works here for me.
  action:
      service: light.turn_on
      entity_id: light.lamp
1 Like

From the configuration of the sensor:

binary_sensor:
  platform: mqtt
  state_topic: "Hallway2Motion"
  name: "Hallway 2 (Motion)"
  qos: 0
  payload_on: "ON"
  payload_off: "OFF"

I believe that this means it should be capitalized. The sketch I wrote publishes “ON” and “OFF” to the broker.

Ok, so as it turns out, you guys were right about the capitalization. States are lower case, and have nothing to do with the MQTT payloads. Thanks for all your help!

Hey I know it is kinda late. But can you maybe send me your .ino Sketch for the Motion Sensors? I don’t get mine to work properly.

1 Like

Hi

As @sebastianapw mentioned can you please give us your sketch as I have a PIR motion sensor and I am not sure where to connect it i.e. on ESP8266 or Raspberry pi? As my lights switch on via MQTT.

Question is how can I integrate my lights with my PIR sensor ? Do you have a similar set up?

Thanks.

Hi, I found a good script on GitHub, link below.
If you have a NodeMCU the numbers for the connection pins are a little confusing, if you need any help there, just reply on this post.
You can wire the PIR as following:
VCC --> 3,3V VCC Pin on ESP8266
GND --> GND on ESP8266
DATA --> PIN 12 on a NodeMCU this equals to D6

On a Raspberry Pi you can use the RaspberryPi GPIO Binary Sensor component, wiring is also easy, for further questions please reply.

Hi

Thank you for the link.

I will try it later on when I get home from work.

Hi

Can you please post your automation.yaml file and the binary sensor part in your configuration.yaml file?

On a side note: the automation.yaml file is meant to be saved in the same directory as the configuration.yaml file is that correct?

I have everything set up but when my NodeMCU detects motion (checking from the serial monitor) the lights do not turn on. I have changed the motion topic in the ino file to match my mqtt lights command topic.

Here is my code:

I have created a new automation.yaml file and placed it in the same directory as my configuration.yaml file. I have copied the automation code and changed some bits as shown below:

alias: Motion 1 On
trigger:
platform: mqtt
topic: sensor/motion1
payload: 'controlOn'
action:
service: light.turn_on
entity_id:
light.living_room

and changed the service and entity id to match mine which is:

alias: Motion 1 On
trigger:
platform: mqtt
topic: sensor/motion1
payload: 'controlOn'
action:
service: switch.turn_on
entity_id:
switch.kitchen_lights

I am using mqtt to turn my kitchen lights on/off and I have them setup as a switch i.e. in my configuration I have them setup like this:

switch:

platform: mqtt
name: "Kitchen Lights"
state_topic: "mydevices/irtransmitter/#"
command_topic: "mydevices/irtransmitter/sender/NEC/32"
payload_on: "16712445"
payload_off: "16712445"
optimistic: true
qos: 0
retain: true

Can you please help?

Thanks.