Calculation in a rule (numbers and dates)

Hello,

i would like to do two “simple” calculations in a rule an would be glad, if someone could help me at this point.
I have already read the topic of comparing and converting dates and times, but the relationships are quite complex.

It would be great, if anyone could help me to finish the rule.

Thanks in advance,
Alex

rule "calculation"
when
    Time cron .....
then
    var Number Kilometer = 123
    var ??? date = 31.12.2022
    item1.sendCommand(   (date - today) * Kilometer   )
    item2.sendCommand(   ((date - today) * Kilometer ) + 2500   )
end

this might lead you to a solution:

in a nutshell, you need to translate the var date to zonedDateTime. Today you get with „now“. Also consider to change sendCommand to postUpdate and make sure you send the string to the item .toString . unless you really want to action something with this item.

1 Like

Hello and thanks for your help!

Now i updated my rule to this, but actually it doesnt work:

rule "BMW G31 Km-Sollwerte errechnen"
when
    Time cron "0 0 0 ? * * *" or
    Item BMW_G31_Trigger_KM_Calc changed to ON
then
    import java.time.temporal.ChronoUnit
    val var_bmw_g31_vertragsende = "2022-09-08T00:00:00.000+0200"
    val var_bmw_g31_restlaufzeit = now.until(var_bmw_g31_vertragsende, ChronoUnit.DAYS)
    val var_bmw_g31_km_tag = 55
    val var_bmw_g31_km_soll = var_bmw_g31_restlaufzeit * var_bmw_g31_km_tag
    val var_bmw_g31_km_soll_tol = (var_bmw_g31_restlaufzeit * var_bmw_g31_km_tag) + 2500  
    bmw_g31_km_soll.postUpdate(var_bmw_g31_km_soll)
    bmw_g31_km_soll_tol.postUpdate(var_bmw_g31_km_soll)
end

I get the following error message:

20:16:56.932 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID 'bmw-2' failed: The name 'import' cannot be resolved to an item or type; line 29, column 5, length 6 in bmw

You have to do imports at the head of your xxx.rules file, outside of any rule.

1 Like

Thank you! I also tried it out, but then i get:

20:38:53.607 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID 'bmw-2' failed: An error occurred during the script execution: Could not invoke method: java.time.ZonedDateTime.until(java.time.temporal.Temporal,java.time.temporal.TemporalUnit) on instance: 2021-04-04T20:38:53.605350+02:00[Europe/Berlin] in bmw

I found a solution now, thanks again for your help! There was an issue with the conversion of the dates.

Here is the “final” code, if someone is interested in:

rule "BMW G31 Km-Sollwerte errechnen"
when
    Time cron "0 0 0 ? * * *" or
    Item BMW_G31_Trigger_KM_Calc changed to ON
then
    val String var_bmw_g31_vertragsstart_string = "2019-09-09T00:00:00.000+02:00"
    val var_bmw_g31_start_km = 21126
    val var_bmw_g31_toleranz = 2500
	val DateTime var_bmw_g31_vertragsstart = parse(var_bmw_g31_vertragsstart_string)
    val var_bmw_g31_restlaufzeit = now.until(var_bmw_g31_vertragsstart, ChronoUnit.DAYS)
    val var_bmw_g31_km_tag = 55
    val var_bmw_g31_km_soll = (var_bmw_g31_restlaufzeit * var_bmw_g31_km_tag * -1) + var_bmw_g31_start_km
    val var_bmw_g31_km_soll_tol = ((var_bmw_g31_restlaufzeit * var_bmw_g31_km_tag) * -1) + var_bmw_g31_toleranz + var_bmw_g31_start_km
    bmw_g31_km_soll.postUpdate(var_bmw_g31_km_soll)
    bmw_g31_km_soll_tol.postUpdate(var_bmw_g31_km_soll_tol)
    BMW_G31_Trigger_KM_Calc.postUpdate(OFF)
end

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