[SOLVED] SendCommand or postUpdate to item with two or more variables

Hello,

it there a way to transfer 2 or more variables (or an Array) to one item to use it in this way:

postUpdate.Item1(Var1, Var2...)

Then use it in rules like:

when
Item1 recieved command (or update)
then

So the background to use it like function.
It there any way?

An Item has only one state, and can only perform one command at a time.

There are many ways to encode information into a single object e.g. JSON as a String…

1 Like

Oh perfect way! Thanks for advice!

I found no json_encode and json_decode like in php, so in this way i need to use JsonPath Transformation Service?
There i have also not found how i can encode a string, only decoding of it.
Maybe you have an simple example or in OH it is not so simple? :grin:

Thanks!

No, I have no samples of that. A moment’s searching turned up this old post

Note post 2 - if you’re trying to do this, it is probably not the best way. But we don’t know what you are actually trying to achieve

Clonky way to do it

I would like to make a kind of function for Logging in Logs, Telegram and Email

I will send to item 2-3 variables. For example

var prefix = "Test Prefix"
var message = "Test Message"
var type = "Email-Telegram" or "Logs-Email-Telegram"

and then rule to parse it and make correspondings logs.

Well, you have the tools to encode into one Item state.

Or you might use three String type Items. They don’t cost you anything. Just make sure to set up in some established order and trigger the “do it” rule(s) from the last one to be changed.

I have made a rule, put them in one item and then parsed them.
It seemed to be a great solution. But, if 2 or more log events comes at one time or close to each other “item” cant not deal with them and get randomly only one of them.
So it is not realy good way ((( but i do not see other. Maybe you see?

Thanks!

Yes, you will have problems handling fast data.

Part of the solution is to encode your logging message into a single package, so parts of one cannot get mixed up with parts of another.

An approach to getting openHAB to queue rapid-fire messages for you is not to use the Item state, but to use commands instead. So long as you trigger any processing rule from received command and use the data in that command, openHABs event bus will manage it and every event will get dealt with.

You’d probably need to lock your rule so that it processed one message at a time, otherwise you will run into the same problem with whatever services it passes messages on to.

Here is the rule

rule "Logging"
when
	Item LogString received command 
then
    val String currentTime = String::format( "%1$tH:%1$tM:%1$tS %1$td.%1$tm.%1$tY", new java.util.Date )
    val botType = LogString.state.toString.split('<>').get(0)
    val prefix = LogString.state.toString.split('<>').get(1)
    val message = LogString.state.toString.split('<>').get(2)

    if (botType.length > 1) {
            sendTelegram (botType, prefix + " | " + message + " [" + currentTime + "]")
    }
    logInfo (prefix, message + " [" + currentTime + "]")
   
end 

i am sending commands so

LogString.sendCommand("bot<>" + MessagePrefix + "<>" + Message) 

So what do i need to do to make it work good and how to lock it?

PS
If i understood right, i changed this lines to

    val botType = receivedCommand.toString.split('<>').get(0)
    val prefix = receivedCommand.toString.split('<>').get(1)
    val message = receivedCommand.toString.split('<>').get(2)

That’s it, do not use state when triggering from command.

Only do this if you really need to.
If your rule is really that simple, and using new variables (‘val’) at each iteration (so that nothing is shared) I do not think much harm will come if you get two triggered instances of the rule running at the same time.

If, say, telegram can’t handle messages that fast (would not surprise me), locks are not the answer for that anyway.

Locking if you really need it.

The risk with using a lock is that if you get an error (say, an unexpected NULL state) the lock can get locked forever. Well, until next reboot/reload.

1 Like

Design Pattern: Gate Keeper is the answer.

1 Like

I know it already, and use it here.
But there ir the same situation and as i understood would not be solved with this rule. I thing you have seen it https://community.openhab.org/t/say-can-not-say-two-or-more-phrases/84805/16?u=thisisio

No, the problem you had in that thread is absolutely solvable by using the Gatekeeper Design Pattern.

1 Like