[SOLVED] Change status of a switch via serial string

Hello, friends,

how can I change the status of a switch via a serially read string?

I keep getting this error about “OnOffType…”
But no matter what I send (“1”, “0”, “ON”, “OFF”, …) nothing works.

Do you have an idea?

2019-12-29 18:17:48.273 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert ‘On’ to a state type which item ‘Licht_Gaestezimmer_Allgemein’ accepts: [OnOffType, UnDefType].

==> /var/log/openhab2/openhab.log <==

2019-12-29 18:33:08.075 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert ‘1’ to a state type which item ‘Licht_Gaestezimmer_Allgemein’ accepts: [OnOffType, UnDefType].

rule:
rule “Arduino Lesen”
when

Item Arduino received update

then

var String ArduinoUpdate = Arduino.state.toString.trim

var int LichtGaestezimmerAllgemeinStartetBei = ArduinoUpdate.indexOf(“Licht_GaesteZ,”) + “Licht_GaesteZ,”.length
var String LichtGaestezimmerAllgemein = ArduinoUpdate.mid(LichtGaestezimmerAllgemeinStartetBei, ArduinoUpdate.indexOf(’;’)-LichtGaestezimmerAllgemeinStartetBei)

Licht_Gaestezimmer_Allgemein.postUpdate(LichtGaestezimmerAllgemein)
end

Many thanks for your support!
Tobi

if (Licht_Gaestezimmer_Allgemein == "On" or Licht_Gaestezimmer_Allgemein == "1") {
    Licht_Gaestezimmer_Allgemein.postUpdate(ON)
} else if (Licht_Gaestezimmer_Allgemein == "Off" or Licht_Gaestezimmer_Allgemein == "0") {
    Licht_Gaestezimmer_Allgemein.postUpdate(OFF)
} else {   // UNEXPECTED VALUE
    Licht_Gaestezimmer_Allgemein.postUpdate(UNDEF)
}

Hey rossko57,

you mean like this?
rule “Arduino Lesen”
when

Item Arduino received update

then

var String ArduinoUpdate = Arduino.state.toString.trim

var int LichtGaestezimmerAllgemeinStartetBei = ArduinoUpdate.indexOf(“Licht_GaesteZ,”) + “Licht_GaesteZ,”.length
var String LichtGaestezimmerAllgemein = ArduinoUpdate.mid(LichtGaestezimmerAllgemeinStartetBei, ArduinoUpdate.indexOf(’;’)-LichtGaestezimmerAllgemeinStartetBei)

Licht_Gaestezimmer_Allgemein.postUpdate(LichtGaestezimmerAllgemein)
if (Licht_Gaestezimmer_Allgemein == “On” or Licht_Gaestezimmer_Allgemein == “1”) {
Licht_Gaestezimmer_Allgemein.postUpdate(ON)
} else if (Licht_Gaestezimmer_Allgemein == “Off” or Licht_Gaestezimmer_Allgemein == “0”) {
Licht_Gaestezimmer_Allgemein.postUpdate(OFF)
} else { // UNEXPECTED VALUE
Licht_Gaestezimmer_Allgemein.postUpdate(UNDEF)
}
end

Get the following error:

2019-12-29 18:49:43.793 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘ETW.rules’ has errors, therefore ignoring it: [151,42]: no viable alternative at input ‘or’

[151,45]: missing ‘)’ at ‘Licht_Gaestezimmer_Allgemein’

[151,80]: mismatched input ‘)’ expecting ‘end’

Yes, silly typo. Use || instead of or in if() expressions.
These are ideas and examples, not tested code. Debugging is a useful exercise for the user, especially when the error message is explanatory.

You don’t even need the “or” if you know what your incoming data is. I don’t know that, because you seem to have been changing it and I don’t know how you left it.

Hey rossko57,

thank you very much for your successful support!
I had to make some adjustments, but now openhab reads the command cleanly!
Now I just have to see why the switch icon sometimes doesn’t react or only reacts very briefly when switching off.

Should I use autoupdate=“false” or not?
Switch Licht_Gaestezimmer_Allgemein “Allgemein” [“Light”] { autoupdate=“false”, serial="/dev/ttyACM1,ON(<Light_GaesteZ_ON>),OFF(<Light_GaesteZ_ON>)" }" }

Please tick the solution
Thanks

rule "Arduino Lesen"
when
    Item Arduino received update
then
    var String ArduinoUpdate = Arduino.state.toString.trim
    var int LichtGaestezimmerAllgemeinStartetBei = ArduinoUpdate.indexOf("Licht_GaesteZ,") + "Licht_GaesteZ,".length
    var String LichtGaestezimmerAllgemein = ArduinoUpdate.mid(LichtGaestezimmerAllgemeinStartetBei, ArduinoUpdate.indexOf(";")-LichtGaestezimmerAllgemeinStartetBei)

    if (LichtGaestezimmerAllgemein == "On" || LichtGaestezimmerAllgemein == "1") {
        Licht_Gaestezimmer_Allgemein.postUpdate(ON)
    } else if (LichtGaestezimmerAllgemein == "Off" || LichtGaestezimmerAllgemein == "0") {
        Licht_Gaestezimmer_Allgemein.postUpdate(OFF)
    } else { // UNEXPECTED VALUE
        Licht_Gaestezimmer_Allgemein.postUpdate(UNDEF)
    }
end

I don’t know what you want, what would you like to happen?
What autoupdate is for -it listens for commands to an Item (say from clicking on UI) and guesses what the likely outcome is on the Items state.
It can do that quickly.
Otherwise, you’d have to wait for a real status update back from your device. Maybe that takes a while, which would give a poor UI experience.
Or, maybe you want your UI to show the truth and not guesses.
Your choice.

Hey rossko57,

everything finally works!

I the “rule” had changed the status of the “switch item” and thus triggered another “switch rule”, which told my Arduino a switch command, but checked the Arduino did not know the physical state of the light relay and always switched :smiley: sorry!