Send Command with one Switch Rule

Hello!
I want to pass a value to a dimmer with a rule written with Blockly. The trigger is a switch, if it gets the value “ON” it should dim the dimmer to 90%, if I turn the switch off(“OFF”) and set it back to “ON” it should dim to 0%.
But the dimmer only gets the value 0%.
What am I doing wrong?

Openhab: Openhab 3.1m4
System: Windows 10

image

triggers:
  - id: "1"
    configuration:
      itemName: SwitchDummy
      state: ON
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    label: Lichtan/aus
    configuration:
      blockSource: <xml xmlns="https://developers.google.com/blockly/xml"><block
        type="controls_if" id="gkt+EW@]!Le@uUevt5Dw" x="268" y="148"><mutation
        elseif="1"></mutation><value name="IF0"><block type="logic_compare"
        id="(Yzk0`P6dlPez5=AYz,B"><field name="OP">GT</field><value
        name="A"><block type="oh_item" id="?PxB/Jy7[Q*:{L`X_l_3"><field
        name="itemName">ModbusData32000_ValueasDimmer</field></block></value><value
        name="B"><block type="text" id="G~Yks3vn~9as_0n^?C~1"><field
        name="TEXT">70</field></block></value></block></value><statement
        name="DO0"><block type="oh_event" id="4^AP_rrutVK]Z1`RnoeU"><field
        name="eventType">sendCommand</field><value name="value"><shadow
        type="text" id="B-a{+rnN*weuPEE8rzz-"><field
        name="TEXT">0</field></shadow></value><value name="itemName"><shadow
        type="oh_item" id="cpj5s_R`XT1*ENN6?6?U"><field
        name="itemName">ModbusData32000_ValueasDimmer</field></shadow></value></block></statement><value
        name="IF1"><block type="logic_compare" id="-7UB3O(IJ01jE:f=591p"><field
        name="OP">LT</field><value name="A"><block type="oh_item"
        id="P{;8]_}?vM;4`I8.09|H"><field
        name="itemName">ModbusData32000_ValueasDimmer</field></block></value><value
        name="B"><block type="text" id="Hrc@JqC$9F{c0orZ[9HW"><field
        name="TEXT">70</field></block></value></block></value><statement
        name="DO1"><block type="oh_event" id="muTk3alHE7mz^#2!B475"><field
        name="eventType">sendCommand</field><value name="value"><shadow
        type="text" id="jR!%yAAoA6]`R8Y^LZby"><field
        name="TEXT">90</field></shadow></value><value name="itemName"><shadow
        type="oh_item" id="]v1Ar+f`Iqk,C+zoTxol"><field
        name="itemName">ModbusData32000_ValueasDimmer</field></shadow></value></block></statement></block></xml>
      type: application/javascript
      script: |
        if ('ModbusData32000_ValueasDimmer' > '70') {
          events.sendCommand('ModbusData32000_ValueasDimmer', '0');
        } else if ('ModbusData32000_ValueasDimmer' < '70') {
          events.sendCommand('ModbusData32000_ValueasDimmer', '90');
        }
    type: script.ScriptAction

There are a couple of issues here. The first is this:
image
An item is not the same as it’s state. An item is a complex objects with several different properties only one of which is the state of the item. In this case instead of the state of the item you are getting the name of the item (because a string of the item name is the default result when an item object is used in a non-object context).

Second is this:
image
The double quotes block is a string variable not a number variable. '70' is a string of length 2 with the character 7 followed by the character 0; it is not the number 70.

The result you are seeing then, may be more clear in this case if you look at the script equivalent that is produced by the blocky code:

if ('ModbusData32000_ValueasDimmer' > '70') {
    events.sendCommand('ModbusData32000_ValueasDimmer', '0');
} else if ('ModbusData32000_ValueasDimmer' < '70') {
    events.sendCommand('ModbusData32000_ValueasDimmer', '90');
}

The first line of this code is asking the rule to compare the string 'ModbusData32000_ValueasDimmer' to the string '70'. String comparison like this actually just alphabetizing the the two inputs, so in this case it looks at the M and the 7 and declares the first input to be greater because alphabetically letters come after numbers. So, this rule will always trigger the first condition and send the 0 command to the item.

To fix this you need to use some different blocks. For the first input, you need to be using the Get Item State block in openHAB that actually extracts the state. Then you need to use the number variable block in Math for the second input.

Here’s the rub: there’s a current bug in blocky that doesn’t recognize that the Get Item State block can return a number and so it won’t let you put it into a comparison logic block with another number. I think the following workaround for this should work. Create a variable block (click on Variables then Create variable... and type in any name that makes sense to you, in this example stateNumber) and then set that variable block to the item state and compare the variable to the a number block.
image

image

@JustinG
Thank you, it works!

I learned something again, but of course I have to read the status of the item and transfer it into a variable.