The MQTT sensor and the Binary sensor are working exactly in that manner. They are just consuming messages. So if you want to use a button you only need to send its state. In sketch it’s done that way. If the button gets pressed then a message is sent on the state topic.
The command topic is for receiving command of a remote device (view from your controller/HA). If the command topic is the same as the state topic then you could end up in a loop. The receiver would consume its own state message which will be interpreted as command and then send a new state message.
Oh ok. Its starting to make a little more sense now. In that case I wonder if this would make sense-
[ul]
1 Add the ESP+Button as an MQTT sensor
2 Create a scene with most lights off for when we’re sleeping
3 Create a “Nightlight” scene for when we need to quickly turn the lights on
4 Create an event that listens for the sensor topic and triggers the nightlight scene
5 Add a condition that triggers the sleep scene if the nightlight is already triggered[/ul]
Or is there a simpler way to toggle between two scenes?
How many programmers does it take to build a lightswitch?
If anybody else has succeeded in building a simple wireless toggle switch then by all means please do share your method… Otherwise, I’m going to declare that this seemingly simple function is proving to be anything but! Nevertheless,it is the type of thing where one has to question the value of having so many amazing automations at our calling when its impossible to physically turn anything on or off without a phone or computer…
Anyway, I thought I had it all figured out with adding the toggle button as a sensor rather than a switch but as soon as I restarted HASS with my new config my lights started going all Skynet on me flickering on and off and refusing all commands… I tried a couple different configs but in the end I’m afraid it all comes down to a phenomenon called “bounce” wherein too much interference on an analog pushbutton causes the client to constantly send out events… This Arduino article explains a little bit more- learn.acrobotic.com/tutorials/po … ush-button
The more I think about it, the more it occurs to me that the simple act of toggling an LED on which most of us began our programming journey is very much dependent on simple analog communications e.g power come on=light comes on/power goes off=light goes off and so on… The same process doesn’t carry over all that well to triggering big LEDs over a wireless network apparently…
So I’m back at square one and will gladly entertain any “its not really that complicated” comments if they lead to an actual working solution…
Here’s one way of doing it I guess, but its pretty specific to functioning as a standalone wifi connecting wall switch. It may be possible to dissect the code and extract the pieces that relate to the switching function itself…
While it demonstrates turning on and off an alarm using an RFID reader, you can modify it so that by presenting a token you turn on and off what ever you want.
Instead of the RFID reader a key switch, push button or some other mechanism can be used. When I have some free time I will try to come up with some kind of solution.
I know this is an old topic. I hope its still relevant. I want to achieve something very similar ( I think!) to this. I have an esp8266 nodeMCU. Plenty of pins and goodies. I basically want a relay, led, and push button. I want the button to trigger the relay and LED (locally, NOT over Mqtt) then report the relays state to the MQTT broker. I also want it receive on/off from the broker. Im using the default MQTT broker with HASS. I can do the simple MQTT on/off but I have gotten lost in the button!
Any help is much appreciated!
Just few steps behind you, I am there as well. Just curious to know if you reached the desired destination? If yes, would be very much thankful if you could share the config yaml and the sketch please.
I am having esp8266 nodeMCU, HA and mqtt broker bundled with HA. So identical scenario.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char *ssid = "SSID"; // cannot be longer than 32 characters!
const char *pass = "PASS"; //
const char *mqtt_server = "MQTT_SERVER";
const int mqtt_port = MQTT_PORT;
const char* connection_id = "MQTT_ID";
const char* client_name = "MQTT_NAME";
const char* client_password = "MQTT_PASSWORD";
const char* topic = "MQTT_TOPIC";
char somebigthing[100];
const int buttonPin = D3;
const int ledPin = BUILTIN_LED;
WiFiClient espClient;
PubSubClient client(espClient);
// the current state of the LED and button
int ledState = LOW;
int buttonState = LOW;
// the current and previous readings from the input pin
int thisButtonState = LOW;
int lastButtonState = LOW;
// time is measured in milliseconds and will quickly exceed limitations of an integer, so we use a long for these two
unsigned long lastDebounceTime = 0; // the time the button state last switched
unsigned long debounceDelay = 50; // the state must remain the same for this many millis to register the button press
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
client.setServer(mqtt_server, mqtt_port); // MQTT!!!
}
void loop() {
Serial.println("Hello world1");
WiFi.begin(ssid, pass);
client.connect(connection_id, client_name, client_password);
Serial.println("Hello world2");
// the buttonPin is read multiple times and the value must remain the same for debounceDelay millis to toggle the LED
// read button state, HIGH when pressed, LOW when not
thisButtonState = digitalRead(buttonPin);
// if the current state does not match the previous state
// the button was just pressed/released, or is transition noise
if (thisButtonState != lastButtonState) {
// reset the timer
lastDebounceTime = millis();
}
// once delay millis have elapsed, if the state remains the same, register the press
if ((millis() - lastDebounceTime) > debounceDelay) {
// if the button state has changed
if (thisButtonState != buttonState) {
buttonState = thisButtonState;
// only toggle the LED if the buttonState has switched from LOW to HIGH
if (buttonState == HIGH) {
ledState = !ledState;
// toggle the LED
if (ledState == HIGH) {
SendOn();
}
else {
SendOff();
}
}
}
}
// persist for next loop iteration
lastButtonState = thisButtonState;
}
void SendOn(){
strcpy(somebigthing, "{\"enabled\":\"true\"}");
client.publish(topic, somebigthing);
}
void SendOff(){
strcpy(somebigthing, "{\"enabled\":\"false\"}");
client.publish(topic, somebigthing);
}