Simple rule, cannot understand results

  • Platform information:
    • Hardware: _RPI4
    • OS: Raspberry OS

I have a simple rule to set a switch ON/OFF depending on a number value, but it seems to be giving the opposite result. The rule takes a value from an Number item

Number OS_Oil_Battery “Gauge” (OS) {channel=“mqtt:topic:61db7b94:B1”}

And if the item has a value 128 should turn a switch item ON, if its anything else, turn it OFF.
The rule is:

rule "Oil Battery"
when 
    Item OS_Oil_Battery  changed
then 
   if (OS_Oil_Battery == 128) {
      sendCommand(OS_Oil_Battery_State, "ON")
   } else {
       sendCommand(OS_Oil_Battery_State, "OFF")
   }
end

But when Oil_Battery gets set to 128, the rule sends OFF command, i must be missing something obvious, but i cannot fathom it out.

Heres the log for the Oil_Battery state change.

2021-01-15 14:57:45.689 [vent.ItemStateChangedEvent] - OS_Oil_Battery changed from NULL to 128
2021-01-15 14:57:45.695 [ome.event.ItemCommandEvent] - Item 'OS_Oil_Battery_State' received command OFF
2021-01-15 14:57:45.700 [vent.ItemStateChangedEvent] - OS_Oil_Battery_State changed from NULL to OFF

first of all, please use code fences (```) for your code, makes ist more readable.
Then, your if-clause is always false, because, it needs to be:

 if (OS_Oil_Battery.state == 128) {

without the .state the rule won’t fnd the state of the item! :wink:

Thanks, i did see the note about using code tags, but there is no message as to what they are!! tried using the ``` but only seems to be a start tag, how do i close them?

same thing: ```
so, it’s the three at the start and the end of your code, each in a new line. that works.

some while back, there also was an icon above, which did it for your - but it’s gone… don’t know why! :wink:

Thanks, got that now. I was looking for an icon in the header, have seen that on other forums for code tags, will try to remember in future.

1 Like

That works fine now, thanks again

Hi, just another option how you can solve your issue and to avoid a rule.

see here

With profile “hysteresis” you can switch ON/OFF your switch item based on the number battery item.

// Indicates a battery low alarm if battery level drops below 15
Number Battery_Level { channel="serialbutton:button:mybutton:battery-level" }
Switch Low_Battery { channel="serialbutton:button:mybutton:battery-level" [profile="hysteresis", lower=15, inverted=true] }

With “hysteresis” and also transformation “Scale” I was able to remove a huge bunch of my rules.

Thanks Steffen i’ll look into profiles, in my particular case i believe that a value of 128 is a good battery state, and anything else is ‘bad’ i don’t have manufacturer documentation to go on just comments from other people that have used this Oil tank gauge. So i am not sure if a bad value will be lower or higher than 128 (i suspect it will be lower).

In this case, you can also have a look on the transform binding SCALE. That will offer all options you need. And also here, you don’t have to create extra rules. All can be defined within the item definition. :slight_smile: