Blockly Script via Rule different wrong result > manual run is correct

  • Platform information:
    • Hardware: Raspi Pi4
    • OS: Raspian Debian Bullseye / Openhabian
    • openHAB version: openHAB 3.4.1
  • Issue of the topic: Blockly Script via Rule different wrong result > manual run is correct

Hi,
I have setup a Rule to run a Blockly script. The rule does execute when the car door status change from Open to Close (before when Engine change from ON to OFF).

The script is running some calculations and I have the impression that the script is only partially executing the steps when it is run as rule (no matter if automated or if manually started as rule).
When I go into the rule editing function and select the Script and click run inside the scripts then the calculations will be correct. I have absolutely no idea what the difference might be causing that issue?
I also have implemented a short wait between each calculation but can not find a solution for it.

I calculate estimation of charging time:

1 of 2 calculations wrong, majority of time both are wrong and not calculated till the end of the script:

I tried to play around finding ways to have correct values when the rule automatically runs. But I had not managed yet.

many thanks for sharing your ideas and maybe the solution with me. I am stuck right now :(.

Many thanks
David

This is the blockly script:

Just 1h ago: wrong automatic result

Going into the rule, open the script and click “Run Now” and see it “Running” > correct result provided:

I have no idea why it does happen like this :(.

None of your screen shots are coming through.

Please click on the code tab of the rule and paste the text you find there. Use code fences.

```
code goes here
```

To show what the rule is doing, please paste the text from relevant logs in openhab.log and events.log.

Hi,

I am absolute beginner and just happy after 2 weeks that I had managed to get the Raspi and Openhab running.
I dont know yet how to get logs relevant to the rule from the ssh connection? Beginner for Linux :=)

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: MG5_Driver_Door
      state: CLOSED
      previousState: OPEN
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      blockSource: <xml
        xmlns="https://developers.google.com/blockly/xml"><variables><variable
        id="CBneheWMeh^*-MfeH(!P">Ladestand</variable><variable
        id="^seWl{q2}HjITytoA~[.">100P</variable><variable
        id="X({J)ISXEbcmCaql[!He">tmp</variable><variable
        id="%Q#Mo4w(+8Jq#iW*EdPD">80P</variable><variable
        id="MFhvzW_@6@EwRil?[`g2">tmp2</variable></variables><block
        type="variables_set" id="_Of1[I1v{eZA3nAQlMxe" x="101" y="135"><field
        name="VAR" id="CBneheWMeh^*-MfeH(!P">Ladestand</field><value
        name="VALUE"><block type="oh_getitem_state"
        id="HDCdP7f`C3I_hvr2jA|L"><value name="itemName"><shadow type="oh_item"
        id="5;E@;RO2lrD;.RGg**Oe"><field
        name="itemName">MG5_Battery_Charge_Level</field></shadow></value></block></value><next><block
        type="math_change" id="IoJatgkx?;v5ROSDdqK#"><field name="VAR"
        id="CBneheWMeh^*-MfeH(!P">Ladestand</field><value name="DELTA"><shadow
        type="math_number" id="cw?OMSDLZ.kCNT@lu}G%"><field
        name="NUM">1</field></shadow><block type="math_arithmetic"
        id="Mh_?yS0Ve$V7WKm1J0Zn"><field name="OP">DIVIDE</field><value
        name="A"><shadow type="math_number" id="cn77%;9NdQVBs7gWhz)y"><field
        name="NUM">1</field></shadow><block type="variables_get"
        id="@c|mica;b3~./%O4V4[@"><field name="VAR"
        id="CBneheWMeh^*-MfeH(!P">Ladestand</field></block></value><value
        name="B"><shadow type="math_number" id="e`g.{32BjiPjr,m8TJtX"><field
        name="NUM">100</field></shadow></value></block></value><next><block
        type="variables_set" id="je6Bor9F@?9ZNsgRFh:,"><field name="VAR"
        id="^seWl{q2}HjITytoA~[.">100P</field><value name="VALUE"><block
        type="math_arithmetic" id="0ljQhXS6-D8TQxvxLRf*"><field
        name="OP">MINUS</field><value name="A"><shadow type="math_number"
        id="2frh`Uk*6u|yvUO(s+8U"><field
        name="NUM">61</field></shadow></value><value name="B"><shadow
        type="math_number" id="shdtHJYfoLtU3c|@7?vN"><field
        name="NUM">1</field></shadow><block type="math_arithmetic"
        id="~L`,v?2o!X,r*xxhM61/" inline="false"><field
        name="OP">MULTIPLY</field><value name="A"><shadow type="math_number"
        id="G|oAXYfaHXO6+bxHe}vX"><field
        name="NUM">61</field></shadow></value><value name="B"><shadow
        type="math_number" id="6{-(A`I2*yDRDaG!~)wZ"><field
        name="NUM">1</field></shadow><block type="variables_get"
        id="mKq|yFjH#Z:!k|xwnmt7"><field name="VAR"
        id="CBneheWMeh^*-MfeH(!P">Ladestand</field></block></value></block></value></block></value><next><block
        type="oh_sleep" id="v765qn8VY9blOZ$m5tV|"><field
        name="milliseconds">1000</field><next><block type="math_change"
        id="5M5ke@K)OcWK3:u2X%yb"><field name="VAR"
        id="X({J)ISXEbcmCaql[!He">tmp</field><value name="DELTA"><shadow
        type="math_number" id=",T:#+-u3z(5DiR*)OHTD"><field
        name="NUM">1</field></shadow><block type="math_arithmetic"
        id="o~,R(-$,OhRtLt1em]^."><field name="OP">DIVIDE</field><value
        name="A"><shadow type="math_number" id="0_@x:I(/PHE0Tn-TvFW6"><field
        name="NUM">1</field></shadow><block type="variables_get"
        id="4^#-=sqTE0;CeW5W|mOy"><field name="VAR"
        id="^seWl{q2}HjITytoA~[.">100P</field></block></value><value
        name="B"><shadow type="math_number" id="~(X*5;i9:?wtWxz[PfoR"><field
        name="NUM">9.65</field></shadow></value></block></value><next><block
        type="oh_sleep" id="~8J+SVK!Qt^1XpLrcKDl"><field
        name="milliseconds">500</field><next><block type="math_change"
        id="xn5kOYPm]s3`3FoP%MVE"><field name="VAR"
        id="X({J)ISXEbcmCaql[!He">tmp</field><value name="DELTA"><shadow
        type="math_number"><field name="NUM">1</field></shadow><block
        type="math_arithmetic" id="N}!.qehP|21bzXC@L]]E"><field
        name="OP">DIVIDE</field><value name="A"><shadow
        type="math_number"><field name="NUM">1</field></shadow><block
        type="variables_get" id="jz8)Y$mg^FR,~M__0^=%"><field name="VAR"
        id="X({J)ISXEbcmCaql[!He">tmp</field></block></value><value
        name="B"><shadow type="math_number" id="NAWE)!O/0Z[K:no)%~J?"><field
        name="NUM">0.02</field></shadow></value></block></value><next><block
        type="oh_sleep" id="9E:-*.uTGp1re2gVeI^]"><field
        name="milliseconds">500</field><next><block type="math_change"
        id="a]jd[^kcA#,6}xGf/;RU"><field name="VAR"
        id="X({J)ISXEbcmCaql[!He">tmp</field><value name="DELTA"><shadow
        type="math_number"><field name="NUM">1</field></shadow><block
        type="math_arithmetic" id="4qb1AOfe9V,:~NN0~NZH"><field
        name="OP">DIVIDE</field><value name="A"><shadow
        type="math_number"><field name="NUM">1</field></shadow><block
        type="variables_get" id="T]|On(?^%=fdPgk^d5#="><field name="VAR"
        id="X({J)ISXEbcmCaql[!He">tmp</field></block></value><value
        name="B"><shadow type="math_number" id="n!~M^Fe~n}jMrjLrqE3n"><field
        name="NUM">60</field></shadow></value></block></value><next><block
        type="oh_sleep" id="4;$tTR3~Y!t75;lB{9U7"><field
        name="milliseconds">500</field><next><block type="variables_set"
        id="pJfcC5j6EXX#F{y{rZo8"><field name="VAR"
        id="X({J)ISXEbcmCaql[!He">tmp</field><value name="VALUE"><block
        type="math_round" id="4%bSgkbi/App*Dzj$Z-S"><field
        name="OP">ROUND</field><value name="NUM"><shadow type="math_number"
        id="*o^![j|FbEEeqsvjtN?a"><field name="NUM">3.1</field></shadow><block
        type="variables_get" id="o(kHJ*aLr`L4Q~)o+R83"><field name="VAR"
        id="X({J)ISXEbcmCaql[!He">tmp</field></block></value></block></value><next><block
        type="oh_sleep" id="Xjk(t9h0GTE}=)A_]8!B"><field
        name="milliseconds">500</field><next><block type="oh_event"
        id="3BcWsb#~=6%kF,gkEBmz"><field
        name="eventType">postUpdate</field><value name="value"><shadow
        type="text" id="Ph]JeIsq[l0VO-O!)DZ."><field
        name="TEXT">value</field></shadow><block type="variables_get"
        id="YW(=G4PiEj@2beLu$lU-"><field name="VAR"
        id="X({J)ISXEbcmCaql[!He">tmp</field></block></value><value
        name="itemName"><shadow type="oh_item" id=".B^U|=4{Xq:^dh_F;t9L"><field
        name="itemName">ChargeTo100</field></shadow></value><next><block
        type="controls_if" id="Qd~TVS/^jaiAUYETbyR7"><mutation
        else="1"></mutation><value name="IF0"><block type="logic_compare"
        id="3CCi!%+boEGb?yq2-GxI"><field name="OP">LT</field><value
        name="A"><block type="variables_get" id="FBK.UHLj.PImf$jw%J:d"><field
        name="VAR"
        id="CBneheWMeh^*-MfeH(!P">Ladestand</field></block></value><value
        name="B"><block type="text" id="OZ?slDGkF1h$1*+%v5G]"><field
        name="TEXT">0.8</field></block></value></block></value><statement
        name="DO0"><block type="variables_set" id="Y7bf/P]-Tuk#cfYn0y0*"><field
        name="VAR" id="%Q#Mo4w(+8Jq#iW*EdPD">80P</field><value
        name="VALUE"><block type="math_arithmetic"
        id="S2gJVf)lwB^2~YEBxsZ2"><field name="OP">MINUS</field><value
        name="A"><shadow type="math_number" id="Ne@0Le4=,],g6gte^NRs"><field
        name="NUM">61</field></shadow><block type="math_arithmetic"
        id="$-jAT}@oca;T/=+2(gDX"><field name="OP">MULTIPLY</field><value
        name="A"><shadow type="math_number" id="x!AtzJr%dVGQX]tfMu?L"><field
        name="NUM">61</field></shadow></value><value name="B"><shadow
        type="math_number" id="4V^/y#FGD@ecjv7uZHl;"><field
        name="NUM">0.8</field></shadow></value></block></value><value
        name="B"><shadow type="math_number"><field
        name="NUM">1</field></shadow><block type="math_arithmetic"
        id="87u*O+Nb+;`f#?oK7WWL" inline="false"><field
        name="OP">MULTIPLY</field><value name="A"><shadow type="math_number"
        id="_0YHgX.xr*Y,,9E_c.%h"><field
        name="NUM">61</field></shadow></value><value name="B"><shadow
        type="math_number"><field name="NUM">1</field></shadow><block
        type="variables_get" id="t.z~08@/a@XU48U9Vf{m"><field name="VAR"
        id="CBneheWMeh^*-MfeH(!P">Ladestand</field></block></value></block></value></block></value><next><block
        type="oh_sleep" id="WR([^T|`3DL8x~o69eY2"><field
        name="milliseconds">500</field><next><block type="math_change"
        id="(v6g`^`.d~o)rE4:Li+="><field name="VAR"
        id="MFhvzW_@6@EwRil?[`g2">tmp2</field><value name="DELTA"><shadow
        type="math_number"><field name="NUM">1</field></shadow><block
        type="math_arithmetic" id="-UeDffb*Vb$EXaS@Kx,e"><field
        name="OP">DIVIDE</field><value name="A"><shadow
        type="math_number"><field name="NUM">1</field></shadow><block
        type="variables_get" id="`oj$DgP`qc[)Swy[-%6)"><field name="VAR"
        id="%Q#Mo4w(+8Jq#iW*EdPD">80P</field></block></value><value
        name="B"><shadow type="math_number" id="iA6`dK22NEM|7gGo*Ju0"><field
        name="NUM">9.65</field></shadow></value></block></value><next><block
        type="oh_sleep" id="byUkm?[*k~P8r7^bmn9h"><field
        name="milliseconds">500</field><next><block type="math_change"
        id="AoV~3eyhwx]`JZxL/~x~"><field name="VAR"
        id="MFhvzW_@6@EwRil?[`g2">tmp2</field><value name="DELTA"><shadow
        type="math_number"><field name="NUM">1</field></shadow><block
        type="math_arithmetic" id="f8n5RM{rDK{]pF(vIGyo"><field
        name="OP">DIVIDE</field><value name="A"><shadow
        type="math_number"><field name="NUM">1</field></shadow><block
        type="variables_get" id="YDQt8c!md/}MS7Pro(tU"><field name="VAR"
        id="MFhvzW_@6@EwRil?[`g2">tmp2</field></block></value><value
        name="B"><shadow type="math_number" id="dJ#m[p-#sA?1Y{}9j*;I"><field
        name="NUM">0.02</field></shadow></value></block></value><next><block
        type="oh_sleep" id="gODN^n/9PBk:Q1X7an~J"><field
        name="milliseconds">500</field><next><block type="math_change"
        id="1{)],8s=t0;d{VATsyH/"><field name="VAR"
        id="MFhvzW_@6@EwRil?[`g2">tmp2</field><value name="DELTA"><shadow
        type="math_number"><field name="NUM">1</field></shadow><block
        type="math_arithmetic" id="Xm$l+FBQ!$Dx%$3)tA)n"><field
        name="OP">DIVIDE</field><value name="A"><shadow
        type="math_number"><field name="NUM">1</field></shadow><block
        type="variables_get" id="nYpDIF)CLjbZLa2{S)B)"><field name="VAR"
        id="MFhvzW_@6@EwRil?[`g2">tmp2</field></block></value><value
        name="B"><shadow type="math_number" id="SY{t!hg+$47KLUnprAXB"><field
        name="NUM">60</field></shadow></value></block></value><next><block
        type="oh_sleep" id="mfPEBBjcjO9saMUXw_tT"><field
        name="milliseconds">500</field><next><block type="variables_set"
        id="{.G7mlREgE^^Gol18er-"><field name="VAR"
        id="MFhvzW_@6@EwRil?[`g2">tmp2</field><value name="VALUE"><block
        type="math_round" id="hUEQ@P##WvaoyYW5QQ+Q"><field
        name="OP">ROUND</field><value name="NUM"><shadow
        type="math_number"><field name="NUM">3.1</field></shadow><block
        type="variables_get" id="VW$=Ohy9Gm9}}E58$9fs"><field name="VAR"
        id="MFhvzW_@6@EwRil?[`g2">tmp2</field></block></value></block></value><next><block
        type="oh_sleep" id="]eALC/BY~[?4l5?,+eGo"><field
        name="milliseconds">500</field><next><block type="oh_event"
        id="8ls_m,USj$c3/JG@quys"><field
        name="eventType">postUpdate</field><value name="value"><shadow
        type="text"><field name="TEXT">value</field></shadow><block
        type="variables_get" id="j^lo+[$y0,J*XTC;:qy+"><field name="VAR"
        id="MFhvzW_@6@EwRil?[`g2">tmp2</field></block></value><value
        name="itemName"><shadow type="oh_item" id="oIa-#..eGKiX_2k.n8Xj"><field
        name="itemName">ChargeTo80</field></shadow></value></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></statement><statement
        name="ELSE"><block type="oh_event" id="rq1iHg%DDCm8GuTJem@H"><field
        name="eventType">postUpdate</field><value name="value"><shadow
        type="text"><field name="TEXT">value</field></shadow><block type="text"
        id="xiE50HX=K$DZS|a?guC3"><field name="TEXT">&gt;= 80%
        Ladestand</field></block></value><value name="itemName"><shadow
        type="oh_item" id="MUh=iG}}v5rT}jYkDH!t"><field
        name="itemName">ChargeTo80</field></shadow></value></block></statement></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></xml>
      type: application/javascript
      script: >
        var Ladestand, my_100P, tmp, my_80P, tmp2;


        var thread = Java.type('java.lang.Thread')



        Ladestand = itemRegistry.getItem('MG5_Battery_Charge_Level').getState();

        Ladestand = (typeof Ladestand == 'number' ? Ladestand : 0) + Ladestand / 100;

        my_100P = 61 - 61 * Ladestand;

        thread.sleep(1000);

        tmp = (typeof tmp == 'number' ? tmp : 0) + my_100P / 9.65;

        thread.sleep(500);

        tmp = (typeof tmp == 'number' ? tmp : 0) + tmp / 0.02;

        thread.sleep(500);

        tmp = (typeof tmp == 'number' ? tmp : 0) + tmp / 60;

        thread.sleep(500);

        tmp = Math.round(tmp);

        thread.sleep(500);

        events.postUpdate('ChargeTo100', tmp);

        if (Ladestand < '0.8') {
          my_80P = 61 * 0.8 - 61 * Ladestand;
          thread.sleep(500);
          tmp2 = (typeof tmp2 == 'number' ? tmp2 : 0) + my_80P / 9.65;
          thread.sleep(500);
          tmp2 = (typeof tmp2 == 'number' ? tmp2 : 0) + tmp2 / 0.02;
          thread.sleep(500);
          tmp2 = (typeof tmp2 == 'number' ? tmp2 : 0) + tmp2 / 60;
          thread.sleep(500);
          tmp2 = Math.round(tmp2);
          thread.sleep(500);
          events.postUpdate('ChargeTo80', tmp2);
        } else {
          events.postUpdate('ChargeTo80', '>= 80% Ladestand');
        }
    type: script.ScriptAction

If you installed openHABian, look in Frontail (hosted on port 9000). Or look in /var/logs/openhab on the machine.

Why all the sleeps? There is absolutely no reason to have all these sleeps. And in fact, there’s probably no reason to have any sleeps in a rule like this.

Everything is hard coded in this rule except for Ladestand which is the state of an Item. So what’s the state of that Item when it works and what’s the state when it doesn’t work?

The sleeps are implemented to check if this might fix the issue.
The variable Ladestand is the percentage divided by 100.

e.g. 49,5% / 100 = 0.495 is the variable

The wired thing happening is that if the rule is running then some wrong numbers come up.
If i go into editing of the rule, go into the script and run it manually “Run Now” and then I see the correct results.

Even that the script is the action inside the rule it must have a difference to be executed by the rule or manually at the script directly.

Once I am home I check if I can find something at logs with your hints given.

They wont.

The only variable number in the calculations is the state of the Ladestand Item. So that’s what you need to focus on. What is it when the rule works? What is it when the rule doesn’t work?

Everything else in this rule is either a constant or based on a calculation.

The only difference is whether or not the event Object exists. You are not using the event Object so there is nothing different between triggering this rule. The only possible difference is the state of that one Item.

There are logging blocks. Log out the Ladestand variable. Look at events.log for the value of MG5_Battery_Charge_Level.

The getItem(‘MG5_Battery_Charge_Level’) for the Ladestand is the same paramater.
If the rule is run now and then 10 seconds later the script inside the rule run manually then the parameter is the same. They would not have changed during that period of time.

This is what you assume. When code doesn’t work it’s because you’ve made some sort of assumption that is false, meaning you must test and verify all your assumptions.

In short, I don’t believe it until you prove it. Looking at the code, the only way it can generate a different answer from one run to the next is if the state of MG5_Battery_Charge_Level is different between those runs.

here is the rule - it is probably blocked on Rich’s side as it is hosted on website named IBB

Can you add some log-blocks and check the values in the log output?

done! i did that before but didnt help me as beginner :P. Later after work or tomorrow I will check if I manage to find the log entries.