Rule to trigger notification when value is > number

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:

label: Int_level_max
type: Contact
category: water
groupNames: []
groupType: None
function: null
tags:
  - Alarm
  - Level
configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: level
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |-
        if (level.state > 20)
        {
          Int_level_max.sendCommand(ON)
        }
    type: script.ScriptAction

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.

  1. Get the water level. Done via “The Thing and Item with the level is working so far.”
  2. Trigger a rule when the water level updates. Done via “type: core.ItemStateUpdateTrigger”
  3. Send an alert when the water is over > 20. Done via “if (level.state > 20)”
  4. 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.

else {
    privateCache.put('alertSent', false)
}

Putting it all together:

if(level.state > 20 && privateCache.get('alertSent', [ | false]) {
    Int_level_max.sendCommand(ON)
    privateCache.set('alertSent', true)
}
else {
    privateCache.set('alertSent', false)
}

Or, because I strongly recommend against new development of rules in Rules DSL, in Blockly:

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.

That’s what I am trying there.

I don’t see a proxy switch. You are triggering on level item. Or do what Rich said, which is functionally similar (variable vs proxy).

My problem starts already with

Int_level_max.sendCommand(ON)

The item always shows NULL

If this would work I wantedt to try it the way kjknauss suggested.
If that’s possigle, since it seems easier.

If I understand correct

my Int_level_max is my switch which I want to switch on, if the level is > 20.

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.

if(level.state as Number > 20)

like that:

Is it possible that I need to do a 20.0 as it’s a float?

label: level
type: Number
category: water
groupNames:
groupType: None
function: null
tags:

  • Measurement
  • Level
channels:
  - id: level
    channelTypeUID: simatic:chNumber
    label: level
    description: level
    configuration:
      stateAddress: DB500.DBD800F

:person_shrugging: 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.

sorry
 how to log it?

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.

Thanks.
I will read into it.

I figured out the logging


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.

grafik

label: Int_level_max
type: Contact

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?

1 Like

OK, that’s close to what you want. But that line is just going to log out the word “level”. You want to see what the state of the Item level is.

image

I’m assuming you know to replace “MyItem” with your level Item. Just click it and you will see a list of all your Items to select from.

You can build of complicated strings to log using the
image
block. Click the gear and add an “Item” for each part of the String you want to create.

Also you need to watch the logs. There should be an error in the logs when you try to send an ON command to a Contact Item.

1 Like

I will try to change it to switch then.

2024-07-03 21:15:26.099 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'level' changed from 14.5 to 14.1
2024-07-03 21:15:26.101 [INFO ] [openhab.event.ItemCommandEvent] - Item 'Int_level_max' received command ON
'level' changed from 13.1 to 12.8
2024-07-03 21:15:30.100 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Int_level_max' received command OFF

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.

If Int_level_max is a switch, then just create this on a trigger of ‘level’ changed:

image

Then another rule to send Telegram when Int_level_max changes from OFF to ON.

1 Like