SendCommand from Script (echo"ON") to Switch Item

Hello,

I use OH3 and wanted to Ping a Bluetooth Device. Sometimes i want to Ping every 40s and sometimes every 10s.


var results = executeCommandLine(Duration.ofSeconds(7), "/etc/openhab/scripts/bt_ping_nils.sh")
                
                if (results == ON)
                {
                //do something
                }
Nils_Handy_BT_Switch.sendCommand(results)
logInfo("BT Suche Aktiv", "Ergebnis von bt_ping_nils.sh: "+results)

At the same time a Thing is scanning every 40s for the Bluetooth device.
That is working. But sometimes I want to Scan every 10s or less.

Thing exec:command:nils_bt "Nils Handy BT" [command="sh /etc/openhab/scripts/BT.sh 1", interval=40, timeout=7, transform="REGEX((.*?))"]

Switch Nils_Handy_BT_Switch "Nils Handy BT" (RRD4J, gAnwesend_Nils) {channel="exec:command:nils_bt:output"}

BT Ping Script

if /usr/bin/sudo /usr/bin/l2ping -c 1 "MY MAC ADRESS" >/dev/null 2>&1

   then
#set echo on
      echo "ON"
   else
#set echo off
      echo "OFF"
fi

That is my error message:

[WARN ] [b.core.model.script.actions.BusEvent] - Cannot convert 'ON

' to a command type which item 'Nils_Handy_BT_Switch' accepts: [OnOffType, RefreshType].

[INFO ] [hab.core.model.script.BT Suche Aktiv] - Ergebnis von bt_ping_nils.sh: ON
[INFO ] [hab.core.model.script.BT Suche Aktiv] - Ergebnis von bt_ping_nils.sh: ON

Edit:
i use the Script BT.sh 1 and bt_ping_nils.sh but both does the same.

As far as I read it result is of type string. That means

Nils_Handy_BT_Switch.sendCommand(results)

sendCommand uses “ON” resp. “OFF” as parameter while it has to be ON resp. OFF.
To solve that you can use and if else construct.

As @Wolfgang_S says, results will always be a string.

So that will always be false. results == "ON" instead.

But this should still work with “ON”, the command parser will accept strings and try to parse to acceptable type.

I think you have extra invisible characters in your results - look closely at the error message, I think you have "ON[newline]"

logInfo("BT Suche Aktiv", "characters: " + results.length)

I think you are right. My Sting length is 3 for “ON” und 4 for “OFF”. Maybe a Newline command.

I couldn’t fix something in the Script. Is there a way to live with this. And how could I do an IF comparison with this?

Strip the last character out in your rule before doing anything else, it’s just string manipulation.

According to the manual page of echo:

-n do not output the trailing newline

So does it work to use
echo -n ON
and
echo -n OFF
instead of
echo ON
resp.
echo OFF