I need some advice on integrating an
electric fence into HA. I have sorted the basics, but I'm stuck at the final step, so apologies for the long post. Here is the entire thing.
I used a Sonoff SV to get control of my Nemtek Merlin M18S energizer. I removed the resistor to isolate the relay as a dry contact. I connected up a 12V relay to the siren connector and the relay's normally open contact to GPIO14 on the SV. The SV has ESPHome flashed on it and so far it works perfectly. However, there is no easy way to see whether the energizer is actually armed/energising the fence except for physically looking at the LED indicator on the energizer. This means that when I switch the energizer on/off via Home Assistant, I have no way to know whether it actually turned on or not.
After a bunch of research, I decided the best, safest and neatest way to tackle this is by soldering two wires to the LED indicator, connecting those to an opto-isolator (I decided on the 4N25) and have the that signal to GPIO4 or GPIO5 whether the fence is armed or not. Simple enough. However, I know just enough about electronics and programming to be a danger to myself. Having done all of this, I am getting log data that I cannot square with what I am expecting or with what I know to fix.
1. I connected the opto-isolator directly to the LED indicator (pin 1 and 2). All the pin-outs ask for a current limiting resistor to pin 1; however, this is usually for higher voltage signals. So I opted to leave that out, because when I measure the voltage drop across the LED is was just over 1V (also, when the opto-isolator is connected the LED indicator no longer blinks - I assume that the opto-isolator is draining all the juice).
2. The opto-isolator's output is intermittent but constant even when the fence is not energised. What i mean by this is the signal I receive in the logs switches on-off very quickly. When the fence is armed 16 or 17 times per second; when unarmed 5 times every three seconds. I did not expect this as the indicator LED itself under normal circumstances flashes about every second and this is the signal that I had intended to interpret as a sensor.
So my question is: what is happening here? Is this an expected behaviour, but I just do not know how to interpret it? Or should I change my circuit slightly by adding something to cancel the 'noise' (I have no idea what I'm saying here - sorry!) - a pull-down resistor between the ground and live of the LED before the opto-isolator or some kind of capacitor (when I said I know just enough to be dangerous, this is what I'm talking about

)? Is there a problem with my ESP programming not interpreting the signals correctly? I tried on_multi_click first but had not sensor output. With my current code I am at least getting a sensor value, but the value is inconsistent even after tweaking the milliseconds many times to try and catch the perfect mid-way.
Here is my truncated code for the flashed SV:
binary_sensor:
- platform: gpio
pin:
number: GPIO14
mode: INPUT_PULLUP
inverted: true
name: Alarm
device_class: tamper
- platform: gpio
pin:
number: GPIO4
inverted: False
mode: INPUT_PULLUP
id: good_LED
name: Good LED
device_class: power
entity_category: diagnostic
disabled_by_default: true
on_click:
- min_length: 0ms
max_length: 100ms
then:
- logger.log:
level: VERBOSE
format: "Armed"
- text_sensor.template.publish:
id: active
state: "Fence Armed"
- min_length: 200ms
then:
- logger.log:
level: VERBOSE
format: "Unarmed"
- text_sensor.template.publish:
id: active
state: "Fence Unarmed"
- platform: status
name: Status
switch:
- platform: gpio
pin: GPIO12
id: relay
- platform: template
icon: "mdi:fence-electric"
name: Fence Switch
turn_on_action:
- switch.turn_on: relay
- delay: 750ms
- switch.turn_off: relay
sensor:
- platform: wifi_signal
name: WIFI Strength
update_interval: '60s'
button:
- platform: restart
name: Restart
text_sensor:
- platform: wifi_info
ip_address:
name: IP Address
- platform: template
icon: mdi:information-outline
id: active
name: Armed
Here is some log output for when the fence is unarmed:
[17:58:48][D][text_sensor:064]: 'Armed': Sending state 'Fence Armed'
[17:58:48][D][binary_sensor:036]: 'Good LED': Sending state ON
[17:58:48][D][binary_sensor:036]: 'Good LED': Sending state OFF
[17:58:48][D][text_sensor:064]: 'Armed': Sending state 'Fence Armed'
[17:58:48][D][binary_sensor:036]: 'Good LED': Sending state ON
[17:58:48][D][binary_sensor:036]: 'Good LED': Sending state OFF
[17:58:48][D][text_sensor:064]: 'Armed': Sending state 'Fence Armed'
[17:58:48][D][binary_sensor:036]: 'Good LED': Sending state ON
[17:58:48][D][binary_sensor:036]: 'Good LED': Sending state OFF
[17:58:48][D][text_sensor:064]: 'Armed': Sending state 'Fence Armed'
[17:58:48][D][binary_sensor:036]: 'Good LED': Sending state ON
[17:58:48][D][binary_sensor:036]: 'Good LED': Sending state OFF
[17:58:48][D][text_sensor:064]: 'Armed': Sending state 'Fence Armed'
[17:58:48][D][binary_sensor:036]: 'Good LED': Sending state ON
[17:58:48][D][binary_sensor:036]: 'Good LED': Sending state OFF
[17:58:48][D][text_sensor:064]: 'Armed': Sending state 'Fence Armed'
[17:58:48][D][binary_sensor:036]: 'Good LED': Sending state ON
[17:58:49][D][binary_sensor:036]: 'Good LED': Sending state OFF
[17:58:49][D][binary_sensor:036]: 'Good LED': Sending state ON
[17:58:49][D][binary_sensor:036]: 'Good LED': Sending state OFF
[17:58:49][D][text_sensor:064]: 'Armed': Sending state 'Fence Armed'
[17:58:49][D][binary_sensor:036]: 'Good LED': Sending state ON
[17:58:49][D][binary_sensor:036]: 'Good LED': Sending state OFF
[17:58:49][D][text_sensor:064]: 'Armed': Sending state 'Fence Armed'
[17:58:49][D][binary_sensor:036]: 'Good LED': Sending state ON
[17:58:49][D][binary_sensor:036]: 'Good LED': Sending state OFF
[17:58:49][D][text_sensor:064]: 'Armed': Sending state 'Fence Armed'
[17:58:49][D][binary_sensor:036]: 'Good LED': Sending state ON
[17:58:49][D][binary_sensor:036]: 'Good LED': Sending state OFF
[17:58:49][D][text_sensor:064]: 'Armed': Sending state 'Fence Armed'
[17:58:49][D][binary_sensor:036]: 'Good LED': Sending state ON
[17:58:49][D][binary_sensor:036]: 'Good LED': Sending state OFF
[17:58:49][D][text_sensor:064]: 'Armed': Sending state 'Fence Armed'
[17:58:49][D][binary_sensor:036]: 'Good LED': Sending state ON
[17:58:49][D][binary_sensor:036]: 'Good LED': Sending state OFF
[17:58:49][D][text_sensor:064]: 'Armed': Sending state 'Fence Armed'
[17:58:49][D][binary_sensor:036]: 'Good LED': Sending state ON
As you can see, the sensor is not getting the right idea and mostly it just flickers between 'armed' and 'unarmed'.
Any advice would be appreciated. Please let me know if you need any clarification.