Passing parameters to a script

Tags: #<Tag:0x00007f617338def8>

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.

Or don’t use netcat and use curl…

Dont think curl works, its not HTTP

https://docs.linn.co.uk/wiki/index.php/Developer:LPEC

Seems its started not working again, the KDS_Command isnt updating when setting the switch to off. If I reboot the system it works again.

It seems theres something wrong with the OH rule engine?? Very odd. Infact, ALL rules stop working.

It’ll depend on which version of netcat you’re using, but try adding a -c, which should close the connection. You’re using the old rule engine, which is running out of threads.

Let me know if you’d like me to convert it Jython… would only take a minute.

1 Like

Will do. Sure have a go at a conversion. I’ll look to see if I can set up jython :+1: thanks Scott

Setup is easy…

from core.rules import rule
from core.triggers import when
from core.actions.Exec import executeCommandLine
# uncomment the next line to use logInfo, like in the DSL
#from core.actions.LogAction import logInfo


@rule("Put KDS into Standby")
@when("Item KDS_Power received command")
def put_kds_into_standby(event)
    new_state = 'Action Preamp/Product 2 SetStandby "{}'.format('0"' if receivedCommand == ON else '1"')
    #logInfo("Linn", new_state)# you can still use this, but you need to use the import above
    put_kds_into_standby.log.info(new_state)

    events.postUpdate("KDS_Command", new_state)
    result = executeCommandLine("/etc/openhab2/scripts/linn.sh@@" + new_state, 5000)
    #logInfo("Linn", "result: {}", result)
    put_kds_into_standby.log.info("result: {}".format(result))

Ah, it requires 2.5 OH, then I’m out.

The addon does, but you can still do a manual install… https://openhab-scripters.github.io/openhab-helper-libraries/Getting%20Started/Installation.html.