Im trying to build a rule to safe the values and states of multiple items into one “summary-item”. Later I want to display this item on my sitemap.
E.g.:
the OpenClosed state of a gasstation + the gasprice of that exact station
This is what i got so far:
rule "Tankstelle 1 Summary update"
when
System started or
Item Station_Open_1 changed or
Item E5_1 changed
then
logInfo("Tankstelle 1 Summary", "--> update")
if (Station_Open_1.state == UNDEF || E5_1.state == NULL) { return }
if (Station_Open_1.state == "OPEN") {
val String state = (Station_Open_1.state as OpenClosedType).toString
val String price = (E5_1.state as DecimalType).toString
var String summary = "ist" state + " - Super: " + price
//summary = String::format("%s - %s", state, price)
Station_Summary_1.postUpdate(summary)
}
else {
Station_Summary_1.postUpdate("geschlossen")
}
end
Im getting this Error:
2019-12-03 18:53:29.699 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'tankstellen.rules', using it anyway:
This expression is not allowed in this context, since it doesn't cause any side effects.
2019-12-03 18:53:29.761 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'tankstellen.rules'
2019-12-03 18:53:35.161 [INFO ] [me.model.script.Tankstelle 1 Summary] - --> update
The “summary-item” state is “geschlossen” all the time.
Station_Open_1 and E5_1 can both be UNDEF or NULL. You need to check both Items for both states.
You need to have a semicolon after the return command: return; This is probably the source of the warning in the log about the statement not being allowed.
Station_Open_1.state is a State type and you are trying to compare it to a String type. if(Station_Open_1.state.toString == "OPEN"). This is probably why the if statement never runs.
val String state... is needlessly complex. val state = Station_Open_1.state.toString
Same for price and summary.
Over all, the whole Rule can be made significantly shorter and simpler.
then
logInfo("Tankstelle 1 Summary", "--> update")
if(Station_Open_1.state == UNDEF || Station_Open_1.state == NULL ||
E5_1.state == UNDEF || E5_1.state == NULL)
return;
val state = Station_Open_1.state.toString
val price = E5_1.state.toString
val summary = if(state == "OPEN") "ist " + state + " - Super: " + price else "geschlossen"
Station_Summary_1.postUpdate(summary)
end
@rlkoshak Okay, i think Im more confused then before, after trying to understand where to put the mapping.
I guess i have to use something like this:
var summary= transform("MAP", "de.map", "OPEN")
If so, where do i put it?
Sorry, but Im totally new to all this and i dont find any guides to make my start easier. The documentaion is really good, but requires basekonowledge Im missing atm.
Im trying to use the rule for a speedtest summary.
rule "Speedtest Summary update"
when
System started or
Item Speedtest_LUD changed from NULL
then
logInfo("Speedtest Summary", "--> update")
if (Speedtest_ResultDown.state == UNDEF || Speedtest_ResultDown.state == NULL ||
Speedtest_ResultUp.state == UNDEF || Speedtest_ResultUp.state == NULL)
return;
val downlink = Speedtest_ResultDown.state.toString
val uplink = Speedtest_ResultUp.state.toString
val time = Speedtest_LUD.state.toString
val summary = "Down ▼ " downlink + " - Up ▲ " + uplink + " um " + time + " Uhr"
Speedtest_Summary.postUpdate(summary)
end
But:
2019-12-14 21:14:24.053 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'SpeedSummary.rules', using it anyway:
This expression is not allowed in this context, since it doesn't cause any side effects.
The summaryItem is only showing “Down ▼”
If i try it like this
val downlink = Speedtest_ResultDown.state as DecimalType.toString
i get the following error:
2019-12-14 21:13:42.163 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'Speedtest Summary update': Could not cast 100.0633163552 Mbit/s to void; line 14, column 20, length 49
[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Test Würfel': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.BooleanExtensions.operator_or(boolean,boolean) on instance: null
If only we knew what your rule code was, we might be able to comment on the error. You must realise that?
Switch-case is a special kind of extended “if”, you set up a switch value and have multiple case statements to do things dependent on the value. Search this forum for examples.
You could save some typing and repeated fetching of the event -
val xx = receivedEvent.getEvent()
if (xx == "1000" || xx == "2000" ...
You could save processing using if-else structure.
When an if() section is satisfied, you do not want to carry on working through all the others that you now know will not match.
Put the likeliest cases at the top for maximum efficiency.