[SOLVED] States of items with CASE and IF

Hello,

i have a Arduino that send commands via serial port to OH.
Like this

2019-01-17 11:33:03.982 [vent.ItemStateChangedEvent] - Arduino changed from 44100608unit1off to 44100608unit1on

I would like to catch them und use them in rules with CASE.

I wrote the rule, but ir do not work

rule "Arduino"
when
  Item Arduino changed 
then
  switch(Arduino.state.toString)  {
      case "44100608unit1on": {
            logInfo("arduino.rules", "Arduino State changed: "+ Arduino.state + "") 
      }
      case "44100608unit1off": {
  
            logInfo("arduino.rules", "Arduino State changed: "+ Arduino.state + "")
      }
  }  
   
end

It do not goes inside case. I thnig problem is here

switch(Arduino.state.toString)  

What have i made wrong?

Thanks a lot!

See:

https://www.eclipse.org/xtend/documentation/203_xtend_expressions.html#switch-expression

rule "Arduino"
when
    Item Arduino changed 
then
    switch Arduino.state.toString  {
        case "44100608unit1on": {
            logInfo("arduino.rules", "Arduino State changed: "+ Arduino.state + "") 
        }
        case "44100608unit1off": {
            logInfo("arduino.rules", "Arduino State changed: "+ Arduino.state + "")
        }
    }  
end
1 Like

Thanks for quick answer!
But do not work(((
Maybe it is not a string?

Maybe you do not receive what you think you receive. Add a log statement before the switch to log Arduino.state.toString.

1 Like

It works before switch

Are you sure that there are not leading/trailing spaces? An easy way to check is to put the printed state between brackets:

logInfo("arduino.rules", "Arduino State changed: ["+ Arduino.state + "]") 
2 Likes
rule "Arduino"
when
    Item Arduino changed 
then
    switch Arduino.state.toString  {
        case "44100608unit1on": {
            logInfo("arduino.rules", "Arduino State changed: "+ Arduino.state + "") 
        }
        case "44100608unit1off": {
            logInfo("arduino.rules", "Arduino State changed: "+ Arduino.state + "")
        }
        default: {
            logInfo("arduino.rules", "Unexpected: "+ Arduino.state.toString + "")
        }
    }  
end
1 Like

Yes now i get Unexpected: 44100608unit1off
So what is wrong?

Does the Arduino send a return character /n?

1 Like

See @marcel_erkel suggestion

1 Like

Yes! Und how do i catch it?

this way do not work(((

 case "44100608unit1on/n": {

This one Works:

case "44100608unit1on\r\n"

Thanks to all!

1 Like