Momentary switch Help

hey everyone i am after a little bit of help with the coding of this. i have a astral Gas spa heater HX120, i am wanting add contacts in for the switches. The way the switch works on the unit is a small press button.

The button is a momentary switch press and release turns the unit on
press and release again the unit will turn off.

here is the code i have so far simple and would also work
switch:

  • platform: gpio
    pin: GPIO18
    id: relay
    name: “Spa ON/OFF”
    icon: “mdi:Spa”
    on_turn_on:
    • delay: 100ms
    • switch.turn_off: relay

what i would like to add in is when i turn the unit on with one press (button clicks in and out) that the state would display on, the same for when i turn the unit off.

hope this make sense and people can help me
Thanks Dave

https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

You need additional template switch for that. But be aware, it’s not anyhow reliable concept of printing state of your device. If you operate it manually or cut the power, you get out of synch.

switch:
  - platform: template
    name: "Virtual Switch"
    id: virtual_sw
    optimistic: true
    restore_mode: RESTORE_DEFAULT_OFF

    turn_on_action:
      - if:
          condition:
            switch.is_off: virtual_sw
          then:
            - switch.turn_on: relay

    turn_off_action:
      - if:
          condition:
            switch.is_on: virtual_sw
          then:
            - switch.turn_on: relay

You can set the relay as internal if you prefer not to see it on HA.

1 Like

I would add an additional physical sensor to monitor power. Maybe there is some output from spa that indicates on that you can use as sensor input to esp.

1 Like

i will have a look into this aswell :slight_smile: LCD does turn on when powered and off when power is off, maybe take something from there?

much better

switch:
  - platform: gpio
    pin: GPIO18
    id: relay
    name: "Spa ON/OFF"
    icon: "mdi:Spa"
    on_turn_on:
    - delay: 100ms
    - switch.turn_off: relay

  - platform: template
    name: "Virtual Switch"
    id: virtual_sw
    optimistic: true
    restore_mode: RESTORE_DEFAULT_OFF

    turn_on_action:
      - if:
          condition:
            switch.is_off: virtual_sw
          then:
            - switch.turn_on: relay

    turn_off_action:
      - if:
          condition:
            switch.is_on: virtual_sw
          then:
            - switch.turn_on: relay

I can’t seem to get this code to load onto the board. Comes up with error.

If i was to take an input off the screen how would i also write that in to the code.

INFO ESPHome 2025.11.2
INFO Reading configuration /config/esphome/spa-controller.yaml...
INFO Generating C++ source...
INFO Compiling app... Build path: /data/build/heater
Processing heater (board: esp32dev; framework: arduino, espidf; platform: https://github.com/pioarduino/platform-espressif32/releases/download/55.03.31-2/platform-espressif32.zip)
--------------------------------------------------------------------------------
INFO Package configuration completed successfully
INFO Package configuration completed successfully
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
 - contrib-piohome @ 3.4.4 
 - framework-arduinoespressif32 @ 3.3.2 
 - framework-arduinoespressif32-libs @ 5.5.0+sha.129cd0d247 
 - framework-espidf @ 3.50501.0 (5.5.1) 
 - tool-cmake @ 4.0.3 
 - tool-esp-rom-elfs @ 2024.10.11 
 - tool-esptoolpy @ 5.1.0 
 - tool-mklittlefs @ 3.2.0 
 - tool-ninja @ 1.13.1 
 - tool-scons @ 4.40801.0 (4.8.1) 
 - toolchain-xtensa-esp-elf @ 14.2.0+20241119
Reading CMake configuration...
Dependency Graph
|-- Networking @ 3.3.2
|-- ESPmDNS @ 3.3.2
|-- Update @ 3.3.2
|-- noise-c @ 0.1.10
|-- ArduinoJson @ 7.4.2
Linking .pioenvs/heater/firmware.elf
/data/cache/platformio/packages/toolchain-xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld: .pioenvs/heater/src/main.cpp.o:(.literal._Z5setupv+0x148): undefined reference to `_ZN7esphome9template_14TemplateSwitchC1Ev'
/data/cache/platformio/packages/toolchain-xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld: .pioenvs/heater/src/main.cpp.o:(.literal._Z5setupv+0x14c): undefined reference to `_ZNK7esphome9template_14TemplateSwitch20get_turn_off_triggerEv'
/data/cache/platformio/packages/toolchain-xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld: .pioenvs/heater/src/main.cpp.o:(.literal._Z5setupv+0x150): undefined reference to `_ZNK7esphome9template_14TemplateSwitch19get_turn_on_triggerEv'
/data/cache/platformio/packages/toolchain-xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld: .pioenvs/heater/src/main.cpp.o:(.literal._Z5setupv+0x154): undefined reference to `_ZN7esphome9template_14TemplateSwitch14set_optimisticEb'
/data/cache/platformio/packages/toolchain-xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld: .pioenvs/heater/src/main.cpp.o:(.literal._Z5setupv+0x158): undefined reference to `_ZN7esphome9template_14TemplateSwitch17set_assumed_stateEb'
/data/cache/platformio/packages/toolchain-xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld: .pioenvs/heater/src/main.cpp.o: in function `_ZN7esphome11Application18register_componentINS_11DelayActionIJEEEEEPT_S5_':
/data/build/heater/src/esphome/core/application.h:228:(.text._Z5setupv+0x4f5): undefined reference to `_ZN7esphome9template_14TemplateSwitchC1Ev'
/data/cache/platformio/packages/toolchain-xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld: .pioenvs/heater/src/main.cpp.o: in function `_ZN7esphome7switch_13TurnOffActionIJEEC4EPNS0_6SwitchE':
/data/build/heater/src/esphome/components/switch/automation.h:22:(.text._Z5setupv+0x549): undefined reference to `_ZNK7esphome9template_14TemplateSwitch20get_turn_off_triggerEv'
/data/cache/platformio/packages/toolchain-xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld: .pioenvs/heater/src/main.cpp.o: in function `_ZN7esphome9Component20set_component_sourceEPKNS_9LogStringE':
/data/build/heater/src/esphome/core/component.h:235:(.text._Z5setupv+0x5e5): undefined reference to `_ZNK7esphome9template_14TemplateSwitch19get_turn_on_triggerEv'
/data/cache/platformio/packages/toolchain-xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld: .pioenvs/heater/src/main.cpp.o: in function `_Z5setupv':
/data/build/heater/src/main.cpp:329:(.text._Z5setupv+0x677): undefined reference to `_ZN7esphome9template_14TemplateSwitch14set_optimisticEb'
/data/cache/platformio/packages/toolchain-xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld: /data/build/heater/src/main.cpp:329:(.text._Z5setupv+0x681): undefined reference to `_ZN7esphome9template_14TemplateSwitch17set_assumed_stateEb'
collect2: error: ld returned 1 exit status
*** [.pioenvs/heater/firmware.elf] Error 1
========================= [FAILED] Took 18.95 seconds =========================

Interesting… Sounds bug. What esphome version you have?
Try if it compiles with optimistic: false

Esphome would be the latest. I will try optimistic: false tomorrow

Ok. Measure also the LCD power pin voltage if you will to go with that approach.