Need help for a simple IF ... ELSE

Good Evening everyone
After upgraded from OH2.4 to 3.0 I tried to migrated it but it crashed so I need to start from scratch my whole system based on the idea of my old OH2. It is hard but it is working.
The system data are:
Platform = Raspberry Pi Model B Plus Rev 1.2
Description: Raspbian GNU/Linux 10 (buster)
Release: 10
Kernel : Linux RspiOpenHAB 5.10.17+ #1403 Mon Feb 22 11:26:13 GMT 2021 armv6l GNU/Linux
openhabian version: 3.0.1 (Build)
openjdk version: “11.0.10” 2021-01-19 LTS
OpenJDK Runtime: Environment Zulu11.45+27-CA (build 11.0.10+9-LTS)
OpenJDK Client: VM Zulu11.45+27-CA (build 11.0.10+9-LTS, mixed mode)

The thing is I adopt the old rules that worked on OH2 but I’ve a problem with this one that “double-check” the movile phones’ presence executing
a script based on the command “ip neigh show” after a lapse time that the rule was fired. The script return a simple ON/OFF string and there is a if / else to decide what to do.
But it doesn’t work, all the time ignores the “ON” value.
This is the rule, and Script and the logs:

//************* Desactivacion GSw Presencia ***********
// GSw Presencia changed from ON to OFF >> Nobody in the house => change Switch Post_Presencia to OFF 
rule "Ausencia"
when

        Item gPresencia changed from ON to OFF
then

        var PresenciaExec = "OFF"

        // Made a timer for waiting 2 min and then check there is no movile in the network 

        // executing the BASH Script Phonedet.sh that use the command "ip neigh show"

        logInfo("CheckAusencia.rule", ">-- It seems that there is no active Mobile. It will be rechecked in a while.")

        createTimer(now.plusMinutes(0).plusSeconds(20), [|

                logInfo("CheckAusencia.rule", ">-- Value PrensenciaExec before evaluating Phonedet.sh: " + PresenciaExec)

                PresenciaExec = executeCommandLine(Duration.ofSeconds(10), "/etc/openhab/scripts/Phonedet.sh")

                logInfo("CheckAusencia.rule", ">-- Value PrensenciaExec after evaluating Phonedet.sh: " + PresenciaExec)

                if (PresenciaExec == "ON" ) {

                        Post_Presencia.postUpdate(ON)

                        logInfo("CheckAusencia.rule", ">-- Maybe was a false desconexion")

                        logInfo("CheckAusencia.rule", ">-- Switch Post_Presencia ON")

                } 

                else {

                        Post_Presencia.postUpdate(OFF)

                        logInfo("CheckAusencia.rule", ">-- Confimed nobody at the House")

                        logInfo("CheckAusencia.rule", ">-- Switch Post_Presencia OFF")

                }

        ])
end

#!/bin/bash

status=OFF

statusprev=OFF

MOVILES='192.168.1.31 192.168.1.36 192.168.1.34 192.168.1.35 192.168.1.37 192.168.1.38'

#MOVILES='192.168.1.31'

for i in $MOVILES

do

    statusprev=`ip neigh show | awk '{print $1,$6}' | grep $i | sed -e "s/$i //" -e "s/REACHABLE\|STALE\|DELAY/ON/g"`

    if [ "$statusprev" = "ON" ]

    then

            status=ON

    fi

done

echo $status
==> openhab.log <==
2021-04-13 20:12:10.520 [INFO ] [core.model.script.CheckAusencia.rule] - >-- It seems that there is no active Mobile. It will be rechecked in a while.
2021-04-13 20:12:30.709 [INFO ] [core.model.script.CheckAusencia.rule] - >-- Value PrensenciaExec before evaluating Phonedet.sh: OFF
2021-04-13 20:12:31.572 [INFO ] [core.model.script.CheckAusencia.rule] - >-- Value PrensenciaExec after evaluating Phonedet.sh: ON

2021-04-13 20:12:31.608 [INFO ] [core.model.script.CheckAusencia.rule] - >-- Confimed nobody at the House
2021-04-13 20:12:31.622 [INFO ] [core.model.script.CheckAusencia.rule] - >-- Switch Post_Presencia OFF

This is almost certainly a whitespace issue. Your PrensenciaExec value looks like it returns “ON” when you have it rendered by the loginfo function, but that is probably ignoring some whitespace character and the script is actually returning “ON\n” or something similar. Try to get rid of the whitespace using the string trim() method.

if (PresenciaExec.trim() == "ON" ) {
2 Likes

You can actually see the newline in the log excerpt, if copy/paste is accurate.

1 Like

You are right, it works:

==> openhab.log <==
2021-04-14 20:13:20.556 [INFO ] [core.model.script.CheckAusencia.rule] - >-- It seems that there is no active Mobile. It will be rechecked in a while.

2021-04-14 20:13:40.748 [INFO ] [core.model.script.CheckAusencia.rule] - >-- Value PrensenciaExec before evaluating Phonedet.sh: OFF
2021-04-14 20:13:42.608 [INFO ] [core.model.script.CheckAusencia.rule] - >-- Value PrensenciaExec after evaluating Phonedet.sh: ON

2021-04-14 20:13:42.698 [INFO ] [core.model.script.CheckAusencia.rule] - >-- Maybe was a false desconexion
2021-04-14 20:13:42.725 [INFO ] [core.model.script.CheckAusencia.rule] - >-- Switch Post_Presencia ON

Thank you very much