Strugging with EXEC binding

(Andrew Huxtable) #1

Hi all, I’ve been battling this for ages and I am getting nowhere.

I have written a basic shell script that returns 0 or 1 as a presence detection based on SNMP from my wireless access points.

I have added the exec1 addon.

I have the following in items:
Switch Presence_Andrew { exec="<[/etc/openhab2/scripts/presense_detect.sh ‘30 07 4D 3A A0 63’:10000:]"}

The shell script also logs to /var/log/openhab2 and it is working as expected.

i have logged on as the openhab user and confirmed the script runs fine.

I set up a test ‘execCommand’ and the log properly shows 0 or 1 but for some reason the item itself will not get the update - There is nothing in the log:tail that even shows it working but the script itself is firing every 10 seconds as expected, just the ‘item’ does not get updated.

Can someone shed some light?

0 Likes

(Rich Koshak) #2

See

My first thing to check would be to use executeCommandLine in a rule to see what is being returned when the exec binding tries to run it.

Are you aware of the SNMP binding?

0 Likes

(Andrew Huxtable) #3

Thanks for taking your time to reply Rich but I have already tried everything in that link. I am not one of these people that log a question in a forum as soon as I hit a glitch, more as a last resort. I feel I learn better that way.

As above, I have tested the execCommand method and it works, returning values as expected in the logs.

The system is running as openhab user
openhab user has a shell set in /etc/passwd
openhab user has permissions to the file etc
I have set a password for openhab user so I can log in as it.

** Logging in as openhab user I can run the script and it works fine
** My script writes to a log file when the script is run and it is running as expected every 10 seconds by openhab, just the response from the command is not being applied to the switch item.

I am aware that SNMP is natively supported, I already use it for other things.
I am specifically not using it here as:

  • I have 2 access points in my house and I need to enumerate both, this simplifies(?) it

  • The SNMP query does not directly return all MAC addresses for attached devices from one OID but it has an array of ‘slots’ for each MAC attached, therefore I have to use snmp walk, grepped to strip out just the MAC address I am looking for:

    #!/bin/bash
    macaddr="${1%"}“
    macaddr=”${macaddr#"}"

    rv1="snmpwalk -Oqv -v 1 -c public 192.168.0.5 1.3.6.1.4.1.9.9.272.1.1.1.8.1.2 | grep -c ‘$macaddr’"
    rv2="snmpwalk -Oqv -v 1 -c public 192.168.0.6 1.3.6.1.4.1.9.9.272.1.1.1.8.1.2 | grep -c ‘$macaddr’"
    rv1=eval "$rv1"
    rv2=eval "$rv2"

    if [ “$rv1” == 1 ] || [ “$rv2” == 1 ]; then
    echo "1"
    echo “$macaddr Found” >> /var/log/openhab2/presensedetect
    else
    echo "0"
    echo “$macaddr not Found” >> /var/log/openhab2/presensedetect
    fi

I have also tried ‘exit 0’ and ‘exit 1’ thinking that perhaps it does not accept the console text as the response.

I did have issues at the start which showed up with testing:

  • Initially I was trying to log to /var/log/presencedetect - Access denied, changed to /var/log/openhab2/presencedetect
  • Openhab adds double quotes to the command passed to the shell script so I had to add code to remove them in the script itself otherwise grep would not find a match.
0 Likes

(Rich Koshak) #4

Did you try replacing the spaces with “@@”?

0 Likes

(Andrew Huxtable) #5

Yes, then the only difference is, it surrounds it in single quotes but still doesn’t work.

0 Likes

(Rich Koshak) #6

At this point all I can say is to either stick with what works and use a rule with executCommandLine and sendCommand or postUpdate the result to the Item, or try using the exec 2.x version of the binding and see if you have better luck.

0 Likes

(steve1) #7

@Andrew_Huxtable. Here a few suggestions but I realize you may have already tried these.

I’m not sure that a “0” or “1” will be processed like you expect for a Switch item. It only accepts an OnOffType value (and UnDef). Normally that would require “ON” and “OFF” but maybe there’s something in other code that does further translation.

Try temporarily changing your item to a StringItem and see if the value is updated correctly. If so, you need to write a mapping file to map “0” and “1” to “OFF” and “ON” or modify the script to return those values.

I recommend increasing the logging for the exec1 binding to trace level if you haven’t already done so.

1 Like

(Andrew Huxtable) #8

Thanks Steve, I can’t try it right now but I will give it a shot. I just assumed that it should take a 0 or a 1 but it’s entirely possible you’re right.

I tried the exec2 binding but it handles the command line differently (it doesn’t like the quotes around the parameter with spaces). I ran out of time to properly test it though.

0 Likes

(Andrew Huxtable) #9

That was it @steve1 !!

Changed the script to return ON and OFF instead of 1 and 0 and now it works fine!!

Thanks so much.

0 Likes