Set item value from rule or script

Tags: #<Tag:0x00007efec62a80e8> #<Tag:0x00007efec62a7b48>

Hi, everyone!
I’m new in OH, so, sorry if this is stupid question.
I’m gathering power meter reading using simple device sending number of pulses of meter LED (works through MySensors network). Every minute I’m getting a value of consumed energy in kWh (say Consumed_kWh item) and want to add this value to one of two items values - Day_kWh or Night_kWh depending on daytime.
It’s was easy to create rule to “do something when Consumed_kWh value is updated between 7:00 and 23:00”, but I totally can’t find how to set needed value to needed item. I’m familliar with python so I’ve tried to “run script”, but I’ve found no document or example of setting value to item. I assumed something like ir.getItem("Night_kWh").postUpdate(12.0) will work for me, but it doesn’t.
I’m sure this must be easy to do such simple thing, but can’t find a way.
If you know how to solve this, please, give a step-by-step plan - maybe I’m just looking in wrong direction.
Thanks in advance!

If you are using Python as your rules language, But How Do I…? — openHAB Helper Libraries documentation is going to be your best resource right now. You will want to download and install the Helper Libraries (look for the PR that has OH 3 support).

In answer to your specific question you need to use events.

events.postUpdate("Night_kWh", "12.0");

Thanks for your reply!
I guess I’d not like to install to my server a module which is not even in “Beta” but in “Pull request” state :slight_smile:
Maybe there is another way to perform an a+=b operation in OH? Doesn’t matter on which language.

In DSL rules it’s just
yourItem.postUpdate(12.0)

The only difference between the main released branch and the PR is there are a couple of changes required to work with OH 3, namely moving to ZonedDateTime and the location of the Logger actions moved.

But that isn’t why I sent you to the But How Do I docs. Even if you don’t use the Helper Libraries, that doc is going to be your go to resource for examples for how to do stuff like this. If you see an import from core that means it’s using the Helper Libraries and you’ll have to look at that code to see how to do it. If not, that’s how the language works out of the box.

The events.postUpdate that I showed does not use the Helper Libraries. That’s how you postUpdate or sendCommand in Python, JavaScript, and Groovy.

Thanks everybody!
I’ve finally made it work using DSL.