How to use String item in IF statement

Hello,

I’m running following configuration as bellow and can’t solve Rule issue.

I’ve an item:
String Wilgotnosc "Wilgotnosc" <humidity> {channel="mqtt:topic:sprinkler:GPIO16"}
It gets values 0 or 1 in log file.

The second item used is switch:
Switch SprinklerSystemSchedule "Zaplanowane podlewanie" <calendar>

rule "Sprinkler System Schedule"

when
  Time cron "0 51 20 1/1 * ? *"
then
  if(SprinklerSystemSchedule.state == ON && Wilgotnosc == 0)
  {
      sendBroadcastNotification("SYSTEM NAWADNIANIA: Zaplanowane podlewanie AKTYWNE")
  }
end

I want the rule to work only when both IF conditions for items are met. I can’t get this to work when I add ‘Wilgotnosc’ item. Seems OH doesn’t recognize the value/state of string item

Thanks for any advice

Tomasz

  • Platform information:
    • Hardware: RPi3b+
    • OS: Raspbian Buster
    • openHAB version: 2.5.2

I suspect that the issue is that the character “0” in the string item is not equal to the numeric zero in the IF statement. You could try changing the String item to a number, or change the numeric zero in the IF statement to a character zero (“0”).

But I have to wonder why Wilgotnosc isn’t a Switch item, and the IF statement would specify “== ON”.

you forgot the “state” for Wilgotnosc in your “if” statement:

Wilgotnosc == 0

should probably be

Wilgotnosc.state == 0

or may be as it is a string

Wilgotnosc.state == "0"

Thanks

This is the solution - I was missing the quotation marks apparently

Wilgotnosc.state == "0"

Tomasz

It is the sensor that provides two states only. Not possible to force change its value that’s why I used the String.

Also it seems I was missing the “” as per other reply

thanks for your input

Tomasz

If you wanted you could use a Switch Channel instead of a String Channel, and link that to a Switch Item. Then you just test for ON or OFF in your rules.

You will need to ensure that the Channel understands 1/0 is ON/OFF, which if you used a Things file requires just on=1, off=0 in your Channel configuration.