Return value for executeCommandLine

Hello,

I want to retrieve a value from a bash script. I setup a simple rule below to get the value 100. However OH1 gives an error

given new state is NULL, couldn’t post update for ‘room_temp’

Any idea how to correct this?

rule "Display temperature"
when
Item room_temp received update or
Item room_temp received changed or
System started

then
var String tem_room=executeCommandLine(“echo 100”)
postUpdate(room_temp,tem_room)
end

please add

logInfo("D.temp","value of tem_room is:{}",tem_room)

and see the output in openhab.log (or, if manually started openHAB even in console)

I get this

2016-03-13 18:29:49.935 [INFO ] [g.openhab.io.net.exec.ExecUtil] - executed commandLine 'echo 100’
2016-03-13 18:29:49.943 [INFO ] [rg.openhab.model.script.D.temp] - value of tem_room is:{}
2016-03-13 18:29:49.949 [WARN ] [.c.i.events.EventPublisherImpl] - given new state is NULL, couldn’t post update for ‘room_temp’

with the following rule

rule "Display temperature"
when
Item room_temp received update or
Item room_temp received changed or
System started
then
var String tem_room=executeCommandLine(“echo 100”)
logInfo(“D.temp”,“value of tem_room is:{}”,tem_room)
postUpdate(room_temp,tem_room)
end

Argh… typo… should be : {} (with a space between : and {)

You need to add a timeout to the executeCommandLine to get the output from the script back.

var String tem_room=executeCommandLine("echo 100", 5000)

That will wait up to five seconds for the script to complete executing.

The version of executeCommandLine without the timeout does not return anything.

4 Likes

Hey guys,
I’m trying to solve similar issue.
I have rule where I want to check value from js.
Rule:

 val result = executeCommandLine("sudo node /home/openhabian/scripts/oilWaterCheck.js", 5000)
 if (result == false){
    logInfo("Diffuser", "Low water")
 }

But I didn’t get any result from that script.
JS:

const TuyAPI = require('tuyapi');

const device = new TuyAPI({
  id: 'xxxx',
  key: 'xxxx',
  ip: 'xxxx'});

device.get({dps: 127}).then(status => {
 console.log('Current status:', status);
 device.set({set: !status}).then(result => {
   console.log('Result of setting status:', result);
  });
	return "status";
});

What I’m doing wrong?
Thanks

Well, part of what is wrong is result will never equal the boolean constant false. It might equal the String “false”. executeCommandLine always returns a String.

If that isn’t it, I suggest logging what the script is actually returning. There might be an error message in there or something.

Thanks for answer. I tried also string but with same result. At the end, I solved it with REST and changing item inside js.