Some issues with script

Latest OH4.3.0 on rpi5
Cant understand where is problem with such script, just made a test script to understand how things works compared with old OH

var outlet, inlet, delta;


outlet = items.getItem('Real_outlet').state;
console.info(items.getItem(outlet));
inlet = items.getItem('Real_inlet').state;
delta = outlet - inlet;
if (outlet > inlet) {
  items.getItem('Real_cop').postUpdate(delta);
}

Get error:

 [ERROR] [.handler.AbstractScriptModuleHandler] - Script execution of rule with UID '222647de12' failed: org.openhab.core.items.ItemNotFoundException: Item '23.56 °C' could not be found in the item registry


This is your clue. Your script is looking for an item with the name 23.56 °C which is a state instead of a name. So, where in the blockly script do you have an item state block where there should just be an item block instead?
image
You set the variable outlet to the state of the of the item Real_outlet and then in the next block you try to get the item with that state variable.

You just want:

image

instead.

Also a little further down, you are going to run into problems. The fact that your outlet state is 23.56 °C indicates that this is a quantity state (that is, it has a unit) and not just a number. I assume the same is true of your inlet state as well. This means that your calculation of the delta variable will not be successful because that basic math function is for standard numbers only (your if statement will also display odd behavior). In the OH section of blockly there’s a whole set of blocks for working with states that have units:
image
You’ll want to use the math block from there for your delta, and the comparison block from there for your if statement test.

I would prefer to use ECMAScript 262 Edition 11 instead of blockly (I know that blockly script section generates same ECMAScript style code) ant question how to do math in proper way when you have different units in it ? I have old OH2.5 rules which want to re-work in OH4. So in OH2.5 for me looked less complicated :slight_smile:
simple OH 2.5 rule for re-work:

rule "COP Calulation"
when
	System started
	or
     Item panasonic_heat_pump_PumpFlow changed
     or
     Item Real_outlet changed
     or 
     Item Real_inlet changed
     or
     Item panasonic_w changed
        
	then 
     var Outlet = Real_outlet.state as Number
     var Inlet = Real_inlet.state as Number
     var Number flow = panasonic_heat_pump_PumpFlow.state as DecimalType
     var power = panasonic_w.state as Number
         
    
    if (Outlet > Inlet) {
    var Number deltaT = Outlet - Inlet
    var Number heatProd = ((flow / 60) * 4181) * deltaT
    var Number realCop = heatProd / power
          postUpdate(Real_cop,realCop)
    }
     else {
     postUpdate(Real_cop,0) 
     }
     
   
	
end

converting that to blockly (just for power used number as still dont have real value), but calculation looks bad…

If you are the type that prefers to learn from documentation then the docs for the js helper library that make the jsscripting rules user-friendly are here:

https://openhab.github.io/openhab-js/index.html

If you prefer to learn from direct examples, then blockly is a great way to go. You are right, blockly generates up-to-date jsscripting code that you can copy and paste into your own script file and modify if you prefer.

OH quantities are unit aware. So, if you are using the blocks I indicated above, then the unit will be automatically taken into account. This will output the comparison between two temperature values regardless of the temperature units used:

And if you’d rather use text then just check the text this produces:

var downTemp, upTemp;
downTemp = items.getItem('Thermostat_Basement_Temperature').quantityState;
upTemp = items.getItem('Thermostat_Upstairs_Temperature').quantityState;
console.info((Quantity(upTemp).greaterThanOrEqual(Quantity(downTemp))));

You can streamline this a little because the variables are already quantities so the extra conversion that blockly does just to be flexible is not required:

var downTemp, upTemp;
downTemp = items.getItem('Thermostat_Basement_Temperature').quantityState;
upTemp = items.getItem('Thermostat_Upstairs_Temperature').quantityState;
console.info(upTemp.greaterThanOrEqual(downTemp));

updated previous topic its funny when it calculates n times higher then comparing with manual calculation :slight_smile:

flow = 8.72 l/min
delta = 2.18 °C
heatProd = 167301.1540933333333333333333333334 l·K/min (manual calculation = 1 324,65 ) 
realCOP = 418.25288523333337500000000000000008365057704666667500000 l·K/min

You are doing your manual calculation with °C, but the quantity is properly converting your °C → °K to give you lK/min:

(8.72 / 60 * 4181 ) * (2.18 + 273.15) = 167301.15409

If you don’t want that unit conversion, then instead of using quantity states, get the numeric state of each item and do the math with the basic numeric blocks.
image

thanks for help, that IK/min looked strange for me as well :slight_smile: Now all works