Here is my rule
rule “PZEM_Energy_WH”
when
Time cron “0/45 * * * * ?” //каждые 45 сек
then
var Number KWh = PZEM_Energy_WH.state as DecimalType
var Number Kal
var Number Dif
var Number Pri
if (PZEM_Energy_WH.state instanceof DecimalType) {
Kal = PZEM_Energy_WH.minimumSince(now.toDateMidnight.withDayOfMonth(1), "rrd4j").state as DecimalType //на начало месяца
//Kal = (PZEM_Energy_WH.minimumSince(now.toDateMidnight.withDayOfMonth(1).withMonthOfYear(1), "rrd4j").state as DecimalType) //на начало года //https://knx-user-forum.de/forum/supportforen/openhab/33557-anf%C3%A4nger
postUpdate(PZEM_Energy_WH_Kalends, Kal.doubleValue )
Dif = KWh - Kal
postUpdate(PZEM_Energy_WH_Consumption, Dif.doubleValue )
Pri = Dif * 3.16 //3,16 рубля за кВт*ч
PZEM_Energy_WH_Consumption_Price.postUpdate(PZEM_Energy_WH_Consumption_Price, Pri.doubleValue )
}
val CropDigital1 = String::format("%.2f", KWh.doubleValue ) //формат строки для sitemap
val CropDigital2 = String::format("%.2f", Dif.doubleValue ) //формат строки для sitemap
postUpdate(PZEM_Energy_WH_general, CropDigital1 + " (" + CropDigital2 + " в тек.мес.)" )
Does your rule run at all?
Find out.
Are the values what you expect?
Find out.
Example
rule “PZEM_Energy_WH”
when
Time cron “0/45 * * * * ?” //каждые 45 сек
then
logInfo("test", "My rule has started")
var Number KWh = PZEM_Energy_WH.state as DecimalType
logInfo("test", "Variable KWh is {}", KWh)
... and so on
2021-07-16 16:32:48.269 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘Meteo.rules’ has errors, therefore ignoring it: [55,45]: no viable alternative at input ‘Watermeter_Total’
[55,91]: mismatched input ‘)’ expecting ‘end’
2021-07-16 16:42:28.587 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘PZEM_Energy_WH.rules’
2021-07-16 16:42:28.593 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘PZEM_Energy_WH.rules’ is either empty or cannot be parsed correctly!
2021-07-16 16:42:29.595 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘PZEM_Energy_WH.rules’
2021-07-16 16:42:29.599 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘PZEM_Energy_WH.rules’ is either empty or cannot be parsed correctly!
2021-07-16 16:42:53.817 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘PZEM_Energy_WH.rules’
2021-07-16 16:43:00.433 [INFO ] [org.openhab.core.model.script.test ] - My rule has started
2021-07-16 16:43:00.438 [INFO ] [org.openhab.core.model.script.test ] - Variable KWh is 440.43
2021-07-16 16:43:00.450 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘PZEM_Energy_WH-1’ failed: ‘toDateMidnight’ is not a member of ‘java.time.ZonedDateTime’; line 13, column 43, length 18 in PZEM_Energy_WH
2021-07-16 16:43:45.052 [INFO ] [org.openhab.core.model.script.test ] - My rule has started
2021-07-16 16:43:45.053 [INFO ] [org.openhab.core.model.script.test ] - Variable KWh is 440.43
2021-07-16 16:43:45.054 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘PZEM_Energy_WH-1’ failed: ‘toDateMidnight’ is not a member of ‘java.time.ZonedDateTime’; line 13, column 43, length 18 in PZEM_Energy_WH
I removed it tonight,it began to count for the beginning of the month, then
2021-07-16 16:57:00.050 [INFO ] [org.openhab.core.model.script.test ] - My rule has started
2021-07-16 16:57:00.051 [INFO ] [org.openhab.core.model.script.test ] - Variable KWh is 440.49
2021-07-16 16:57:00.052 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘PZEM_Energy_WH-1’ failed: ‘toDateMidnight’ is not a member of ‘java.time.ZonedDateTime’; line 13, column 43, length 18 in PZEM_Energy_WH
2021-07-16 16:57:00.586 [INFO ] [org.openhab.core.model.script.test ] - My rule has started
2021-07-16 16:57:00.587 [INFO ] [org.openhab.core.model.script.test ] - Variable KWh is 440.49
2021-07-16 17:06:00.359 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘PZEM_Energy_WH-1’ failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.BusEvent.postUpdate(org.openhab.core.items.Item,java.lang.String) on instance: null in PZEM_Energy_WH
rule “PZEM_Energy_WH”
when
Time cron “0/45 * * * * ?” //каждые 45 сек
then
logInfo(“test”, “My rule has started”)
var Number KWh = PZEM_Energy_WH.state as DecimalType
logInfo(“test”, “Variable KWh is {}”, KWh)
var Number Kal
var Number Dif
var Number Pri
if (PZEM_Energy_WH.state instanceof DecimalType) {
Kal = PZEM_Energy_WH.minimumSince(now.withDayOfMonth(1), "rrd4j").state as DecimalType //на начало месяца
//Kal = (PZEM_Energy_WH.minimumSince(now.toDateMidnight.withDayOfMonth(1).withMonthOfYear(1), "rrd4j").state as DecimalType) //на начало года //https://knx-user-forum.de/forum/supportforen/openhab/33557-anf%C3%A4nger
postUpdate(PZEM_Energy_WH_Kalends, Kal.doubleValue )
Dif = KWh - Kal
postUpdate(PZEM_Energy_WH_Consumption, Dif.doubleValue )
Pri = Dif * 3.16 //3,16 рубля за кВт*ч
PZEM_Energy_WH_Consumption_Price.postUpdate(PZEM_Energy_WH_Consumption_Price, Pri.doubleValue )
}
val CropDigital1 = String::format("%.2f", KWh.doubleValue ) //формат строки для sitemap
val CropDigital2 = String::format("%.2f", Dif.doubleValue ) //формат строки для sitemap
postUpdate(PZEM_Energy_WH_general, CropDigital1 + " (" + CropDigital2 + " в тек.мес.)" )
2021-07-16 17:39:00.080 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘PZEM_Energy_WH-1’ failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.BusEvent.postUpdate(org.openhab.core.items.Item,java.lang.String) on instance: null in PZEM_Energy_WH
2021-07-16 17:39:00.330 [INFO ] [org.openhab.core.model.script.test ] - My rule has started