AC input on Optocoupler - How to detect smooth out the input

Hello, I have 24VAC on an optocoupler to a digital input. I want home assistant to see no AC as a OFF and AC presence as ON. The issue is that it Turns ON and OFF 60 times a second when AC is present.

I expected that so I put an input filter in the code, but it doesn’t seem to do what I think it should:

  - platform: gpio
    pin:
      number: GPIO14
      mode:
        input: true
        pullup: true
    name: "HVAC_Opto_2"
    filters:
      delayed_off: 1s

Rectify the input to dc.

1 Like

You can get AC input optocouplers, they have 2 LEDs on the input.

ac input

I was really hoping to do this in software as the multi optocouplers module is quite neat and I don’t want to butcher it, any other software idea?

No, you can’t do everything in software! You need to first rectify it, then filter it with a RC filter. Choose a cutoff frequency of _____ (whatever, you can search the googles for a formula to figure out what RC time constant you need, or select a cutoff frequency that you want).
Here’s what’ll happen: the output of the filter will be 3 dB (50%) of the input. Then it will be safe level for your microcontroller inputs. (I’d still probably use an optocoupler, though, just to be ‘safe.’)
It’s really simple math (I’ve even done it myself), and the rest you can do in software!
(The formula for the RC time constant (in seconds) = pi * R (ohms) * C (microfarads).)

You shouldn’t need any hardware filtering.
Does the input show off when the AC is off and you see 60Hz when the input is on?
Is so, use delayed_off filter.
I’ve just re-read the docs on filter, maybe use delayed_on_off : 1s

Filters delayed_off or delayed_on_off won’t help. Documentation says:

if the binary sensor has stayed in the same state for at least the specified time period

Which would never work for an AC signal. At 60Hz it’s HIGH for 8ms only (actually less for GPIO to determine that it’s HIGH), then it goes back to LOW.

It works without hardware filtering.
I’ve just tried it, albeit on 50Hz…

I needed to remove the ‘pullup: True’ to allow testing with ‘50Hz hum’ on the input.
50Hz present, the led remained lit. (Not flashing / flickering).
50Hz not present, the led remained off.

With ‘pullup: True’ in use, the filter will need to be changed to - delayed_on: 500ms
:+1:

1 Like

EDIT/ADD Comment:
I retract my comments on this topic… “Sorry, my bad” (as they say).
I’m just learning about all the awesome dev tools that are built into HA.
I didn’t realize that…well, maybe you CAN do everything in software!
Anyway, please ignore my comments, I was just trying to be helpful. I’ll shut up for now.

I didn’t forget your suggestion, today I just turned on my air conditioning and it’s on the optocoupler, so part of the issue was an outdated version of ESPHome, but I also changed my code closer to your example and it works perfectly. Thanks for the great reply.

  # HVAC furnace cool (AC) command
  - platform: gpio
    pin:
      number: GPIO14
      inverted: true
      mode:
        input: true
        pullup: true
    filters:
      delayed_off: 500ms
1 Like