rospogrigio / localtuya

local handling for Tuya devices
GNU General Public License v3.0
2.86k stars 549 forks source link

Cannot use sensor filters of ESPHome to calibrate plugs #420

Open Tryfos opened 3 years ago

Tryfos commented 3 years ago

I've managed to integrate with localtuya a couple of smart plugs, which also have power monitor. I've thought of using YAML, in order to use sensors' filters of ESPHome https://esphome.io/components/sensor/index.html and calibrate the plugs (calibrate_linear, offset etc). This should work, since when YAML is used, these entities are integrated as normal sensors in ESPHome logic. But the value "filters" in YAML localtuya is not accepted and throws an error. If I remove the filters, the plugs are integrated normally. Does this integration support normal sensor filters of ESPHome? If not, how can we programmatically calibrate the received power readings of the plugs?

postlund commented 3 years ago

ESPHome and Home Assistant are different products, you can't mix syntax between them like that. They both use YAML and ESPHome is heavily inspired by the YAML syntax used by Home Assistant, but that's more or less it. You can either use a template sensor and provide a mathematical expression yourself, or use the filter sensor for this.

Not sure what you are trying to calibrate though? They should already be fairly calibrated from factory.

Tryfos commented 3 years ago

ESPHome and Home Assistant are different products, you can't mix syntax between them like that. They both use YAML and ESPHome is heavily inspired by the YAML syntax used by Home Assistant, but that's more or less it. You can either use a template sensor and provide a mathematical expression yourself, or use the filter sensor for this.

Not sure what you are trying to calibrate though? They should already be fairly calibrated from factory.

I think it would be fairly difficult (at least for me) to create a template with mathematical expression for eg. "calibrate_linear" of ESPHome, which I use in flashable plugs... I'll take a look at the "filter" integration, even though from a quick look I think it is for other use cases. Are you sure that the plugs are calibrated from factory? We're talking about 5-7€ worth of plugs...

Edit: Just checked, voltage is off at least 2 v, wattage is less accurate, 20-40W constant difference. So there must be a way to calibrate the plugs somehow.

postlund commented 3 years ago

I mean, the plugs must be calibrated at the factory as you can only calibrate them with a known load. As they are cheap, I would expect there to be some level if inaccuracy of course (you get what you pay for), but that shouldn't be too large as it would render the functionality useless. How are you measuring the load as comparison?

Tryfos commented 3 years ago

I mean, the plugs must be calibrated at the factory as you can only calibrate them with a known load. As they are cheap, I would expect there to be some level if inaccuracy of course (you get what you pay for), but that shouldn't be too large as it would render the functionality useless. How are you measuring the load as comparison?

With a fairly good calibrated wall power meter and a load with constant power needs(hair dryer, old style light bulb no led etc.). It is described here more or less https://frenck.dev/calibrating-an-esphome-flashed-power-plug/ . Would it be difficult to incorporate the sensor filters of esphome in localtuya logic?

postlund commented 3 years ago

It seems strange to me that the measurements would be so off, but I can't argue if you have made external measurements.

Adding filters similar to what's in ESPHome is not something I want to see in localtuya. It should be implemented either in the base sensor component (assuming core devs find that appropriate) or extensions should be made to the filter integration. All those filters are potentially interesting for other people using non-tuya devices as well, so it should be places somewhere it can be re-used.

marianomd commented 1 year ago

Hi!! This project is nuts, thank you guys.

Regarding calibration, I bought a smart plug that reads 10 V less than my multimeter.

I'm intrigued by these values, as they could be the calibration constants:

image

Is there any possibility to write these to the device? Or they are read-only?

Thanks

marianomd commented 1 year ago

It seems they are read only :(. Will have to use HA filters.

image

https://developer.tuya.com/en/docs/iot/1s?id=K9tp11c62qjq8