Passing parameters to a script

The log wont print the contents of your val scott



rule "Put KDS into Standby"
when
    Item KDS_Power received command
then
    val new_state = 'Action Preamp/Product 2 SetStandby "' + if (KDS_Power.state == ON) '0"' else '1"'
        logInfo("Linn", "State=" + new_state)
    KDS_Command.postUpdate(new_state)
end

This works fine for me…

rule "Put KDS into Standby"
when
    System started
then
    val new_state = 'Action Preamp/Product 2 SetStandby "' + if (Virtual_Switch_1.state == ON) '0"' else '1"'
    logWarn("Rules", new_state)

    Virtual_String_1.postUpdate(new_state)
end
2020-05-14 21:09:47.423 [WARN ] [org.eclipse.smarthome.model.script.Rules] - Action Preamp/Product 2 SetStandby "0"
openhab> smarthome:status Virtual_String_1                                                                                                                                                                                                    
Action Preamp/Product 2 SetStandby "0"

I just rebooted openhab and now its started to work but still an error

Found this: [SOLVED] String update through rules

So I rebooted

13:34:01.442 [WARN ] [clipse.smarthome.io.net.exec.ExecUtil] - Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "sudo /etc/openhab2/scripts/linn.sh" (in directory "."): error=2, No such file or directory)

The file is there as being executable.

13:34:01.136 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'KDS_Power' received command ON
13:34:01.150 [INFO ] [smarthome.event.ItemStateChangedEvent] - KDS_Power changed from NULL to ON
13:34:01.391 [INFO ] [smarthome.event.ItemStateChangedEvent] - KDS_Command changed from NULL to Action Preamp/Product 2 SetStandby "0"

kris@ihp:/etc/openhab2/scripts$ ls -la
total 64
drwxr-xr-x  4 openhab openhab  4096 May 15 13:23 .
drwxr-xr-x 14 openhab openhab  4096 Mar 19 14:05 ..
-rw-r--r--  1 openhab openhab   870 Apr 25  2019 backup.sh
-rw-r--r--  1 openhab openhab   844 Apr 25  2019 bomradar.sh
drwxr-xr-x  2 openhab openhab  4096 Apr 25  2019 digestAuthenWrapper
-rw-r--r--  1 openhab openhab   197 Apr 25  2019 kodi_rescan.sh
-rwxr-xr-x  1 root    root      116 May 15 11:37 linn.sh

Permissions. Make sure the openhab account can access /etc/openhab2/scripts/linn.sh. What do you see with…

ls -alh /etc/openhab2/scripts

OK… you did that :slightly_smiling_face:. The openhab account can read it.

doh!!! root owns it, not openhab

kris@ihp:/etc/openhab2/rules$ ls -alh /etc/openhab2/scripts
total 64K
drwxr-xr-x  4 openhab openhab 4.0K May 15 13:39 .
drwxr-xr-x 14 openhab openhab 4.0K Mar 19 14:05 ..
-rw-r--r--  1 openhab openhab  870 Apr 25  2019 backup.sh
-rw-r--r--  1 openhab openhab  844 Apr 25  2019 bomradar.sh
drwxr-xr-x  2 openhab openhab 4.0K Apr 25  2019 digestAuthenWrapper
-rw-r--r--  1 openhab openhab  197 Apr 25  2019 kodi_rescan.sh
-rwxr-xr-x  1 root    root     117 May 15 13:39 linn.sh

Right, but everyone has read. Can you just run the script from the command line using the openhab account?

Yep! i can

kris@ihp:/etc/openhab2/scripts$ sudo -u openhab ./linn.sh
ALIVE Preamp 4c494e4e-0026-0f22-3898-012366860133
ALIVE Ds 4c494e4e-0026-0f22-3898-01236686013f
ALIVE MediaRenderer 4c494e4e-0026-0f22-3898-012366860171

I have no idea what that means :slightly_smiling_face:. What’s in the script? Maybe add some logging to it? Or just comment out everything but a simple log to make sure that the rule runs OK.

I think its the executeCommandLine, because if I remove the @@ i get another error, totally different


13:55:06.701 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'KDS_Power' received command ON
13:55:06.999 [INFO ] [g.eclipse.smarthome.model.script.linn] - results: sudo: /etc/openhab2/scripts/linn.shAction: command not found

