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;4.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.