I built a gas logs controller. It uses two momentary relays, one to start the flame and the other to turn it off. I looked at some lambda examples, but all the ones I can find are for a single switch. Not sure how to adapt it to keep track of dual switching, or if maybe there is another or better way to do this?
Look at the URL it is esphome, so that’s where it goes.
The template switch without any state feedback (if you don’t define the lambda) can either run in optimistic mode (last action defines state) or as stateless. In which case you get two push buttons, one for on, one for off.
Ideally you would have some sort of state feedback to tell if the fire was on.
If the fire can be controlled outside home assistant (buttons on the fire, remote, etc…) I would highly advise against optimistic mode. It will get out of sync with the actual state. This is why a state feedback sensor is needed.
For now, it cannot. Only the ESP8266 can control it. No external controllers. I tried the lambda code and the new switch follows the 2s delay and doesn’t stay on. I added this to the bottom of the above code.
That did the trick! I need to read up on this optimistic mode and also state feedback sensors
I have seen on this forum many people building these and a few have ask about how to keep track of the state, but none ever said they got it working in the end.
Thank you and of course until after I do some reading regarding the optimistic mode and state feedback sensors, I will still consider this a work-in-progress.
State feedback - best method. The state of the switch depends on the state of a sensor. This way changes outside Home Assistant / ESPHome can be followed.
Optimistic - switch state follows whatever the last action in Home Assistant / ESPHome (turn on / off) was. Will get out of sync with actual state of the device if controlled outside Home Assistant / ESPhome.
Stateless - no state feedback and not in optimistic mode. Shows two buttons instead of a toggle switch. You can press either the on or off button at any time regardless of the state of the device.
Looking into flame sensors now. I guess I need to find out if flame sensors are compatible with ESPHome. I think safety is of utmost importance and this project really should require a flame sensor.
I have another question. Is it possible to add an LED to this, or use the one already on the ESP8266 to show the state? ie Lit when fireplace is on?
Edit: Found some example code to do this, but not yet working. Can one template state be used to set another template, because this seems to be the case here.
In both cases if you don’t want the light showing up in Home Assistant as a control, remove the name: option from the light config and just leave the id:. The output config will not show up as a control in Home Assistant, even with a name.
In either case you add it to your template switch like this
The turn on and turn off actions of the template switch can accept a list of actions to do. So no need for another template. The turn_on action is where you would use an effect action if you used the first light config and want this.
If it turns out you can not control the on-board LED on D4, look up the schematic of your board and adjust the GPIO pin as necessary. Or add your own LED (and current limiting resistor) to another pin. Keep in mind there are GPIOs you should not use. See the table lower down on this page: ESP8266 Pinout Reference: Which GPIO pins should you use? | Random Nerd Tutorials
What an awesome explanation! It is obvious you are very well acquainted with ESPHome programming. I opted for the simple solution as I really don’t need any light effects. This worked perfectly. Well, it didn’t at first. Once I added the new code the Lovelace switch started to turn back off after a few seconds, even though off had not been triggered. I fought with this for about an hour before I ended up simply rebooting Home Assistant. Everything worked fine after that. Weird.
Thank you and here is my complete yaml code after adding both the optimistic mode and the LED control.
That was odd. You can also reload the ESPHome Integration in Setings → Devices & Services. Click on the ESPHome card then click the three dots icon next to your device:
It’s a lot less extreme than “rebooting”. From least to most invasive to your system:
Reload Integration (or config): reload the integration/config for just the single integration/device.
Restart Home Assistant: Restart the Home Assistant container and all integrations.
Reboot host: shut off the hardware and restart. This stops everything down to the hardware level. Don’t confuse “reboot” with “restart home assistant”. Two very different things.