Rule question

Hi Mike,
I do not use the Telegram Action, but as I understand the Docs you can use it with patterns inside the second string of your “sendTelegram” and as variables you can use the implicit variables previousState and newState.

But you have to use OH 2.5.stable( Maybe 2.5.M5 or M6 works too)

1 Like

Items have a state. The state bears the “value” of the Item. To access the state of an Item in a Rule, you call Wind_Speed_Mph.state.

To build up a String you can use the + operator.

Putting the two together:

sendTelegram("bot1", "The Wind Speed Has Changed to " + Wind_Speed_Mph.state.toString)

The toString forces the state to become a String. Otherwise it will be a Number:Speed type.

Since you are just now getting started with OH I have two suggestions for you.

  1. Use the v2 Telegram binding rather than the Telegram Action. The Action is deprecated and will no longer work in future versions of OH.

  2. Consider using Scripted Automation. It’s a little rough to get started compared to Rules DSL (you have to install separate addons) but in OH 3, Scripted Automation will replace Rules DSL as the default way to write Rules. This will put you in a better position when 3.0 comes along than if you get started with Rules DSL now. There is a draw back in that there are fewer examples to go by. But I think you will find users on the forum like me will be willing to do a bit more work for you just to get some more examples on this forum.

1 Like

Sorry for my amateurish attempt to help :cry:

That wasn’t directed at you at all. Normally we here on the forum have a “we won’t write it for you” stance. But for Scripted Automation, many of us will be more willing to actually write it for you just so we can get more examples of Scripted Automation in the forum. That comment was just to say that beginners who start with Scripted Automation might get a little more hand holding from us than those using Rules DSL.

You never need to apologize for trying to help. It’s always appreciated.

1 Like

No worries :smiley:. But as said, I’m still a beginner too, but nosy what comes in the future. I don’t know exactly what “Scripted Automation” means, how to get it and how to handle it. (I think it’s not only JSR223)
So I hope I can get a bit “hand holding” too, to learn a bit more about this theme. Is there a road-map, where I can participate from ?

Actually it is, plus a bit more.

Helper Libraries for openHAB Scripted Automation — openHAB Helper Libraries documentation and if you want to use Python [beta testers wanted!] Jython addon w/ helper libraries (requires OH 2.5.x)

Of course

I don’t know if there is a roadmap. The underlying rule engine had been around for years and is very stable and feature complete. So if you want to write rules using text files a la JSR223, that’s solid as can be. The Helper Libraries make using it just about as easy as Rules DSL.

Where it needs work is the UI that will let us create roles through the replacement to PaperUI.

1 Like

Hi All

Thanks for all the reply’s and suggestions, the binding I am using is weather1 and the action is telegram.

as suggested by rlkoshak by rule now looks like this but i am not receiving any notifications


rule “Send telegram with Formatted Message”
Item Wind_Speed_Mph changed
sendTelegram(“bot1”, "The Wind Speed Has Changed to " + Wind_Speed_Mph.state.toString)

2020-01-04 11:16:20.227 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘telegram.rules’ is either empty or cannot be parsed correctly!

Sorry it was my mistake I had a random character further up that was causing the issue after a bit of faffing about its now all working great

Thank you very much for your help

If I wanted to build on this rule, as you mentioned above we call Wind_Speed_Mph.state .

as an example, if I wanted to know what the wind speed had changed from and to, I assumed the

" + Wind_Speed_Mph.state.toString)

Would become

" + Wind_Speed_Mph.state.fromString)

but that does not seem to work, what am I doing wrong?

Also, my notifications are now i.e. “The Wind Speed Has Changed To 16.11”

If I wanted to add Mph like so “The Wind Speed Has Changed To 16.11 Mph” is this possible?



Try adding + “M/ph” to the end.

You assumed wrongly. I strongly suggest reviewing How to get started (there is no step-by-step tutorial) and the Rules documentation page. The answers to what you are trying to do now a will as the many hundreds more you will experience along the way are there. Blindly trying stuff is not likely going to ever work, especially if you have no experience with programming. You should always fires look to the docs and then look for examples.

For example, you want to know what the previous state of the item was? Rules | openHAB

what happened to,

