Setup PIR sensor to Home Assistant (MQTT)

Help connect the PIR sensor to Home assistant.
There is esp12 with tasmota firmware.
The PIR sensor is connected to the esp12 pin, in the tasmota settings it is defined as a “switch”. A relay is connected to the other esp12 pin (in the tasmota defined as “relay1”).
In esp12, the rule is configured - when the PIR sensor is triggered, turn on the relay, turn off when the movement ends.

Rule1 
on Switch1#state=1 do BackLog Power1 1; publish stat/%topic%/PIR1 ON endon
on Switch1#state=0 do BackLog Power1 0; Publish stat/%topic%/PIR1 OFF endon
Rule1 1

The Home assistant relay was found, displayed as switch.nodemcu_relay1, and controlled from the lovelace card.
I wanted to see the Home assistant state of the PIR sensor. I added in the configuration.yaml sensor:

binary_sensor:
  - platform: mqtt
    name: "PIR Sensor"
    state_topic: "stat/nodemcu/PIR1"
    availability_topic: "tele/nodemcu_125D7E/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    device_class: motion

The sensor appeared in the Home assistant, but its state is unavailable
What other settings need to be made so that this PIR sensor becomes active and can be used in automation?

Open the tasmota console, trigger the pir and show the output of the console window.

After the sensor is triggered, the following is displayed in the console:

10:30:07 RUL: SWITCH1#STATE=1 performs "BackLog Power1 on; publish stat/nodemcu/PIR1 ON"
10:30:07 MQT: stat/nodemcu/SWITCH1 = {"STATE":"ON"}
10:30:07 MQT: tele/nodemcu/STATE = {"Time":"2020-05-28T10:30:07","Uptime":"0T19:05:46","UptimeSec":68746,"Heap":26,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":37,"MqttCount":1,"POWER1":"ON","POWER2":"OFF","Wifi":{"AP":1,"SSId":"Gate","BSSId":"18:18:4D:6a:d2:e6","Channel":7,"RSSI":86,"Signal":-57,"LinkCount":1,"Downtime":"0T00:00:41"}}
10:30:07 MQT: stat/nodemcu/RESULT = {"POWER1":"ON"}
10:30:07 MQT: stat/nodemcu/POWER1 = ON
10:30:07 MQT: stat/nodemcu/PIR1 = ON

You did not define your availability correct :

binary_sensor:
  - platform: mqtt
    name: "PIR Sensor"
    state_topic: "stat/nodemcu/PIR1"
    availability_topic: "tele/nodemcu/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    device_class: motion

I made corrections, but the situation has not changed.
In HomeAssistant:


In configuration nodemcu:

Maybe there are more ideas?

Try :

binary_sensor:
  - platform: mqtt
    name: "PIR Sensor"
    state_topic: "stat/nodemcu/PIR1"
    availability_topic: "tele/nodemcu/LWT"
    qos: 1
    payload_available: "Online"
    payload_not_available: "Offline"
    device_class: motion

ON and OFF are defaults for binary_sensors, so not needed.

No. That doesn’t work either.
I connected the mqtt client from the computer and I see that messages in the topic about sensor operation are coming.
And for some reason, Home Assistant doesn’t understand them. :frowning:

PIR sensors can trigger repetitively in a short period of time. It might be worth setting up an R-C circuit to keep relay going for set amount of time (capacitor decay) to prevent cycling power on the relay and other sensors. simple 47uH and 500kO provides about 20 seconds before 3.3 voltage drops to 0.6V and transistor gate closes.

For the ‘unavailable’ issue…

Looks like the binary_sensor is expiring causing it to change to unavailable. I believe this is caused because the ESP32 is disconnecting from the MQTT server, making HA not know what is going on… T

I think you have a couple options here…

Consider using retain - set that to true. This will make HA keep the last payload received. Therefore, even if the ESP32 disconnects the last message received maintains its status.

Other options, try adding: off_delay: 60 (this will make HA in the device class: motion change from: “detected motion” - to -> “cleared (no motion detected)”

Or expire_after, but that might start causing ‘unknown’.