Well, because the item has a status, and this status is most likely to be used in rules. You don’t want to parse strings everytime you want to compare or add something It’s easy to change display behavior and it’s easy to build a string from a number in a rule.
Take a look at this rule:
rule "PV-Anlage Update"
when
Item PV_Current received update
then
MyPV.postUpdate(PV_Current.state.toString+"W "+PV_DaySum.state.toString+"Wh/d "+PV_YearSum.state.toString+"Wh/y "+PV_Total.state.toString+"Wh/t")
end
Each time PV_Current
gets an update, MyPV
is updated, too, but with additional items. It looks like this:
In fact, as time goes by, I already have a more feature-complete version of the rule:
rule "PV-Anlage Update"
when
Item PV_Current received update
then
var String strCurr = "Err; "
var String strDay = "Err; "
var String strYear = "Err; "
var String strTotal = "Err; "
logDebug("PV-update", "Start der Rule")
if (PV_Current.state instanceof Number) {
if (PV_Current.state > 1000)
strCurr = String::format("%.2f kW; ", (PV_Current.state as Number) / 1000)
else
strCurr = PV_Current.state.format("%d W; ")
}
logDebug("PV-update", "strCurr = {}",strCurr)
if (PV_DaySum.state instanceof Number) {
if (PV_DaySum.state > 1000)
strDay = String::format("%.2f kWh/d; ", (PV_DaySum.state as Number) / 1000)
else
strDay = PV_DaySum.state.format("%d Wh/d; ")
}
logDebug("PV-update", "strDay = {}",strDay)
if (PV_YearSum.state instanceof Number) {
if (PV_YearSum.state > 1000000)
strYear = String::format("%.2f mWh/y; ", (PV_YearSum.state as Number) / 1000000)
else if (PV_YearSum.state > 1000)
strYear = String::format("%.2f kWh/y; ", (PV_YearSum.state as Number) / 1000)
else
strYear = PV_YearSum.state.format("%d Wh/y; ")
}
logDebug("PV-update", "strYear = {}",strYear)
if (PV_Total.state instanceof Number) {
if (PV_Total.state > 1000000)
strTotal = String::format("%.2f mWh/t", (PV_Total.state as Number) / 1000000)
else if (PV_Total.state > 1000)
strTotal = String::format("%.2f kWh/t", (PV_Total.state as Number) / 1000)
else
strTotal = PV_Total.state.format("%d Wh/t")
}
logDebug("PV-update", "strTotal = {}",strTotal)
MyPV.postUpdate(strCurr + strDay + strYear + strTotal)
end
And the output is like this:
And as you can see, I have an issue in this rule It should be MWh, not mWh but guess what: changed that already…