Sorry if this is a stupid question. So i set up openhab the other day and also setup a telegram api and a basic rule with a wind speed notification just as a test as below and it works great. so I was wondering how i could include the “Wind_Speed_Mph” from and to in this rule so it tells me what the wind speed was and now what the new value is. I can see it outputs this to the log bit not sure how to get it into this rule
rule “Send telegram with Fixed Message”
when
Item Wind_Speed_Mph changed
then
sendTelegram(“bot1”, “The Wind Speed Has Changed”)
end
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 variablespreviousState and newState.
But you have to use OH 2.5.stable( Maybe 2.5.M5 or M6 works too)
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.
Use the v2 Telegram binding rather than the Telegram Action. The Action is deprecated and will no longer work in future versions of OH.
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.
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.
No worries . 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 ?
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.
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
Thoughts?
rule “Send telegram with Formatted Message”
when
Item Wind_Speed_Mph changed
then
sendTelegram(“bot1”, "The Wind Speed Has Changed to " + Wind_Speed_Mph.state.toString)
end
2020-01-04 11:16:20.227 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘telegram.rules’ is either empty or cannot be parsed correctly!
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
“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.
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.
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 previousState.toString
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,
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 someItem.previousState.state
In this case you’d further want to use it as a string someItem.previousState.state.toString
This has more overhead than the implicit variable, because of database accesses and processing.
this is my rule, exactly as you have stated above someItem.previousState.state.toString
rule "Send Telegram Wind Speeds"
when
Item Wind_Speed_Mph changed
then
sendTelegram("bot1", "The Wind Speed Has Changed From " + Wind_Speed_Mph.previousState.toString + " To " + Wind_Speed_Mph.state.toString + " Mph")
end
rule "Send Telegram Temperature °C"
when
Item Temperature changed
then
sendTelegram("bot1", "The Temperature Has Changed From " + Temperature.previousState.toString + " To " + Temperature.state.toString + " °C")
end
rule "Send Telegram Humidity"
when
Item Humidity changed
then
sendTelegram("bot1", "The Humidity Has Changed From " + Humidity.previousState.toString + " To " + Humidity.state.toString)
end
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