Rules beginner question

Hello community,
i have a question about creating my first rule and have absolutely no idea where to start.

I would like to transfer a value from a String Item to a Number Item every time the value in the String Item changes.
I ask about the string item e.g. the available storage space. As an example I get 45% back and would like to transfer this value to a number item for further use.

Unfortunately, I have not found any examples in the openHAB documentation or here in the forum that could help me.

This is what i tried:

rule “Konvertiert den Ausgabewert in den Ausgabestatus”

when

Item AeonSD_Output changed

then

// post the new value to the Number Item

AeonSDBelegung_RCkgabestatus.postUpdate( newValue )

end

And so far only 0 comes out in the display. The value should be transferred one to one at the end. So 45% = 45%.
Can someone tell me where my mistake is?

Kind regards

Looks like your problem is newValue. This is not a recognized variable unless you have defined it somewhere else in your rule file. I suspect what you were looking for is the implicit variable newState

Also, for future posts, please try to use code fences as it makes the posts much easier to read and troubleshoot.

2 Likes

Thanks for your quick answer. Unfortunately, the code buttons are not visible to me, so I’ve never seen them.
I’m trying to get it right now.

rule "Konvertiert den Ausgabewert in den Ausgabestatus"
  when
    Item AeonSD_Output received update
 then
    // post the new value to the Number Item
    AeonSDBelegung_RCkgabestatus.postUpdate(newState)
 end

This is what the rule looks like now, but unfortunately the result is still 0. Is it important to mention that I query the value via the exec binding?

Kind regards

Try using newState.toString in your postUpdate. However, why not just define the original Item as a Number instead of a String?

It would be quite helpful to clarify what your input string is.
You will see that in your events.log
Or you can add temporary diagnostics to your rule

loginfo("test", "my rule was triggered")
logInfo("test", "the item state is " + newState.toString)

Exec binding returns only strings. Profiles are not yet capable of matching/parsing channels to other Item types.

Hmmm… the docs show an example with a NumberItem.

That’s the exit code, not the user script output

1 Like

With newState.toString it works so far. But the% character is not transmitted. That must be transformed with RegEx?

If you defined your target Item as a simple Number type, 32% is not a simple number.

openHAB does have what are referred to as Quantity types, such as Number:Dimensionless for percentages, that have units as part of their state value.

Remove the % (by string manipulation perhaps) or change the target Item type.

I’ll try it.