LSC outdoor dual socket (libretiny)

First go at tuya-cloudcutter. Flashed esphome on my device but can’t get it work properly. The challenge (for me) is the separate pins for on and off.

I’ve tried to solve it with the following code but the switches behave eratically. For example switching one relay on works but then shortly after switches off again by itself.

Any help much appreciated.

I base my yaml of of the following info: https://github.com/tuya-cloudcutter/tuya-cloudcutter.github.io/blob/4934b68d31df1b00eb7e73ab4811919f50d5de20/devices/lsc-3004200-wifi-outdoor-dual-socket.json

esphome:
  name: 2x_outdoor_power

bk72xx:
  board: generic-bk7231t-qfn32-tuya

logger:

web_server:

captive_portal:

mdns:

api:
  password: ""

ota:
  password: ""

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

text_sensor:
  - platform: libretiny
    version:
      name: LibreTiny Version


binary_sensor:
  - platform: gpio
    id: binary_switch_all
    pin:
      number: P7
      inverted: true
      mode: INPUT_PULLUP



switch:
  - platform: template
    name: "Relay 1"
    turn_on_action:
      - switch.turn_off: switch_1_off 
      - switch.turn_on: switch_1_on  
      - logger.log: "Switch Turned On!"
      - light.turn_on: stat_led
    turn_off_action:
      - switch.turn_off: switch_1_on 
      - switch.turn_on: switch_1_off  
      - logger.log: "Switch Turned Off!"
      - light.turn_off: stat_led

  - platform: template
    name: "Relay 2"
    turn_on_action:
      - switch.turn_off: switch_2_off 
      - switch.turn_on: switch_2_on  
      - logger.log: "Switch Turned On!"
      - light.turn_on: stat_led
    turn_off_action:
      - switch.turn_off: switch_2_on 
      - switch.turn_on: switch_2_off  
      - logger.log: "Switch Turned Off!"
      - light.turn_off: stat_led     

  - platform: gpio
    id: switch_1_on
    name: 1on
    pin: P14 

  - platform: gpio
    id: switch_2_on
    name: 2on    
    pin: P9

  - platform: gpio
    id: switch_1_off
    name: 1off    
    pin: P26

  - platform: gpio
    id: switch_2_off
    name: 2off
    pin: P8  

#  - platform: gpio
#    id: switch_all
#    name: Relay 1&2
#    pin: P7
#    on_turn_on:
#    - switch.turn_off: switch_1_off
#    - switch.turn_on: switch_1_on
#    - switch.turn_off: switch_2_off
#    - switch.turn_on: switch_2_on
#    - logger.log: "Switch Turned On!"
#    - light.turn_on: stat_led
#    on_turn_off:
#    - switch.turn_on: switch_1_off
#    - switch.turn_off: switch_1_on
#    - switch.turn_on: switch_2_off    
#    - switch.turn_off: switch_2_on
#    - logger.log: "Switch Turned Off!"
#    - light.turn_off: stat_led


light:
  - platform: status_led
    id: stat_led
    name: "Switch state"
    pin:
      number: P1
      inverted: true

#status_led:
#  pin:
#    number: P1
#    inverted: true

Are the relays not bistable relays that just require a pulse on a pin to switch on, and a pulse on another pin to switch off? The GitHub mentions a relay drive time of 60, not a 60ms pulse to switch on/off is it?

The relay will only switch on when the ‘off’ pin is off and the ‘on’ pin is on. That is as far as I’ve come.
I would not know how to go about just sending a pulse.

I’ve think that the relay crashes/reboots constantly as the uptime sensor I’ve added never reaches one minute.

Hi,

don’t know if you already got it fixed, but i just got mine up and running with the below code.

esphome:
  name: garden-spike-1
  friendly_name: Garden Spike 1
  area: Terasse

bk72xx:
  board: generic-bk7231t-qfn32-tuya

# Enable logging
logger:

web_server:

captive_portal:

mdns:

# Enable Home Assistant API
api:
  encryption:
    key: "<Your API KEY>"

