Xinyuan-LilyGO / LilyGo-T-Relay

MIT License
70 stars 28 forks source link

Prevent relay trigger on boot or reset with esphome code #24

Open davidusb-geek opened 9 months ago

davidusb-geek commented 9 months ago

I'm using a very similar code to the one here for esphome and Home Assistant. Everything works fine except that when the board resets or reboot all the relay are triggered. Very inconvenient when controlling a garage door for example. Any suggestions to solve this and avoid these relay triggering? I have tried some solutions using the relay options on esphome (ALWAYS_OFF, etc.) but without success. Any help will be much appreciated.

Misiu commented 9 months ago

@davidusb-geek what model or relay are you using (T-Relay, T-Relay 8, or T-Relay S3)? I'm thinking about ordering those relays and I'd like to know did you solved the issue you described. I also want to use it to control garage doors, so I can't allow random opening. Thank you for the reply.

davidusb-geek commented 9 months ago

I'm using the t-relay 8. The issue is not solved, it's open. So I disconnected the garage door for now.

Misiu commented 9 months ago

Sad to hear that. Mayby try to contact support via website chat.

davidusb-geek commented 9 months ago

Oh man, I've just solved this after months looking for a solution! So the thing is that I've already tested with options mode: INPUT_PULLUP and inverted: True but without good results. But my problem was with indentation in my yaml for esphome. These parameters were just being ignored. Today I just found that the correct way to pass this parameters and make my switch work without triggering at boot is this:

First define the output pins:

output:
  - platform: gpio
    pin: 
      number: GPIO33
      mode: INPUT_PULLUP
      inverted: True
    id: output_pin_1 # garage door

Then I define my switch for the garage door like this:

switch:
  - platform: output
    restore_mode: ALWAYS_OFF
    id: garage_door_bare_switch
    output: output_pin_1
  - platform: template
    restore_mode: ALWAYS_OFF
    name: "Garage Door Switch"
    icon: "mdi:garage"
    id: garage_door_bare_template_switch
    lambda: 'return id(garage_door_bare_switch).state;'
    turn_on_action:
      - switch.turn_on: garage_door_bare_switch
      - delay: 0.5s
      - switch.turn_off: garage_door_bare_switch
    turn_off_action:
      - switch.turn_on: garage_door_bare_switch
      - delay: 0.5s
      - switch.turn_off: garage_door_bare_switch

And that's it, works like charm.

Misiu commented 9 months ago

@davidusb-geek thank you for sharing the solution :) Now I think I order the boards with confidence. Did you maybe attach the board to a DIN rail? I was searching for a mounting solution and I would like to know what you choose.

davidusb-geek commented 9 months ago

Wait! I spoke too soon! Actually the switch is not working. I thought it was fine because it didn't trigger at boot and it was showing fine on Home Assistant (and it was very late yesterday night) . But it is not working, the switch is not responding. My guess is that the INPUT_PULLUP is not supported by the hardware. I'm reopening the issue :-(

davidusb-geek commented 9 months ago

Yes I'm using DIN rail. I'll post the aliexpress link for that piece. It is a shame that I have this issue because the board quality is very good.

davidusb-geek commented 9 months ago

These DIN supports work fine: 1 Pair DRG-01 PCB Mounting Brackets Screws Green For DIN 35 Mounting Rails Adapter Replacements Parts https://a.aliexpress.com/_EwgujuV

lewisxhe commented 9 months ago

Relay4/realy8 should not have this problem, but there is indeed a chance that this problem will occur on realy6

davidusb-geek commented 9 months ago

Ok I will try the INPUT_PULLUP option on relay4 or relay8 and see how it goes.

davidusb-geek commented 9 months ago

Relay4/realy8 should not have this problem, but there is indeed a chance that this problem will occur on realy6

I just tested it on relay 4 (ie. GPIO12), still doesn't works.

Misiu commented 9 months ago

Relay4/realy8 should not have this problem, but there is indeed a chance that this problem will occur on realy6

@lewisxhe could you please say more about relay6 (T-Relay S3)? I need to be 100% sure that the relays won't switch on/off after power loss. I want to control my garage door, so if this issue occurs my garage will open!

davidusb-geek commented 9 months ago

Relay4/realy8 should not have this problem, but there is indeed a chance that this problem will occur on realy6

So I completely misinterpreted this reply.

Misiu commented 9 months ago

Relay4/realy8 should not have this problem, but there is indeed a chance that this problem will occur on realy6

So I completely misinterpreted this reply.

I'm not sure if I understand it right. I'm assuming it was related to product types and not to relay in T-Relay 8

davidusb-geek commented 9 months ago

Ok, got it working now! Tested it for real and everything is working fine and as expected.

I tested many options and finally got it working with this configutaion:

output:
  - platform: gpio
    pin: 
      number: GPIO12
      inverted: True
      mode:
        pullup: True
        output: True
    id: output_pin_1 # garage door

The switch part of the yaml code is the same that I posted earlier in this issue.

Cheers!!!

Misiu commented 9 months ago

@davidusb-geek thank you for sharing a solution. Yesterday I ordered a T-relay 8 board, it should arrive in about 3-4 weeks so I'll also test all the options.

szab0lcs commented 6 months ago

I got it working with an easier method, the mode: OUTPUT was the key part:

sn74hc595:
  - id: "sn74hc595_hub"
    data_pin: 7
    clock_pin: 5
    latch_pin: 6
    sr_count: 2

# Individual outputs
switch:
  - platform: gpio
    # SN74HC595 Pin #0
    name: "Switch 1"
    pin:
      sn74hc595: sn74hc595_hub
      # Use pin number 0
      number: 0
      inverted: false
      mode: OUTPUT
    id: relay1