Easiest possible rule: Add two items (and round) (in code page)

Hi, have two items A and B and want to add another one C.
C should be (rounded) sum of A+B.

Howto fill that code page ? That looks very much different from documentaion and I am a little lost

configuration: {}
triggers: []
conditions: []
actions: []

(I know Items can be added by a sum group members - but that should be just the starting point of my experiments with rules)

There are several possible approaches.

And you can then set the State Description Pattern on the Group Item to round the value. %.0f

The docs have a page under concepts that explains the three parts of a rule one what they are for. The Getting Started Tutorial has several pages dedicated to showing how to construct rules of growing degrees of complexity. And each rules programming language has it’s own reference documentation. Have you read and understood theses?

Sure I read this, but I doesn’t get it. I try to solve with blockly. It is somewhat difficult to understand. So there is a “1 + 1” operation, but you can add the items, but the “state”. I would call it a value, as a state imho is something different.
Also I would assume that I can simply add two items.

Same with triggers. Call me stubid - It took me two hours until I found a combination of triggers and calculation.

I finally end up with (which likely works simplier)

configuration: {}
triggers:
  - id: "2"
    configuration:
      itemName: sm_consumption
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      type: application/javascript
      script: |-
        var A, B, C, D, E, F;
        A = itemRegistry.getItem('solar').getState();
        B = itemRegistry.getItem('sm_consumption').getState();
        C = A + B;
        D = C * 100;
        E = Math.round(D);
        F = E / 100;
        events.sendCommand('sm_calc_consumption', F);
    type: script.ScriptAction

I’m not sure what it works be different, but “state” is the term the developers choose for the current disposition of the Item. And it makes sense. Items represent real world devices. You wouldn’t say " the value of the light is on".

An Item consists of:

  • name
  • label
  • state
  • link(s) to channels
  • metadata
  • members (if it’s a Group)

What would it even mean to add two collections of data made up of all that?

Understand. Sure, as switch has 2 states. Maybe that was the origin of openHAB. Many items have values (temperature, brightness, power). It’s bit weird to call that states - but ok, you just have to be aware of it.

What drives me nuts was the to add two values in rule via blockly.
The math 1+1 operator can add items, but in fact it has to add states of items. But that doesn’t match. I need an hour to get the running.

Also hard to understand is “sending command” is used for “update a state”

Although there is done a lot to make it easier for beginners, that are things that makes me headache.

No it can’t. That’s the problem. An Item is more than just it’s state. And it’s state isn’t always a number that math can be done with (any Item can carry UNDEF and NULL). If I have the Item which consists of:

{
  "link": "http://10.10.1.112:8080/rest/items/vWeather_Temp",
  "state": "20.804 °F",
  "stateDescription": {
    "pattern": "%.0f ॰F",
    "readOnly": true,
    "options": []
  },
  "editable": true,
  "type": "Number:Temperature",
  "name": "vWeather_Temp",
  "label": "Outside Temp",
  "category": "temperature",
  "tags": [
    "Measurement",
    "Temperature"
  ],
  "groupNames": [
    "Weather_Equipment"
  ]
}

and I want to add it to another Item which consists of

{
  "link": "http://10.10.1.112:8080/rest/items/Mainfloorsensors_Temperature",
  "state": "68.42 °F",
  "stateDescription": {
    "step": 1,
    "pattern": "%.0f °F",
    "readOnly": true,
    "options": []
  },
  "editable": true,
  "type": "Number:Temperature",
  "name": "Mainfloorsensors_Temperature",
  "label": "Main Floor Temperature",
  "category": "temperature",
  "tags": [
    "Measurement",
    "Temperature"
  ],
  "groupNames": [
    "Mainfloorsensors"
  ]
}

What does that even mean? An Item is so much more than just it’s state. The state is one among many properties on an Item. So if you want to add two temperatures together, you need to get the two temperature values. Not the whole data structure.

Well, it’s not. See Rules | openHAB for a full explanation of the difference between sendCommand and postUpdate.

In short, sendCommand when you want an end device to do something (e.g. turn on a light). postUpdate when you want to change the state of an Item. Updates do not get sent to the end device, they stay within openHAB. Note that not all commands are reflected as a state on an Item (e.g. INCREASE/DECREASE) and not all commands result in an Item changing state.

Understand and item is more than its value/status.

I mean it can 1+1 can add (theoretically)items. An item can dock to that operator graphically (which seem to be a bug to me), but docking a get_state(item) fails. If I understood you right, that should work, but it doesn’t lock-in in blockly. Or what can a math operator 1+1 mean otherwise?

Ok, changed my working sendCommand to postUpdate. Seems to be both are working.

That’s a detail concerning Blockly specifically which and I’m not an expert in that. Have you watched the YouTube tutorials? Rules Blockly | openHAB

But in general, only the blocks you see under the openHAB section are written by openHAB. All the other blocks in all the other categories are standard Blockly which means they don’t know anything about openHAB Items or anything else.

In fact, I cannot get either the Item block nor the “get state as Item” block to dock to the + operator block. But I’m running OH 4 snapshots and Blockly has had a ton of work done since January. In order to dock them to the addition block I need to assign them to a variable first. But that causes them to lose their type meaning I can dock any variable to the addition block.

var test, foo, bar;


test = items.getItem('MyItem').state;
foo = items.getItem('MyItem');
bar = test + (foo - 1);

Note that because I’m on OH 4 snapshots, Blockly “compiles” into JS Scripting ECMAScript 2021 instead of Nashorn JS Scripting ECMAScript 5.1 so the code will look different.

There is a feature called “AutoUpdate” which can be configured on an Item by Item basis. It’s enabled by default and what it does is guess what state the Item should become based on the command and updates the Item accordingly. If you disable that, sending a command to an Item will not cause the Item to update. Something else (e.g. the binding after the device reported it’s new state) will update the Item.

Yes, can confirm. I also worked around this with variable. :slight_smile:
Maybe blocky in 3.4 has still somethings to learn. It is cool feature. Thanks for explaining!