I struggle quite a while looking around everywhere, but I did not find the solution for my (simple?) problem.
I would like to run a script on a regular basis to update the status of my sony TV.
Running the script in a shell works well.
but not in my rule, which looks like this:
rule “Ist TV an?”
when
Time cron “0 0/3 * * * ?” // every 3 minutes
then
executeCommandLine( “/etc/openhab/configurations/scripts/sonyTVcheck.sh” )
end
I read a lot about substituting spaces with @@, but this is obviously not the problem here.
By the way: Would Habmin find the right syntax for this kind of work?
Would this solve all my syntax issues (there are a lot more, I guess).
Thanks.
Actually I runs it as pi.
I have placed the script (using curl, if this is relevant) in the openhab home folder:
/usr/share/openhab/configurations/scripts (yes, it’s an apt-get installation),
Furthermore I changed the permissions to 755 and are able to run it as pi (no duso).
So, if openhab runs as openhab, this user should be able to run it as well.
And obviously it’s executed:
2015-10-18 05:03:00.014 [INFO ] [g.openhab.io.net.exec.ExecUtil] - executed commandLine ‘/etc/openhab/configurations/scripts/sonyTVcheck.sh’
Hopefully @p912s 's advice will help. But just for clarity:
That just means that the ExecUtil didn’t run into any problems. Try the following so you can see what the script actually outputs:
var results = executeCommandLint("/etc/openhab/configurations/scripts/sonyTVcheck.sh", 5000)
logInfo("SonyExec", results)
The new argument is a timeout. Without that it executes the command line off in another thread and you never see any of the outputs from the script. This may reveal an error or give other clues as to why the script isn’t running.
In the shell it returns “on” or “standby” depending on my TV’s state.
The rule looks like this (I changed it after I created a different functional rule:
rule “request Sony TV status”
when
Time cron “0 0/3 * * * ?” // every 3 minutes -> works as expected !!!
then
var result_TV = executeCommandLine("/etc/openhab/configurations/scripts/readTVstate.sh", 5000)
logInfo(“BLABLA sony.rules - TV Status”, result_TV)
if(result_TV == “on”) {
postUpdate(Sonne_TV, ON) }
else {
postUpdate(Sonne_TV, OFF) }
end
The returned value (e.g. “on” is not shown in the log (logInfo …), so the issue must be somewhere where the result is handed over to the openhab rule.
I thought about that but found out that the response from the script in the shell came within 2-3 seconds.
However, I tried a timeout of 20000 (20 seconds), but it didn’t help.
I have the same problem with another script using the cec-client for starting up or shutting down my TV. It looks like this:
#!/bin/bash
echo ‘on 0’ | cec-client -s >> /tmp/sonyTVon.log
Very simple!
It works in the shell and starts my Sony TV.
When I do an exec like this:
Switch Sonne_TV_2 “TV script” { exec=">[ON:/etc/openhab/configurations/scripts/sonyTVon.sh]" }
It seems to work as well (openhab.log):
2015-10-23 13:23:51.137 [INFO ] [.o.b.exec.internal.ExecBinding] - executed commandLine ‘/etc/openhab/configurations/scripts/sonyTVon.sh’
But when I check the logfile created by the script above (/tmp/sonyTVon.log), there is obviously a problem:
failed to open vchiq instance
I guess this is the root cause also for the other problems.
Did not find a proper solution though…
Adding openhab user to the video group did not help
We have eliminated user permissions as a problem (I think).
So there must be something else different between the pi user’s environment and the openhab user’s environment. Are there any differences between pi and openhab’s environment variables, in particular the PATH?
pi’s PATH is:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
openhab’s PATH:
/sbin:/usr/sbin:/bin:/usr/bin
However, I played a little this morning and now it works.
I actually don’t know and just can imagine that adding openhab to the video group required a restart of openhab!?
Anyway, here are the functional rule:
rule “Sony TV State”
when
Time cron “0 0/2 * * * ?” // every 2 minutes -> works as expected !!!
then
var result_TV = executeCommandLine("/etc/openhab/configurations/scripts/readTVstate.sh", 5000)
logInfo(“sony - BLABLA TV Status”, result_TV)
if(result_TV == “on”) {
postUpdate(Sonne_TV, ON) }
else {
postUpdate(Sonne_TV, OFF) }
end
Problem solved (my second issue switching the TV) !!!
Working version:
Switch Sonne_TV “TV per script” { exec=">[ON:/etc/openhab/configurations/scripts/sonyTVon.sh] >[OFF:/etc/openhab/configurations/scripts/sonyTVoff.sh]" }
I actually don’t know why - it might have been related to the acces rights. That’s why I used a “sudo” in the exec command above.
Additionally I added the user openhab (standard openhab user) to the video group.
The according script I run (see exec above, located in the script folder of openhab) looks like this:
#!/bin/bash
echo ‘on 0’ | cec-client -s