Ho provato le vostre soluzioni, ma non funzionano, e forse l'errore e' il mio che non ho pubblicato l'intero sketch e mi sono spiegato male.
Lo sketch serve per gestire un rele comandato sia da OpenHab che da un pulsante. MQTT mi serve per la notifica su OpenHab per lo stato ON/OFF
Ecco lo sketch completo e formattato con ctrl+T dalla IDE (non sapevo che andasse fatto, ma si impara sempre qualcosa 
grazie in anticipo per il vostro aiuto
Davide
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ArduinoOTA.h>
#include <IRremoteESP8266.h>
#include <WiFiUdp.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
IPAddress staticIP(xxx, xxx, xxx, xxx); //ESP static ip
IPAddress gateway(xxx, xxx, xxx, xxx); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
IPAddress dns(8, 8, 8, 8); //DNS
#define wifi_ssid "xxxxx"
#define wifi_password "xxxx"
/************************* MQTT Broker Setup *********************************/
#define mqtt_server "xx.xx.xx.xx"
#define mqtt_serverport 1883 // use 8883 for SSL
#define mqtt_username "xxxxx"
#define mqtt_password "xxxxx"
/************************* Constants, Variables, Integers, etc *********************************/
const int BLINK = D3; //D3
const int FEED = D6;
const int relayPin = D1;// D1
int value = 0;
int relayState = LOW;
const long togDelay = 100; // pause for 100 milliseconds before toggling to Open
const long postDelay = 200;
boolean flag = HIGH;
int ValueQ = 0;
long Tempo1 = 0;
long Tempo2 = 50;
int ValueCombQ = 5;
/**********************MQTT*****************************************************/
Adafruit_MQTT_Client mqtt(&client, mqtt_server, mqtt_serverport, mqtt_username, mqtt_password);
Adafruit_MQTT_Subscribe Robo500 = Adafruit_MQTT_Subscribe(&mqtt, "openhab/out/Robo500/command");
Adafruit_MQTT_Publish feed = Adafruit_MQTT_Publish(&mqtt, "openhab/in/Robo500/state");
void MQTT_connect();
WiFiClient client;
//==============================================================
// SETUP
//==============================================================
void setup() {
Serial.begin(115200);
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
WiFi.config(staticIP, subnet, gateway, dns);
WiFi.mode(WIFI_STA);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected at IP address: ");
Serial.println(WiFi.localIP());
pinMode(BLINK, OUTPUT);
pinMode(relayPin, OUTPUT);
pinMode(FEED, INPUT);
mqtt.subscribe(&Robo500);
// Begin OTA
ArduinoOTA.setPort(8266); // Port defaults to 8266
ArduinoOTA.setHostname("xxxxxx"); // Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setPassword((const char *)"xxxxx"); // No authentication by default
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("");
Serial.println("Ready & WiFi connected");
Serial.print("IP address: "); Serial.println(WiFi.localIP());
}
//==============================================================
// LOOP
//==============================================================
void loop() {
ArduinoOTA.handle();
if (WiFi.status() == WL_CONNECTED) {
digitalWrite(BLINK, HIGH);
delay(20);
digitalWrite(BLINK, LOW);
delay(20);
}
/****************antirimbalzo*******************/
int StatoTastoQ;
int StatoTastoQa = LOW;
if (valoreTastoQ != StatoTastoQa)
{
Tempo1 = millis();
}
if ((millis() - Tempo1) > Tempo2)
{
StatoTastoQ = valoreTastoQ;
}
StatoTastoQa = valoreTastoQ;
/*************fine*****************************/
/**************esecuzione da openhab********************/
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(500))) {
if (subscription == &Robo500) {
Serial.print(F("Got: ")); Serial.println((char *)Robo500.lastread);
Serial.println("Close Relay for 100 ms & then Open");
digitalWrite(relayPin, HIGH);
delay(togDelay);
digitalWrite(relayPin, LOW);
delay(postDelay);
}
}
/**************esecuzione da pulsante ***********************/
int valoreTastoQ = digitalRead(FEED);
if (valoreTastoQ == HIGH && flag == HIGH)
{
digitalWrite(relayPin, HIGH);
delay(togDelay);
digitalWrite(relayPin, LOW);
feed.publish("ON");
flag = LOW;
}
if (valoreTastoQ == HIGH && flag == LOW) {
digitalWrite(relayPin, LOW);
delay(togDelay);
digitalWrite(relayPin, LOW);
feed.publish ("OFF");
flag = HIGH;
}
delay(1000); // attendo un secondo prima di ripetere il loop
Serial.println (flag);
delay (350);
if (! mqtt.ping ()) {
mqtt.disconnect ();
}
}
/******************************funzione MQTT**********************/
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}