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


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/")
                if (results == ON)
                //do something
logInfo("BT Suche Aktiv", "Ergebnis von "+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/ 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

#set echo on
      echo "ON"
#set echo off
      echo "OFF"

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 ON
[INFO ] [hab.core.model.script.BT Suche Aktiv] - Ergebnis von ON

i use the Script 1 and but both does the same.

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


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
echo -n OFF
instead of
echo ON
echo OFF