HG7881 and bistable solenoid valve

Hello everyone!
Sooo… I was trying to do my own irrigation system with esphome. I know there are a lot of projects like mine out there, but in my specific case I am trying to control a bistable 9V DC solenoid valve: with an impulse at “forward polarity” the valve opens, and another impulse with reversed polarity closes the valve. The valve actually is the one employed in this watering timer.

While it is possible to do this using two relays, I stumbled upon a HG7881 h-bridge module that it seemed perfect to solve my problem. Also it saves up a lot of space. The thing is… it is not “officially” compatible with esphome; I still tried to give it a shot.

Because it switches using transistors, it can be controlled via PWM and this is mostly useful in case someone had to control speed of motors. So I thought that if instead than using PWM I use a standard pin with digital logic, it would be no problemo. The logic is, to open the valve I apply a short impulse on A1a and the valve stays open, then to close it I apply a short impulse on A1b that inverts the poles and closes the valve.

code:

esphome:
  name: esp-garden
  platform: ESP8266
  board: d1_mini

switch:
  # the following template is for the h-bridge control channel 1
  - platform: template
    name: "Zone 1"
    turn_on_action:
      - switch.turn_on: a1a
    turn_off_action:
      - switch.turn_on: a1b
  # internal h-bridge channel 1
  - platform: gpio
    name: "Open Zone 1 (a1a)"
    id: a1a
    pin: 
      number: D5
      inverted: true
    interlock: [a1b]
    on_turn_on:
      - delay: 20ms
      - switch.turn_off: a1a
    internal: true
  - platform: gpio
    name: "Close Zone 1 (a1b)"
    id: a1b
    pin: 
      number: D6
      inverted: true
    interlock: [a1a] 
    on_turn_on:
      - delay: 20ms
      - switch.turn_off: a1b
    internal: true

The problem here is, on homeassistant in the UI I see the switch “Zone 1” but when I turn it on it stays there for a few seconds and then turns back off again without doing anything at all. I want to be able to control with a switch the valve so that when I turn the switch on esphome enables the bridge for 20ms and then turns it off, and vice versa does the same but with the polarity inversion.

Does anyone know what is going on here?

Did you find a fix ?

Hi Luca,
did you find a solution? can you share here?