Need ESP8266 relay device to display last status

Sorry for this very basic question as I am very new (2 weeks) to ESPHome and HA.

I have an esp8266 device connected to a 4 relay board. My main goal is to control 2 water valves. Basically turn then on and off and show their status in HA. I have everything wired and all works well. I can turn on/off the individual relays from HA…which is the intent. What I’m stuck on is I would like to show the “status” of the valve (or actually it would be which relay I activated last). My esp yaml code is as follows for the main valve:

switch:
- platform: gpio
pin: GPIO14 # LED (D5) connected
name: ${name} Relay1
id: relay1
on_turn_on:
- delay: 15s # Turn off after 15 seconds
- switch.turn_off: relay1

  • platform: gpio
    pin: GPIO12 # LED (D6) connected
    name: ${name} Relay2
    id: relay2
    on_turn_on:
    • delay: 15s # Turn off after 15 seconds
    • switch.turn_off: relay2

Relay 1 opens the valve (actually powers the opening actuator for 15s then powers off…that is the time it takes to open the valve all the way).

Relay 2 closes the valve (using the same power cycle time).

What I would like to present in HA is if I activate Relay1, after 15s I would have a item on my dashboard saying “Main Valve Open”.

Then if I activate Relay2 my dashboard would say “Main Valve Closed” and it would remain that way until Relay1 is activated again.

I cant seem to find how to do this?

You could have a look at cover and valve components

You can try something like this.

switch:
- platform: gpio
    pin: GPIO14 # LED (D5) connected
    name: ${name} Relay1
    id: relay1
    on_turn_on:
    - delay: 15s # Turn off after 15 seconds
    - switch.turn_off: relay1
    - lambda: |-
         id(main_valve).publish_state("Main Valve Open");

- platform: gpio
    pin: GPIO12 # LED (D6) connected
    name: ${name} Relay2
    id: relay2
    on_turn_on:
      - delay: 15s # Turn off after 15 seconds
      - switch.turn_off: relay2
      - lambda: |-
          id(main_valve).publish_state("Main Valve Closed");
 
text_sensor:
  - platform: template
    name: Main Valve
    id: main_valve

You will probably have to fix the indentation

Perfect! That worked fantastic!

One more question if I could - Any idea how to preserve and restore the valve of the text sensors after a reboot?

The text sensor doesn’t have a restore value, so you’ll have to do something like this:

esphome:
  name: display
  friendly_name: display
  on_boot:
    priority: -100.0
    then:
      - lambda: |-
          id(main_valve).publish_state(id(var_main_valve));


switch:
  - platform: template
    name: "Relay1"
    id: relay1
    optimistic: true
    on_turn_on:
      #
      - delay: 15s # Turn off after 15 seconds
      - switch.turn_off: relay1
      - lambda: |-
          id(main_valve).publish_state("Main Valve Open");


  - platform: template
    name: "Relay2"
    id: relay2
    optimistic: true
    on_turn_on:
      - delay: 15s # Turn off after 15 seconds
      - switch.turn_off: relay2
      - lambda: |-
          id(main_valve).publish_state("Main Valve Closed");
  
text_sensor:
  - platform: template
    name: Main Valve
    id: main_valve
    on_value:
      then:
        - globals.set:
            id: var_main_valve
            value: !lambda 'return x;'

globals:
  - id: var_main_valve
    type: std::string
    restore_value: yes

edit: Fixed the code
I tested with a template switch instead of GPIO, so you have to reconfigure it as GPIO

edit 2:
Doing some tests here…
The value is saved in memory once per minute, I don’t know if there is a way to save it immediately.
So if the esp restarts in the time between the sensor change and the recording in memory, the text will be wrong.

[14:02:16][D][switch:012]: 'Relay2' Turning ON.
[14:02:16][D][switch:055]: 'Relay2': Sending state ON
[14:02:21][D][switch:016]: 'Relay2' Turning OFF.
[14:02:21][D][switch:055]: 'Relay2': Sending state OFF
[14:02:21][D][text_sensor:064]: 'Main Valve': Sending state 'Main Valve Closed'

If the reset happens here, the value will not be saved

[14:02:28][D][esp32.preferences:114]: Saving 1 preferences to flash...
[14:02:28][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed