How to keep track of STATE of dual momentary relays on one device [Gas Logs controller]

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.

1 Like

I will drop in this example, edit it and see what happens. Thank you.

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.

 - platform: template
    name: "Fireplace Switching"
    lambda: |-
      if (id(gaslogsrelay).state) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      - switch.turn_on: gaslogsrelay
    turn_off_action:
      - switch.turn_on: gaslogsrelayneg

Ok then, remove the lambda and add optimistic: true

1 Like

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.

Thank you!

Eventually I would like to convert this into an HVAC integration. Not even sure where to begin with doing that.

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.

d723e203ea3861b41037c65be641da32adc23ccd

I assume this is some type of hardware sensor, or is it possible to save the state in the device itself as a variable?

I clearly understand this one now.

I may experiment with this one.

Yes a physical sensor. Using a variable would be equivalent to optimistic mode.

1 Like

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.

Read the whole topic. There is possibly a better option lower down.: Winter is coming - best way to keep an eye on the fireplace?

I read the entire topic. I originally looked for a thermocouple on amazon but was not able to find one. The MLX90614 looks promising.

1 Like

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.

I think the on-board LED is on GPIO2/D4 on your ESP board (A Wemos D1 mini, or clone). So you could define a monochromatic light on that pin:

output:
  - platform: esp8266_pwm
    id: board_led
    pin: D4

light:
  - platform: monochromatic
    name: "On-board LED"
    id: onboard_led
    output: board_led

The only reason for doing that is that you can actually use effects like flashing the LED rather than just turning it on.

A more simple solution that only turns on or off would be a binary light:

output:
  - platform: gpio
    id: light_output
    pin: D4

light:
  - platform: binary
    name: "On-board LED"
    id: onboard_led
    output: board_led

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

    turn_on_action:
      - switch.turn_on: gaslogsrelay
      - light.turn_on: onboard_led
    turn_off_action:
      - switch.turn_on: gaslogsrelayneg
      - light.turn_off: onboard_led

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

2 Likes

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.

esphome:
  name: gas-logs-hvac
  friendly_name: Gas Logs HVAC

esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "blablabla"

ota:
  password: "blablabla"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

# Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Gas-Logs-Hvac Fallback Hotspot"
    password: "blablabla"

captive_portal:

output:
  - platform: gpio
    id: board_led
    pin: D4 #GPIO2 Built in LED
    inverted: true


light:
  - platform: binary
#    name: "On-board LED"
    id: onboard_led
    output: board_led 


switch:
  - platform: gpio
    name: "Gas Logs Relay" #Positive polarity +5vdc
    id: gaslogsrelay
    pin: D2 #GPIO2
    restore_mode: ALWAYS_OFF
    interlock: [gaslogsrelayneg]
    on_turn_on:
    - delay: 3000ms
    - switch.turn_off: gaslogsrelay
    

  - platform: gpio
    name: "Gas Logs Relay Neg" #Negative polarity -5vdc
    id: gaslogsrelayneg
    pin: D3 #GPIO4
    restore_mode: ALWAYS_OFF
    interlock: [gaslogsrelay]
    on_turn_on:
    - delay: 3000ms
    - switch.turn_off: gaslogsrelayneg

  
  - platform: template
    name: "Fireplace Switching"
    optimistic: true 
    turn_on_action:
      - switch.turn_on: gaslogsrelay
      - light.turn_on: onboard_led
    turn_off_action:
      - switch.turn_on: gaslogsrelayneg
      - light.turn_off: onboard_led
1 Like

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!

1 Like

I created a new thread about this in Configuration.

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.