General Rule help needed!

Hi,

I would like to create an item which starts with a number, e.g. 1000, which increments by a sensor trigger. I created an item and a rule, but I don´t understand how to bring them together:

Item:

Number StromZK "Stromzählerstand [%s]"	<line-decline>	(gBM)	{mqtt="<[mysensor:mygateway1-out/5/1/1/0/16:state:0:1],<[mysensor:mygateway1-out/5/1/1/0/16:state:1:0]"}

Rule:

var Number StromZK = 1000
rule “Stromzaehler”

when
Item StromZK received update 1
then
{

  StromZK = StromZK+1

}
end

I don´t even know what this [%s] in the item is for… I hope you can help me

I think that you should create a dummy Item that you can then modify it’s state value when the actual Item (StromZK) gets updated.

If you modify the state value of the actual item, it will be overwritten the next time that it gets an update from the mqtt topic.

Also, in your rule, I believe that you will have to use something like: Dummy_Item_Name.postUpdate()
Search in the https://community.openhab.org/c/setup-configuration-and-use/scripts-rules section for some examples.

Remember to use a persistence service also :slight_smile:

Plus, you really do not want to create both an Item and a variable var with the same name. They are not the same thing at all, and you will never know which your code will use when you refer to that name.

Ok guys, I really started over and the first thing I did now was creating a MySQL Database…

So now I am trying to connect my StromZK item to the DB to uodate it´s values. Any help is very appreciated…

edit: I created a table using phpmyadmin, but how do I tell an item to which table in which db it must connect to read it´s data??

edit2: mmh, seems like openhab is managing the DB on it´s own!?! But it created only 3 item tables and no “StromZK” item. How can I test openhab -> MySQL connectivity?

Yep. In OH this is referred to as persistence
http://docs.openhab.org/addons/persistence.html
http://docs.openhab.org/addons/persistence/mysql/readme.html

yes, I´ve installed everything as you described in you tutorial. Now I see 4 tables: Item1, Item2, Item3, Items. In ITems I can really find some items: Light_Wz_Bild, Light_Wz_Couch, Light_Wz_Deckenlampe. But it´s only 3 items out of many more…

So I want this item:

Number StromZK “Stromzählerstand [%s]” (gBM)

to display a value which is stored in a db. I have no idea what I must write in the items file to connect the Database. Or does it pull the data automatically? If so, how does it know which value I want? E.g. I want to save Time and Value…

Have another look at the tutorial.
It describes a file mysql.persist
Within that file is an editable section Items
There you tell SQL which items, or groups of items, to record ; and when to record them.

Ok, I got my Database running fine now, it´s recording my items as it is supposed to.

Now one more question in direction SQL:

the database records the behavior of my sensor with the Value OPEN / CLOSED and the time when this happens. So my table has 2 columns and a lot of rows now.
How can I manage in openhab2 that the item shows me a value e.g. 1000 as an initial value and than adds +1 whenever the sensor writes an OPEN into the database?
In the end I want to have a power consumption measurement which shows me tha actual KWh consumption / month and I also like to build a diagram where I can see at which time most power was consumed.

edit: I think I need another table, let´s name it StromZ, inside the db with the initial Value (e.g. 1000) where +1 gets added whenever item StromZK gets an OPEN!?! I have no idea how to realise this. From my expirience now openhab only generates a table for an item when it gets an update, but how should I update an item like StromZ which is not attached to a sensor?

That doesn’t seem to e a database related requirement.
I’d create a number item for counting.
Trigger a rule to set it 1000 whenever (at system start? when the date changes? when a button is pushed?)
Trigger a rule from the sensor item changing to OPEN and have that rule add 1 to the counter.

I’m not sure how that helps with

Might be some ideas here