Good afternoon.
I have recently started to master OpenHAB.
Please help me create a rule for calculating the cost per hour, day,month with sending data by email 5 days before the end of the month.
I use OpenHAB 3.2.0
there are item
Number PZEM_Voltage_V " Voltage[%.2f In]”
Number PZEM_Current_A “Current [%.2f A]”
Number PZEM_Power_W “Power[%.2f W / h]”
Number PZEM_Energy_WH “Energy [%.2f kW]”
Channels:
Type number : PZEM_Voltage_V " Voltage”
Type number : PZEM_Current_A”Current"
Type number : PZEM_Power_W “Power”
Type number : PZEM_Energy_WH “Energy”
He shows everything,counts everything.
I didn’t find anything suitable on the forum.
The data is saved in rrd4j
English please, this is an international community !
rule “MercuryIncotex201KWh_Past”
when
Time cron “0 0/5 * * * ?” //каждые 5 минут
then
var Number KWh = MercuryIncotex201KWh.state as DecimalType
var Number Kal
var Number Dif
var Number Pri
if (MercuryIncotex201KWh.state instanceof DecimalType) {
Kal = MercuryIncotex201KWh.minimumSince(now.toDateMidnight.withDayOfMonth(1)).state as DecimalType //на начало месяца
//Kal = (MercuryIncotex201KWh.minimumSince(now.toDateMidnight.withDayOfMonth(1).withMonthOfYear(1), "rrd4j").state as DecimalType) //на начало года //https://knx-user-forum.de/forum/supportforen/openhab/33557-anf%C3%A4nger
postUpdate(MercuryIncotex201KWh_Kalends, Kal.doubleValue )
Dif = KWh - Kal
postUpdate(MercuryIncotex201KWh_Consumption, Dif.doubleValue )
Pri = Dif * 3.62 //3,62 рубля за кВт*ч
postUpdate(MercuryIncotex201KWh_Consumption_Price, Pri.doubleValue )
}
val CropDigital1 = String::format("%.2f", KWh.doubleValue ) //формат строки для sitemap
val CropDigital2 = String::format("%.2f", Dif.doubleValue ) //формат строки для sitemap
postUpdate(MercuryIncotex201KWh_general, CropDigital1 + " (" + CropDigital2 + " в тек.мес.)" )
end
Good afternoon. Please help me to redo this rule for my needs.
Which part of it are you having trouble with?
This is probably the most important part for you
someItem.minimumSince(now.toDateMidnight.withDayOfMonth(1)).state
where “someItem” is a meter reading in kWh
minimumSince reaches back in time in a persistence database, and gets the lowest value since a particular date/time.
Since we are dealing with a meter reading that only gets bigger and bigger over time, minimumSince will give us the “start” meter reading at the beginning of that time period.
It’s then easy to subtract “start meter” from “today meter” to calculate consumption from past date until today.
By extension, you can do minimumSince for two different dates to get two readings for calculation, e.g. 1st day of last month to 1st day of this month.
First thing you have to do is work out which of your Items represents the ongoing meter reading, if any. kWh is bit muddled in your first post.
Then make sure you are persisting that Item.
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.