ESP 8266 PIR Motion sensor + MQTT

Hi everybody,

Hope I’m posting in the right section…

I’m searching for a working arduino sketch to use a HC-SR501/505 PIR sensor on an esp8266 with MQTT and can handle to login with usr/pass to my MQTT broker.
If someone can share, it would be amazing thanks

I use the following code … for the PIR sensor, if reads GPIO 14 on a SonOff(which is ESP8266 based) which is connected to the PIR, as for the MQTT just use and modify the example code, it’s the same for most use cases. A good example would be KmanOzs SonOff code on this forum (https://github.com/KmanOz/Sonoff-HomeAssistant)

void pir_readPIR()
{
  char log[LOGSZ];

  pirval = digitalRead(PIR_PIN);  // read input value

    if (pirval == HIGH) 
    {            // check if the input is HIGH
      if (pirState == LOW) 
      { 
        digitalWrite(LED, LOW);  // turn LED ON
        // we have just turned on
        addLog("Motion detected!");
        digitalWrite(RELAY,HIGH); // turn relay on
        sprintf(log, "on");
        addLog(log);
        // Publish the Relay state to HA
        mqttClient.publish(MQTT::Publish(PUB_PREFIX"/"MQTT_TOPIC,log).set_retain().set_qos(1));
        ESP.wdtFeed();
        // We only want to turn on light on the output change, not state
        pirState = HIGH;      
      }
    } 
    else 
    {
      if (pirState == HIGH)
      {

        digitalWrite(LED, HIGH); // turn LED OFF
        // we have just turned off
        addLog("Motion detected Ended!");
        digitalWrite(RELAY,LOW); // turn relay off
        sprintf(log, "off");
        addLog(log);
        // Publish the Relay state to HA
        mqttClient.publish(MQTT::Publish(PUB_PREFIX"/"MQTT_TOPIC,log).set_retain().set_qos(1));
        ESP.wdtFeed();
     
        // We only want to turn on the light on the output change, not state
        pirState = LOW;
      }
    }
}

Awsome I’m gonna have a look at it thank you @keithh666

If you need more info let me know :slight_smile:

I will Thank’s

@keithh666 Can you share your entire sketch?

My sketch is the SonOff sketch but modified to call the function above, so download that and check it out. All I did was add a pinout as an input and a call to the above function.

1 Like

Super gonna try that.

Hi

Is your setup similar to this : https://github.com/jayrox/esp8266_feather_huzzah_pir_dht11_btn . I am trying to work this but failing. Can you please help?

Thanks.

The PIR sensor looks the same, and as I said I use the Sonoff sketch modified to call the above function. What does not work for you?

Hi

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

So whenever there is a motion I can tell it detects it from the serial monitor but it doesnt turn the kitchen lights on/off. Can you please help?

I would try subscribing to mydevices/irtransmitter/sender/NEC/# in a putty terrminal to see what if anything is being sent out to your transmitter. Also what does HA say in the log file, is it seeing the controlon payload you can also subscribe to sensor/motion1 to check that it’s being sent out to HA.

I’m not familiar with your lights, but the payload for “on” and “off” in your config are both the same, all mine have a different payload for each command.

@bachoo786 First make sure to use an ESP12 based board it doesn’t worked for me on any of my ESP01 maybe because of the type of the input.(pull up?)

Edit: I’ve tried with the github link you’ve posted before and it works well.

I think you should proceed step by step and make sure you can first receive the MQTT message from your device (esp).
You can track it using the “MQTT Dashboard app” on Android https://play.google.com/store/apps/details?id=com.thn.iotmqttdashboard&hl=fr or another one like for IOS or even a chrome plugin.

First configure your Mqtt server ip, login, password into the app then add you device with a friendly name and the topic you have to subscribe to like this:

Once its done and you are sure that it’s ok add this to your sensor.yaml file or in configuration.yaml with sensor before the code.the restart “Home Assistant”

platform: mqtt
state_topic: ‘sensor/motion1’
name: ‘Bedroom Motion’
qos: 0

you will be able to monitor the state of your sensor in your home page.

When you arrive to this point then you can think about your automation.

@keithh666 I have subscribed to my topic as set on my nodemcu and motion is been detected but HA doesn’t detect anything regarding payload.

@wkdwill I have am now using the bedroom lights which have different payloads for on and off but this does not matter as for the motion you need to subscribe to the topic set on the nodemcu. It all matters in the automation.yaml which in my case is as follows:

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


- alias: Motion 1 Off
  trigger:
    platform: mqtt
    topic: sensor/motion1
    payload: 'controlOff'
  action:
   service: switch.turn_off
   entity_id:
    - switch.bedroom_lights

@Kareem I do not need to monitor the state of my sensor as I am subscribing to it via mqtt and can confirm the payloads are being read when motion is detected. My issue is with the automation because when motion is detected ‘controlOn’ payload is published by no “action” is taking place as per the automation.yaml

Do your lights turn on when you trigger the automation from the HA frontend? Using either the automation card or the switch card?

@bachoo786 I’ve tried on my side and it work flawlesslly for me. Make sure the service you call for the light is the right one “switch.turn_on” I think that it’s fine, but you don’t need to specify the entity_id : switch.bedroom_lights this is maybe the problem…

well check that if it can help you:

and here is the code:

  - alias: "Motion 1 On"
    trigger:
      - platform: mqtt
        topic: sensor/motion1
        payload: 'On'
    action:
        service: light.turn_on

  - alias: "Motion 1 Off"
    trigger:
      - platform: mqtt
        topic: sensor/motion1
        payload: 'Off'
    action:
        service: light.turn_off

Right so it turns out I wasnt making the correct reference to my automation.yaml which was causing the problems.

It works now many thanks for your feedback everyone.

Glad you manage to make it work :slight_smile:

Hello,

Can this sensor be directly connected to my PI running HA and have a rpi_gpio as the platform?
Something like this:
binary_sensor:

  • platform: rpi_gpio
    pullmode: DOWN
    ports:
    27: Sensor do Quarto

It doesnt seem to get updated as there is any motion detected.

Thank you!

Antonio