Many electricity meters have an LED. We need the ability to calculate electricity from the video from the meter. For example, such a HA component code needs to be written. Attention attention, the code is not working, dead and bad
import cv2
import numpy as np
import voluptuous as vol
from homeassistant.const import CONF_NAME, CONF_SOURCE
from homeassistant.components.camera import PLATFORM_SCHEMA, Camera
import homeassistant.helpers.config_validation as cv
CONF_COUNT_THRESHOLD = "count_threshold"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_SOURCE): cv.string,
vol.Optional(CONF_COUNT_THRESHOLD, default=0): cv.positive_int,
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the camera platform."""
name = config[CONF_NAME]
source = config[CONF_SOURCE]
count_threshold = config[CONF_COUNT_THRESHOLD]
add_entities([ElectricMeterCamera(name, source, count_threshold)])
class ElectricMeterCamera(Camera):
"""An implementation of a custom camera for detecting electric meter blinking."""
def __init__(self, name, source, count_threshold):
"""Initialize a custom camera."""
super().__init__()
self._name = name
self._source = source
self._count_threshold = count_threshold
self._state = None
self._prev_frame = None
self._count = 0
def camera_image(self):
"""Return the camera image."""
cap = cv2.VideoCapture(self._source)
ret, frame = cap.read()
cap.release()
if not ret:
return None
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Enhance the contrast on the image
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
gray = clahe.apply(gray)
# Apply median blur to reduce noise
gray = cv2.medianBlur(gray, 5)
if self._prev_frame is not None:
# Find the difference between the current frame and the previous frame
diff = cv2.absdiff(gray, self._prev_frame)
# Binarize the difference image using Otsu's method
thresh = cv2.threshold(
diff, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU
)[1]
# Use morphological opening to remove small objects in the image
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
# Count the number of blinks by counting the number of white pixels in the binarized image
self._count += cv2.countNonZero(thresh)
# Set the state to "on" if the blink count threshold has been reached
if self._count >= self._count_threshold:
self._state = "on"
self._count = 0
else:
self._state = "off"
self._prev_frame = gray
# Return the current camera image
_, jpeg = cv2.imencode(".jpg", frame)
return jpeg.tobytes()
@property
def name(self):
"""Return the name of the camera."""
return self._name
@property
def is_on(self):
"""Return the state of the camera."""
return self._state == "on"
def turn_off(self):
"""Turn off the camera."""
self
You probably can have some other help as well, if you post what kind of electricity meter you have.
There are dozens of other ways to read an electricity meter than a camera…
An by just looking at the code and the way how it is written, my guess would be it has been written by ChatGPT.
So, please give some more information and we can help to figure out what you should do and how to read the meter.
And to be honest, reading a meter by blinks is one of the most inaccurate ways, especially if the hardware is not independent from HA. Every restart of HA would cause that you would loose counts and then it would drift…
I do prefer more reading the meter through the optical port or any other port if available. More accurate and more information.
Frient has a device which counts blinks and uses Zigbee. I tried to hack it with a reed sensor for a gas meter, but unfortunately couldn’t sense the magnet inside.
I have such power meter with led. 6400 imp is kwh So I will be nice to take camera, and input in component 6400 imp is kwh, and have electric meter in my HA
# Count the number of blinks by counting the number of white pixels in the binarized image
self._count += cv2.countNonZero(thresh)
It has misinterpreted the request and suggested a wrong solution. I also tried ChatGPT before, and suggested functions what I didn’t know (pop count). The result was kinda the same as in the case of this code. It did not count what it supposed to. Misinterpreted my request and suggested what it “thought” to be was asked for…
It is a good tool to learn things. If you know what you are doing, how you want to do it, but you need to crosscheck what it suggests, due to these misinterpretation.
So, if you rely on image processing for counting by HA, during the period when HA does a reboot or an update and reboot, etc. then you are going to loose the processing of the images. It depends on your hardware what time does it take to do this. But it will definitely loose some frames. And after a few restarts, etc you will have a drift.
It is a better solution to count flashes by a photodetector, as somebody suggested.
My suggestion would be to use preferably and independent device which would do the counting and would not be effected by any interruption in HA and would just transfer to HA the counted numbers or sums every few seconds, or minute, whatever you need it for.
Or, the better way is to actually read the data from the meter. Most meters has an optical port for communication. The date, 2012 suggests that it should not be encrypted yet and could be used with an IR sensor, to read out the OBIS codes. But as you haven’t provided a brand and model, I cannot look up that is it supporting this or not.
If you really want to go down the root of image processing with camera, and you have a ESP32-CAM for the camera hardware, then I can suggest this project:
It processes the camera iamge on the ESP32 and reads the numbers from the display, if the display can be read without pressing a button on the meter. On your image it looks like you can read it that way, so could work far more better than the pulse count.
The other solution as pulse count, is the ESPHome way:
Or just build a Home Assistant Glow:
And finally with an IR sensor, read OBIS codes from the meter with ESPHome:
Otherwise out of the box, ready to use pulse counter with Zigbee you can get this:
They have got a fair range and long battery life. And they report total energy (kWh) and actual power consumption (Wh).