简体   繁体   English

数据库/MySQL - 计算给定表的能耗

[英]Database / MySQL - Calculate Energy Consumption for given table

i would like to calculate the sum of the energy consumption from this openhab persistence table, which contains the rows for current energy consumption of my lamps:我想从这个 openhab 持久性表中计算能源消耗的总和,其中包含我的灯当前能源消耗的行:

+---------------------+--------+
| time                | value  |
+---------------------+--------+
| 2022-10-08 21:48:08 |      0 |
| 2022-10-08 21:56:52 |  13.48 |
| 2022-10-08 21:56:57 |  13.63 |
| 2022-10-08 21:57:06 |  13.68 |
| 2022-10-08 21:57:09 |  13.63 |
| 2022-10-08 21:57:22 |  13.68 |
| 2022-10-08 21:57:39 |  13.73 |
| 2022-10-08 21:57:52 |  13.68 |
| 2022-10-08 21:57:54 |  13.73 |
| 2022-10-08 21:58:06 |  13.78 |
| 2022-10-08 21:58:09 |  13.73 |
| 2022-10-08 21:58:22 |  13.78 |
| 2022-10-08 21:58:37 |  13.73 |
| 2022-10-08 21:58:39 |  13.78 |
| 2022-10-08 21:58:54 |  13.83 |
| 2022-10-08 21:59:06 |  13.78 |
| 2022-10-08 21:59:22 |  13.73 |
| 2022-10-08 21:59:24 |  13.78 |
| 2022-10-08 22:00:11 |  13.73 |

this table represents the power consumption value at the current date and time.此表表示当前日期和时间的功耗值。

means: the power consumption didn´t change until the next dataset is inserted with a new value.意思是:在下一个数据集插入新值之前,功耗不会改变。 the power-consumtion is constant for the time between the current and the following dataset with different value.对于当前和具有不同值的后续数据集之间的时间,功耗是恒定的。

i have no idea how to get which wattage was for what duration.我不知道如何获得持续多长时间的瓦数。 my goal is to get the sum, which should be in watthours oder kilowatthours.我的目标是得到总和,它应该以瓦时或千瓦时为单位。

anybody has any hints for me how to do it?有人对我有任何提示吗? thx谢谢

Edit: 2022-10-08 21:59:06 to 2022-10-08 21:59:22 the current wattage is 13.78.编辑:2022-10-08 21:59:06 至 2022-10-08 21:59:22 当前瓦数为 13.78。

13.78W for 16 seconds: 13.78W * 16s = 220,48 Ws 13.78W 持续 16 秒:13.78W * 16s = 220,48 Ws

searched here, is the sum of the obove calculated Wattseconds.在这里搜索,是上面计算的瓦秒的总和。

We first have to JOIN the table itself with the following row (nearest time after current rows time):我们首先必须将表本身与以下行(当前行时间之后的最近时间)连接起来:

SELECT A.time as time1, B.time as time2, A.value as value1, B.value as value2 FROM OpenHAB.item0022 as A left join OpenHAB.item0022 as B ON B.time = (SELECT time FROM item0022 WHERE time > A.time ORDER BY time ASC LIMIT 1)

after this is done, we can get the difference in seconds between the current and the following dataset, with some calculations, and divide it by 60 seconds * 60 minutes * 1000 (Watt to kWatt) we get the correct result.完成后,我们可以得到当前数据集和后续数据集之间的秒数差异,通过一些计算,并将其除以 60 秒 * 60 分钟 * 1000(瓦特到千瓦特)我们得到正确的结果。

SELECT SUM(TIMESTAMPDIFF(SECOND,time1,time2)*value1)/(60*60*1000) as Sum_kWh FROM 

    (
SELECT A.time as time1, B.time as time2, A.value as value1, B.value as value2 FROM OpenHAB.item0022 as A left join OpenHAB.item0022 as B ON B.time = (SELECT time FROM item0022 WHERE time > A.time ORDER BY time ASC LIMIT 1)
) as q1 WHERE value1 <> 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM