Mysensors sketch (i think this is the one i am using)
Ok, so that should be the sketch i am using,
so as for the rules :-
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
var long LastUpdate = 0 //// <- At the top of the file
//////////////////////////////////////////////////////////////////////////////////////////////////
rule “Energy consumption calculation”
////////////////////////////////////////////////////////////////////////////////////////////////
when
Item Node_garage1 received update
then
var long currentTime = now.millis
if (LastUpdate != 0) {
var long timeElapsed = currentTime - LastUpdate
if (timeElapsed > 0) {
var Number power = Node_garage1.state as DecimalType
var Number energyConsumption = (power * timeElapsed) / 3600000 / 1000 // kWh
postUpdate(CumulativeEnergyConsumption, CumulativeEnergyConsumption.state as DecimalType + energyConsumption) //increment
postUpdate(DailyEnergyConsumption, DailyEnergyConsumption.state as DecimalType + energyConsumption)
}
}
LastUpdate = currentTime
end
//////////////////////////////////////////////////////////////////////////////////////////////////
rule “Clear daily consumption”
//////////////////////////////////////////////////////////////////////////////////////////////////
when
Time cron “0 0 0 * * ?” // every day
then
postUpdate(DailyEnergyConsumption, 0)
end
//////////////////////////////////////////////////////////////////////////////////////////////////
rule “Energy in past hour”
when
Time cron “0 0 0/1 1/1 * ? *” // every hour
then
postUpdate(HoursEnergyConsumption, (CumulativeEnergyConsumption.state as DecimalType - CumulativeEnergyConsumption.historicState(now.minusHours(1)).state as DecimalType))
end
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Items
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// //////////////////-------------ENERGY AND POWER input----------\\\\\\\\\
Number Node_garage1 “Current Energy [%d W]” (Power_Chart) { mqtt="<[mysensor:MyMQTT/22/0/V_WATT:state:default]" }
//////////////------------VARIABLES_ENERGY-------------\\\\\\\\\\
Number CumulativeEnergyConsumption “total energy [%.2f kWh]” { mqtt=">[mysensor:MyMQTT/55/0/info:state:*:default]" }
Number DailyEnergyConsumption “todays energy [%.8f kWh]”
Number HoursEnergyConsumption “hour energy [%.8f kWh]” (Power_Chart2)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Sitemap (or just the relavant part of it.)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Frame {
Group item=Node_garage1 { //Current Energy/////
Text item=Node_garage1
Text item=DailyEnergyConsumption
Text item=CumulativeEnergyConsumption
Text item=HoursEnergyConsumption
Chart item=Power_Chart2 period=3D refresh=30000
Chart item=Power_Chart period=2h refresh=30000
Chart item=Power_Chart period=4h refresh=30000
Chart item=Power_Chart period=3D refresh=30000
}
}
Hope this makes sense.