Rule for calculating the cost of electricity

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 + " в тек.мес.)" )	   

end
it doesn’t work,what could be wrong with it

??
Not even a little clue for us?

You could try to find out.
Use logInfo()

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

The logs are empty,most likely it does not start.

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*

import java.lang.Math
import java.util.Calendar
import java.util.Date
import java.util.TimeZone
import java.text.SimpleDateFormat
import java.util.List //https://github.com/openhab/openhab/wiki/Actions#mail-actions
import java.util.Random

import java.lang.Double.*
import java.lang.String.*
maybe there’s something missing here

Are you using openHAB version 1? It makes a difference. Most likely you do not want any imports at all.

openHAB version 3.2

You do not want any imports at all.

Does your xxx.rules file get loaded? Your openhab.log will tell you at boot time and after each edit.

What kind of file, I have a lot of them for some reason. with respect

2021-07-16 16:32:48.198 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘Meteo.rules’
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: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

As you are using OH3, “now” is a zonedDateTime object and you will have to work with different methods to the OH2 rule that you copied.

is there any way to fix it for version 3?

Yes. Example

Does not count the cost, per hour and per day and in the current month for some reason, can you help solve this problem? Thank you in advance.

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 + " в тек.мес.)" )	   

end

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

Thank you for your help,I set everything up,it worked.There is one question left,how can I send it to the mail now?

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.