openshwprojects / OpenBK7231T_App

Open source firmware (Tasmota/Esphome replacement) for BK7231T, BK7231N, BL2028N, T34, XR809, W800/W801, W600/W601 and BL602
https://openbekeniot.github.io/webapp/devicesList.html
1.38k stars 234 forks source link

Allow SetupEnergyStats for non BL / Tuya power meters #978

Open analystcmyk opened 9 months ago

analystcmyk commented 9 months ago

The power statistics command (SetupEnergyStats) is a great tool, but unfortunately hardcoded to be used only for power measured using the BL driver.

definition

Suggestion : allow statistics to be collected for other type of meters, eg Tuya powermeters.

My autoexec.bat ( using EARU EAMPDW-TY-63 ) :

startdriver SSDP
startDriver TuyaMCU
startDriver NTP
tuyaMcu_defWiFiState 4
setChannelType 1 toggle
linkTuyaMCUOutputToChannel 16 1 1
setChannelType 2 Voltage_div10
setChannelType 3 Power
setChannelType 4 Current_div1000
setChannelType 5 EnergyTotal_kWh_div100
linkTuyaMCUOutputToChannel 1 0 5
linkTuyaMCUOutputToChannel 6 RAW_TAC2121C_VCP
linkTuyaMCUOutputToChannel 15 1 7
ntp_timeZoneOfs 2:00

Thank you for considering.

analystcmyk commented 9 months ago

Maybe overstretching and out of scope here, but possibly this would not be strictly to be limited to power.

Any value could be "statted" using the sample period , but it would mean having to calculate and store min/max/avg per sample period, while currently we only hold the total per sample period.

Example : Value = Temperature , Sample time = 5 seconds, Sample count = 2

image

analystcmyk commented 4 months ago

Any plans for moving the stats out of the BLE code? Even just a daily diff like https://github.com/openshwprojects/OpenBK7231T_App/blob/c07f66f3818c7067e60acbf27cb1a5d86f77e692/src/driver/drv_bl_shared.c#L187 would be nice (instead of the full fledged configurable powermeter)

Maybe a script can do the same thing, ill try that as well.

analystcmyk commented 4 months ago

Show energy usage of last hour:

Of course this is not a real running hourly total, as it will reset to zero every hour. A moving window would be better, sharing here anyway.

// measure 1 hr usage, $CH5 holds total kWh

// store the last measured value
setChannelType 10 EnergyTotal_kWh_div100
setChannelLabel 10 EnergyTotal_1hr

// set to total when booting 
// check if you need a wait time, some tuya's do not have this value yet when booting
setChannel 10 $CH5;

// channel to hold the usage of last hour
setChannelType 11 EnergyTotal_kWh_div100
setChannelLabel 11 EnergyUse_1hr

// every hour, reset the counter
addRepeatingEventID 3600 -1 111 measure1hr

// and keep updating the hour totals when the energytotal changes 
addEventHandler OnChannelChange 5 measure1hr_update

alias measure1hr backlog setChannel 11 $CH5-$CH10; publishChannel 10; setChannel 10 $CH5;
alias measure1h_rupdate backlog setChannel 11 $CH5-$CH10; publishChannel  11;