Blockly: decreasing a 'get state of item'-value

Dear community,

I’m new to OH3 and Blockly. In order to to control a heating in the bathroom I did create 2 rules (#1 to turn it off, #2 to turn it on).
Rule #1 (this one works) checks if the ‘actual bathroom temperature’ ≥ ‘selected bathroom temperature’ and turns OFF the heating:

configuration: {}
triggers:
  - id: "1"
    configuration:
      cronExpression: 0 0/10 5-21 * * ? *
    type: timer.GenericCronTrigger
conditions:
  - inputs: {}
    id: "4"
    configuration:
      blockSource: <xml xmlns="https://developers.google.com/blockly/xml"><block
        type="logic_compare" id=":RCrAD$.OcV{g7wvmXn#" x="106" y="100"><field
        name="OP">GTE</field><value name="A"><block type="oh_getitem_state"
        id="APV^a2GOj4ph_~TpiN,`"><value name="itemName"><shadow type="oh_item"
        id="tn{^zO)7#lJuBYZwon*M"><field
        name="itemName">BadTLSensor_ActualTemperature</field></shadow></value></block></value><value
        name="B"><block type="oh_getitem_state" id="uH5K!HDe/K=UJq(CB:Z6"><value
        name="itemName"><shadow type="oh_item" id="4yBJGB8hv*H,n)Si0w)0"><field
        name="itemName">BadTLSensor_SetPointTemperature</field></shadow></value></block></value></block></xml>
      type: application/javascript
      script: >
        itemRegistry.getItem('BadTLSensor_ActualTemperature').getState() >=
        itemRegistry.getItem('BadTLSensor_SetPointTemperature').getState();
    type: script.ScriptCondition
actions:
  - inputs: {}
    id: "2"
    configuration:
      command: OFF
      itemName: IRHeizungBad_PowerState
    type: core.ItemCommandAction

With rule #2 I want to turn the heating on if ‘actual bathroom temperature’ ≤ ‘selected bathroom temperature’-2°C. (if I use ‘19’ as a fixed number it works).

Unfortunately I’m not able to create a condition (blockly script) to do it with a value based on the ‘selected bathroom temp’. This is how I would do it (but obviously it doesn’t work like that :smiley:)

Can you please enlighten me?

Think about the order of operations. They happen from the inside outward. You want to do the comparison last so it’s the outermost block. The comparison has two operands. You can plug a block in for each operand. You want the subtraction to happen before the comparison so the subtract block needs to be put into one of the operands slots for the comparison. The subtraction likewise has two operands. So you’ll plug the get item state into one and 2 into the other.

In short, it’ll all be one long line.

1 Like

Isn’t my last picture almost correct then? Unfortunately I can’t get the ‘BadTLSensor_SetPointTemperature’-block into the subtraction-block nor the subtraction-block into the comparison-block.

Try to store the state of your item into a variable first and then do the math with the variable

The item state is a string but math functions requires a number, therefore type conversion is not always working ootb and throw you cannot combine all blocks

2 Likes

Looks like I’m way to stupid to solve this problem.

This solution doesn’t work as well. What am I doing wrong?

Store both item values into a variable and not only one.

Alternatively run this as a script and add some logging of the values

1 Like

Still not working. :frowning:

Ah, I have missed in the first screenshot that you are using the wrong blocks.

Do not mix up “variables” (block: set … to …) and the “value storage” blocks (block: store value … into …).
The value storage is to store information across multiple runs of the same script, while the variable blocks defining a simple variable for one script execution only.

Again, please rebuild this as a script and add logging if changing the store value blocks to variable blocks will not solve your issue

1 Like

You’re da man! Dankeschön!

This one actually works. What is the advantage of using a separate script iso using the integrated script within the rule?
Thanks again!

If you have the script as condition, you always need true/false output.
If you put the logic into a separate script, you can run it standalone for testing / debugging purpose.

Also if you want to reuse your work, you can call a script from multiple other rules and pass some parameters to the script

1 Like

You can do that with regular rules too. In fact, a Script is a regular rule, it just has a special UI to create a rule with just one script action and adds the “Script” tag.

Yes, but in the original post the script was not part of a rule action, but only part of a rule condition. Therefore I recommended for testing & debugging to run the code standalone as script.