MQTT Motion Sensor NO ADAFRUIT

Ok, I hate adafruits garbage mqtt service so I am using cloud mqtt. I am having an issue with the payload_off part. I have a command to set the motion sensor state to on, but I was hoping to just hook up a timer that would start when the state was changed and then after 10 mins or so it would turn it off. I will put my esp8266 code below.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = “SSID”;
const char* password = “SSID_PASSWORD”;
const char* mqttServer = “mxx.cloudmqtt.com”;
const int mqttPort = PORT;
const char* mqttUser = “esp8266”;
const char* mqttPassword = “esp8266”;
int counter = 0;
int previousReading = LOW;

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup() {
pinMode(BUILTIN_LED, OUTPUT); // declare LED as output
pinMode(D5, INPUT); // declare sensor as input

Serial.begin(115200);

digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println(“Connecting to WiFi…”);
}
Serial.println(“Connected to the WiFi network”);

client.setServer(mqttServer, mqttPort);
client.setCallback(callback);

while (!client.connected()) {
Serial.println(“Connecting to MQTT…”);

if (client.connect(“ESP8266Client”, mqttUser, mqttPassword )) {

Serial.println("connected");  

} else {

 Serial.print("failed with state ");
 Serial.print(client.state());
 delay(2000);

}
}

client.publish(“esp/motion1”, “Motion 1 is Online”);
client.subscribe(“esp/motion1”);

}

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print(“Message arrived in topic: “);
Serial.println(topic);
Serial.print(“Message:”);
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println(”-----------------------”);

if ((char)payload[0] == ‘1’) {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED off (Note that LOW is the voltage level
} else if ((char)payload[0] == ‘2’) {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED on by making the voltage HIGH
}
}

void reconnect() {
// Loop until we’re reconnected
while (!client.connected()) {
Serial.print(“Attempting MQTT connection…”);
// Attempt to connect
if (client.connect(“ESP8266Client”, mqttUser, mqttPassword )) {
Serial.println(“connected”);
// Once connected, publish an announcement…
client.publish(“esp/motion1”, “Connected”);
// … and resubscribe
client.subscribe(“esp/motion1”);
} else {
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void loop() {

if (!client.connected()) {
reconnect();
}
client.loop();

if (!client.connected()) {
reconnect();
}
client.loop();
int reading = digitalRead(D5);
Serial.println(reading);
if (previousReading == LOW && reading == HIGH) {
counter++;
client.publish(“esp/motion1”, “Motion Detected”);
Serial.print(“Triggered”);
Serial.print(counter);
Serial.print("xs! ");
delay(1000);
}
previousReading = reading;

}

I find that it is more convenient to add this sort of timing to HA, rather than the sensor. You can change HA scripts relatively simply, but to change this (to get a shorter delay) you would have to reprogram the esp8266.