ota:
  password: "<Your OTA Password>"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  #manual_ip:
    #static_ip: 192.168.1.100
    #gateway: 192.168.1.1
    #subnet: 255.255.255.0
  ap:
    ssid: "Garden Spike 1.fb"
    password: "<YourPassword>"

button:
  - platform: restart
    name: Restart

debug:
  update_interval: 30s

text_sensor:
  - platform: debug
    reset_reason:
      name: Reset Reason
  - platform: libretiny
    version:
      name: LibreTiny Version
  - platform: wifi_info
    ip_address:
      name: IP Address

sensor:
  - platform: uptime
    name: Uptime

binary_sensor:
  - platform: gpio
    id: binary_switch_all
    pin:
      number: P7
      inverted: true
      mode: INPUT_PULLUP
  - platform: template
    id: relay_state
    name: "LED"
    lambda: |-
      if (id(relay1).state || id(relay2).state) {
        return true;
      }
      else {
        return false;
      }
    on_state:
      if:
        condition:
          - binary_sensor.is_on: relay_state
        then:
          - light.turn_on: stat_led
        else:
          - light.turn_off: stat_led

switch:
  - platform: gpio
    id: switch_1_on
    pin: P14
  - platform: gpio
    id: switch_2_on
    pin: P9
  - platform: gpio
    id: switch_1_off
    pin: P26
  - platform: gpio
    id: switch_2_off
    pin: P8
  - platform: template
    name: "Relay 1"
    id: relay1
    turn_on_action:      
      - switch.turn_off: switch_1_off
      - switch.turn_on: switch_1_on
      - logger.log: "Switch Turned On!"
      - delay: 0.1s
      - switch.turn_off: switch_1_on
      - switch.template.publish:
          id: relay1
          state: true
    turn_off_action:
      - switch.turn_off: switch_1_on
      - switch.turn_on: switch_1_off
      - logger.log: "Switch Turned Off!"
      - delay: 0.1s
      - switch.turn_off: switch_1_off
      - switch.template.publish:
          id: relay1
          state: false
  - platform: template
    name: "Relay 2"
    id: relay2
    turn_on_action:
      - switch.turn_off: switch_2_off
      - switch.turn_on: switch_2_on
      - logger.log: "Switch Turned On!"
      - delay: 0.1s
      - switch.turn_off: switch_2_on
      - switch.template.publish:
          id: relay2
          state: true
    turn_off_action:
      - switch.turn_off: switch_2_on
      - switch.turn_on: switch_2_off
      - logger.log: "Switch Turned Off!"
      - delay: 0.1s
      - switch.turn_off: switch_2_off
      - switch.template.publish:
          id: relay2
          state: false

light:
  - platform: binary
    output: light_output
    id: stat_led

output:
  - id: light_output
    platform: gpio
    pin:
      number: P1
      inverted: true

I also changed the LED to be only on if any of the relays is switched on.
Else it will be turned off.

So it’s a bit more like the original behaviour :slight_smile:


And one more thing, that i just made:

I wanted the button on the top to be the main switch, so i could turn both relays on in and off.

There was a little issue with it, because if you just let them turn on and off in a sequence it will sound a bit odd, so i made it run parallel.

If anybody wants this, just add this 2 blocks in and delete the old binary_sensor section.

script:
  - id: switches_on
    mode: parallel
    then:
      - switch.turn_on: relay1
      - switch.turn_on: relay2
  - id: switches_off
    mode: parallel
    then:
      - switch.turn_off: relay1
      - switch.turn_off: relay2

binary_sensor:
  - platform: gpio
    id: binary_switch_all
    pin:
      number: P7
      inverted: true
      mode: INPUT_PULLUP
    on_release:
      if:
        condition:
          - binary_sensor.is_off: relay_state
        then:
          - script.execute: switches_on
        else:
          - script.execute: switches_off
  - platform: template
    id: relay_state
    name: "LED"
    lambda: |-
      if (id(relay1).state || id(relay2).state) {
        return true;
      }
      else {
        return false;
      }
    on_state:
      if:
        condition:
          - binary_sensor.is_on: relay_state
        then:
          - light.turn_on: stat_led
        else:
          - light.turn_off: stat_led