“Normally we here on the forum have a “we won’t write it for you” stance. But for Scripted Automation, many of us will be more willing to actually write it for you just so we can get more examples of Scripted Automation in the forum.”

i was just looking for some pointers with my first openhab rule.

looking at

previousState is the correct variable?

You are asking for help with the old rule engine, which it was suggested that you not use. Scripted automation, which is largely Jython, is a different animal. The examples in the forum will be transitioning more and more to Jython. It’s those examples that you will see some more assistance with as the community comes up to speed with the new rule engine and scripted automation.

Yes… or event.oldItemState in scripted automation.

So long as you have a changed trigger. You’ll be fine with the (DSL) rule shown.


sendTelegram(“bot1”, “The Wind Speed Has Changed From " + Wind_Speed_Mph.previousState + " To " + Wind_Speed_Mph.state.toString + " Mph”)

when i received telegram msg previousState value is always null

This would call on openHABs persistence service to recover the particular state of the named Item. If you haven’t installed a persistence service, set it as default, persisted that Item, and have some data already stored … you won’t get anything.

previousState completely on its own has nothing to do with any of that. When a “changed” event triggers a rule, the event also carries previous state information. That is made available within the rule only as previousState, an implicit variable of type state.
You’ll be wanting

Thanks for the reply

now it makes sense why I saw the below with this previousState.toString

Rule ‘Send Telegram Wind Speeds’: cannot invoke method public java.lang.String java.lang.Object.toString() on null

ok i have done a bit of research on persistence and have map dB up and running i believe, please bear with me as I have only been using OpenHab for a few days,

the above error has now gone from the log,

this is my mabdb.persist

Strategies {

default = everyChange


Items {

Wind_Speed_Mph,Temperature,Humidity : strategy = everyUpdate, restoreOnStartup


I receive notification on telegram, but they read

"The Wind Speed Has Changed From org.openhab.core.persistence.internal.QueryablePersistenceServiceDelegate$1@ca4888 To 14.99 Mph”

What have I done wrong?



I have no idea why you are messing with persistence. You do not need to use it with this rule.
SomeItem.previousState is a method of an Item that uses persistence. You do not need that because …

Every rule with a changed trigger has an implicit variable that happens to be named previousState. There is no need for any kind of persistence for that to work. It’s just a variable.

logInfo("test", "previous state from event " + previousState.toString)

Though, having gone to the trouble of setting up persistence, you might want to use it for the exercise. The trick about persistence previousState method is that a returns an Historic state object - with additional info like timestamp. This is in the docs.
To get the state, you have to ask for it
In this case you’d further want to use it as a string

This has more overhead than the implicit variable, because of database accesses and processing.

1 Like


so removing persistence out of the mix for a min,

this is my rule, exactly as you have stated above someItem.previousState.state.toString

rule "Send Telegram Wind Speeds"
   Item Wind_Speed_Mph changed
   sendTelegram("bot1", "The Wind Speed Has Changed From " + Wind_Speed_Mph.previousState.toString + " To " + Wind_Speed_Mph.state.toString + " Mph")

rule "Send Telegram Temperature °C"
   Item Temperature changed
   sendTelegram("bot1", "The Temperature Has Changed From " + Temperature.previousState.toString + " To " + Temperature.state.toString + " °C")

rule "Send Telegram Humidity"
   Item Humidity changed
   sendTelegram("bot1", "The Humidity Has Changed From " + Humidity.previousState.toString + " To " + Humidity.state.toString)

This is the error seen in the logs, no msg recieved,

2020-01-05 21:50:34.852 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Send Telegram Wind Speeds': cannot invoke method public java.lang.String java.lang.Object.toString() on null

2020-01-05 21:50:34.852 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Send Telegram Humidity': cannot invoke method public java.lang.String java.lang.Object.toString() on null

2020-01-05 21:50:34.852 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Send Telegram Temperature °C': cannot invoke method public java.lang.String java.lang.Object.toString() on null

No, it’s not.

This would be the change, as @rossko57, mentioned is needed:

sendTelegram("bot1", "The Wind Speed Has Changed From " + Wind_Speed_Mph.previousState.state.toString + " To " + Wind_Speed_Mph.state.toString + " Mph")

You didn’t add “state” between “previous” and “toString”.