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.
Very good to know. I did a full reboot from the ssh command line (#sudo reboot) . I should have said I rebooted the RPi instead of HA. Inaccurate choice of words lol. If this happens again, I will try your suggestions first. Thanks!
I need help with almost the same, but I only have one relay switch. This turns on the device(coffee machine) when the relay(connected in parallel to the physical button) turns on via ESPHome. The same process is used to turn it off, either the device’s button is pressed or the relay (controlled by ESP32 via HA) is triggered again.
So I need to know if the device is on. That means a “value” or boolean_,switch that keeps track of the device state. That is: this device turns on via the relay and the “value” gets assigned a “true”, even though the relay was turned on and off, within on second. This “value” should stay true, until the same relay turns on and off again, then only should the “value” change to “false” - stating the status of the devices’ power.
I do not know how to keep track of the physical device state yet. The device have a power LED that is on while the power is turned on. If I can somehow know that, I can base the “value” on the state of the LED instead.
But I first need to know the state of “value” between the two triggers of the relay.
How can I accomplish that?
This is my code:
> switch: # power-switch - B1: GPIO16
> - platform: gpio
> id: power
> pin:
> number: 16
> name: power
> icon: ""
> restore_mode: ALWAYS_OFF
> on_turn_on:
> - delay: 500ms
> #value needs to be set to "true" which can be used to display the device state as "powered" or "on" until the relay is triggered again
> - switch.turn_off: power
The problem I have with this code is the “value” is only “true” for 500ms, while the relay receives a signal.