I am trying to create a rule to send a notification via Telegram if a liquid level is reached.
The Thing and Item with the level is working so far.
I tried to create a rule triggered if the item state is updated and the value > 20.
But it should only send once not every update until it is below 20 again.
I also tryied this:
Int_level_max is an item which I want to use to trigger the rule to send the message:
A simple way would be to create a proxy switch, then use a rule to turn it ON > 20 and OFF < 20. Then send your notification on the switch action (when switch changes from OFF to ON.
Take your requirements and break them down and implement them one by one.
Get the water level. Done via “The Thing and Item with the level is working so far.”
Trigger a rule when the water level updates. Done via “type: core.ItemStateUpdateTrigger”
Send an alert when the water is over > 20. Done via “if (level.state > 20)”
Only send an alert once when the water is > 20 until the water level drops below 20 again.
OK so that’s a little more complicated. Let’s break 4 apart into smaller steps.
4a. Send the alert. Done via “Int_level_max.sendCommand(ON)”
4b. Record when the alert was sent. privateCache.put('alertSent', true)
4c. Only send the alert if one hasn’t already been sent. if(privateCache.get('alertSent', [ | false]))
4d. All an alert to be sent again once the level has dropped to 20 or below.
For a more capable approach that includes hystersis and do not disturb periods see Threshold Alert and Open Reminder [4.0.0.0;5.9.9.9] which can handle this use case. All you’d need to do is code the alert part and configure the rule with your Item, threshold as desired.
Add logging. What’s the state of level? Is it a plain number or does it have units?
Given it’s Rules DSL, you probably have to cast the state to a Number before comparing it. Notice how you can just get the state as a number in Blockly avoiding these sorts of complications and confusions.
Log out level.state and find out. Most likely not. Operations like > work with ints or floats. But the state of an Item isn’t just a Number. It’s a State with all sorts of stuff and Rules DSL often has problems figuring out what the state of the Item is.
But I will say it again, when a rule dioesn’t work, log out everything.
If you can not or don’t want to use the reference docs as your first place to look for basic questions like these I cannot stress strongly enough that you will be much better off using Blockly where the answers to such questions are right there in front of you:
When a statement is logged it goes to openhab.log. If you’ve installed openHABian you can see the logs through Frontail. Otherwise you need to look at the files themselves.
There was an entry that the item “Int_level_max” received an command “ON”.
The item level stated updates from 21.5 to 21.6 and so on.
But the item “Int_level_max” still stated “NULL” in the items list
So I wanted to try to add the part of the script to switch it OFF again, but now the logging script prints a message if I try to start it: “script cannot be run if it is uninitialized!”
I deleted it and created a new one… still the same.
This looks like a contact, and generally Contact item types don’t have OnOff as options. It is usually OPEN and CLOSED. You might be sending it ON/OFF, but it is probably ignoring it.
Maybe show the thing. Is Int_level_max linked to any Thing?
If you don’t see a “Item changed from blah to blah” that means the Item’s state never changed. So either Int_level_max is already ON/OFF (which is clearly not the case) or something went wrong with the command. Do you see anything in openhab.log? I would expect to see an error indicating that you cannot send a command to an Item of type Contact or ON is not a usable state for a Contact Item or something like that. These errors are not in events.log.