Sorry for my amateurish attempt to help
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 ?
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.
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
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!
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?
Thanks
Mike
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.
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.
using
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
previousState.toString
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?
Thanks
Mike
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.
Hi
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"
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
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”.
I really don’t know how else to put this.
Wind_Speed_Mph.previousState.toString
Temperature.previousState.toString
Humidity.previousState.toString
are all methods on your Items and absolutely rely on persistence.
(as @H102 points out, you’d not be getting the property you want anyway)
The part you don’t seem to believe is
previousState.toString
is completely unrelated to the above, and does not use persistence at all.
There is no Item in that construction, because it is a not a method of an Item, because it is derived from the event that triggers the rule.
It is an implicit variable within that rule only.
It exists only if the yule is triggered by change.
It automatically gives the previous state of whatever Item triggered the rule.
You do not specify the Item name anywhere near it.
Just put
previousState.toString
in your three examples and stop adding the Item name.
Thank you rossko57
That clears that up and its now working as i wanted
Thanks Again