The script is:

#!/bin/sh

IP=192.168.0.64
PORT=23

echo ${1} | nc ${IP} ${PORT}

Let’s see… try this for the script… I will too…

#!/bin/sh

#IP=192.168.0.64
#PORT=23

echo ${1}# | nc ${IP} ${PORT}
1 Like

Same issue for me, how’d you go? Basically, the rule is meant to execute the script and send the string contents as the command for the script

It will be used to switch on/off a UPNP device using the ‘NC’ program.

Had to get some ice cream… gimme a sec… :smile:

1 Like

Try this for the script…

#!/bin/sh

echo ${1}

This will help show that the rule is working and openhab has access to the script.

1 Like

Same issue Scott with your script


14:47:11.461 [WARN ] [clipse.smarthome.io.net.exec.ExecUtil] - Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "sudo -u openhab /etc/openhab2/scripts/linn.sh" (in directory "."): error=2, No such file or directory)
14:47:11.462 [INFO ] [g.eclipse.smarthome.model.script.linn] - results:

Try this for the rule…

rule "Put KDS into Standby"
when
    Item KDS_Power received command
then
    val new_state = 'Action Preamp/Product 2 SetStandby "' + if (receivedCommand == ON) '0"' else '1"'
    logInfo("Linn", new_state)

    KDS_Command.postUpdate(new_state)
    val result = executeCommandLine("/etc/openhab2/scripts/linn.sh@@" + new_state, 5000)
    logInfo("Linn", "result: {}", result)
end
1 Like

We have a winner!!

That works so far Scott!


15:09:23.619 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'KDS_Power' received command OFF
15:09:23.620 [INFO ] [g.eclipse.smarthome.model.script.Linn] - Action Preamp/Product 2 SetStandby "1"
15:09:23.621 [INFO ] [smarthome.event.ItemStateChangedEvent] - KDS_Power changed from ON to OFF
15:09:23.622 [INFO ] [smarthome.event.ItemStateChangedEvent] - KDS_Command changed from Action Preamp/Product 2 SetStandby "0" to Action Preamp/Product 2 SetStandby "1"
15:09:23.673 [INFO ] [g.eclipse.smarthome.model.script.Linn] - result: Action Preamp/Product 2 SetStandby "1"

OK! So, how are things if you put your script back the way it was?

I’ve put the script back to this and it works :smiley: this is awesome! So why the change, what is it about the previous rule it didnt like?

Only issue is, it seems to only work once. Ie turns on/off, then wont go back on again. This might be because the unit can only accept 4 connections, if they are left open, it wont close them. So i think i need an ability for the script to disconnect (Control Z after it executes the command)

#!/bin/sh

IP=192.168.0.64
PORT=23

echo ${1} | nc ${IP} ${PORT}

There were lots of changes from your original rule, but the last change was to remove the sudo. The openhab account had permissions to read and execute the script, so it wasn’t needed.

Before the DLNA binding, and even still now for some proprietary UPnP commands for my speakers, I use curl (and Jython)…

start_result = Exec.executeCommandLine("/bin/sh@@-c@@/usr/bin/curl -s --connect-timeout 10 --max-time 20 \"http://{}:{}\" -H \"CONTENT-TYPE: text/xml; charset=\\\"utf-8\\\"\" -H \"CONNECTION: close\" -H \"SOAPACTION: \\\"urn:schemas-sony-com:service:Party:1#X_Start\\\"\" -d \"<?xml version=\\\"1.0\\\" encoding=\\\"utf-8\\\"?><s:Envelope s:encodingStyle=\\\"http://schemas.xmlsoap.org/soap/encoding/\\\" xmlns:s=\\\"http://schemas.xmlsoap.org/soap/envelope/\\\"><s:Body><u:X_Start xmlns:u=\\\"urn:schemas-sony-com:service:Party:1\\\"><PartyMode>PARTY</PartyMode><ListenerList>{}</ListenerList></u:X_Start></s:Body></s:Envelope>\"".format(items[self.host + "_IP"], self.host_port_and_control, self.listener_list), 25000)

Thanks, i think I just need a control z or an exit in my script. If i restart the device, it accepts connections again.