Needing help with formats in a rule

Tags: #<Tag:0x00007f173bd871c0>


i am using the bmw connected drive scipt to log the mileage of my bmw.

Now i would like to create a rule, which informs me to the actual status of my used and avaible kilometers.

Therefore i created the following variables and rule to get the informations in a telegram message.

My problem is, that i am not really familiar with formatting the values in the right way. it would be great, if you could help me to complete my rule.

Thanks in advance,

var G31_V_Beginn = 09.09.2019
var G31_V_Ende = 08.09.2022
var G31_KM_Start = 12345
var G31_KM_Ziel = 72345
var G31_KM_pa = 20000

// Berechnung zu KM-Verbrauch

rule "Berechnung verfügbare Kilometer"
    Time Time cron "0 0 0 1/1 * ? *" or
    Item Bmw_ForceUpdate changed to ON
    // Verbleibende Tage Vertragslaufzeit
    // = Vertragsende - heute
    Bmw__G31_Rest_Tage.sendCommand(G31_V_Ende - today)
    createTimer(now.plusSeconds(3)) [ sendTelegram("bot1","Noch verbleibende Zeit bis zum Vertragsende: "+Bmw__G31_Rest_Tage.state+" Tage")]

    // Verbleibende KM Vertragslaufzeit gesamt
    // = "Ziel_KM" - "Start_KM" - "aktuelle KM"
    Bmw_G31_Summe_Rest_KM.sendCommand(G31_KM_Ziel - G31_KM_Start - Bmw_mileage)
    createTimer(now.plusSeconds(3)) [ sendTelegram("bot1","Noch verbleibende Kilometer bis zum Vertragsende: "+Bmw_G31_Summe_Rest_KM.state+" km")]

    // Verbleibende KM täglicher Durchschnitt
    // = "Bmw_G31_Summe_Rest_KM" / "Bmw__G31_Rest_Tage"
    Bmw_G31_Taegl_Rest_KM.sendCommand( ((G31_KM_pa * 3) - Bmw_mileage) / Bmw__G31_Rest_Tage )
    createTimer(now.plusSeconds(3)) [ sendTelegram("bot1","Noch verbleibende Kilometer - tägl. Durchschnitt: "+Bmw_G31_Taegl_Rest_KM.state+" km")]

    // Abweichung aktueller KM-Verbrauch zu urspr. Plan
    // = ( ("Vertrags_KM" / "Vertagslaufzeit") * "Tage seit Laufzeitbeginn" ) - "aktuelle KM"
    Bmw_G31_Diff_Verbr_KM.sendCommand( (((G31_KM_pa * 3) / (G31_V_Ende - G31_V_Start )) * (heute - G31_V_Start)) - Bmw_mileage )
    createTimer(now.plusSeconds(3)) [ sendTelegram("bot1","Abweichung zum geplanten KM-Verbrauch: "+Bmw_G31_Diff_Verbr_KM.state+" km")]

Can you give us a clue what the “right” format would be?
might be useful for you

All of your timers will execute at the same time, by the by. createTimer() schedules a job for the future, it does not stop and wait.

Thanks for your reply.

I think especially the is a problem with the date formats and calculations.

I added the timer to make sure, the at first the values for the items are calculated and sent after that. (Hope it was the right way with the timer?)

For excample i get these warnings/errors in VSCode and the log after deploying the rule:

21:46:05.225 [WARN ] [del.core.internal.ModelRepositoryImpl] - Configuration model 'bmw.rules' has errors, therefore ignoring it: [1,26]: no viable alternative at input '2019'
[2,24]: no viable alternative at input '2022'
[11,5]: no viable alternative at input 'Time'

Oh, I see. Because it takes ti,me for sendCommand to work. But … you know what you just sent as a command, why not use iit directly?

var x = some calculation

Okay, I think we are not so much about format here yet, but basic Rules syntax.

var G31_V_Beginn = 09.09.2019

That’s not valid syntax. The rules parser does not know what sort of variable you want, you have not told it, so it has to guess. 09 … ah it is a Number … 09.09 … ahh a decimal Number … 09.09.2019 … oh, its not a valid number at all. Error.

Are you trying to use dates here? This might help

Thanks for that. I see, this is quite more complex than i thougt.
I am not really sure, which format i should use - and how in detail.

Can you suggest a format for the variables (maybe with an excample?)

I’ve little idea what you are trying to do.
It looks like comparing some kind of Item state with a fixed date/time. What kind of Item, is it a DateTime type?

Mayb this will help

1 Like

The only item i rule in these rules is the item of the actual mileage of my car, this is a number item.

The idea is, to calculate how many of the appointed kilometers are already “used” and how many are left and if i am in plan with the “consumption” of the kilometers.

So i would like to set fix values for:

  • beginning of the contract
  • end of the contract
  • appointed km / year
  • km at the beginning of the contract
  • appointed km at the end of the contract

With these fixed values i would like to calculate the consumption in comparision with the actual date (today) and the actual mileage (number item)

My thought was, that i simply can define the static values to variables, so that i can use them in the calculation.

If i am unterstanding right, your second link describes, how to write a time into and string item.
I think this i would have to do with the “today” value to use is in the rules?

But how to set up the varialbes?
Do i just have to change “09.09.2019” to " “2019-09-09’T’00:00:00.SSSZ” ?

This thread shows you how to create a dateTime object from a string

As per that thread, the easiest way to handle all this is, is to use time in milliseconds.
Get your contract-start date as a datetime, convert to epoch.
Get your contract-end date as a datetime, convert to epoch.
Get now, convert to epoch.

Now do whatever maths you like.
now - start = time so far
end - now = time to go
end - start = contract duration

(time so far / contract duration) * total km allowed = allowed to date

etc etc.