Executecommand(echo

All i am looking to do is append the string i built to a file. I’ve tried escaping this every which way with numerous quotes, double quotes, @@s etc…I can’t figure it out.
Any help greatly appreciated.

Platform information: OH 2.5.11 Ubuntu

postUpdate(FrontDoorLogString,
ZWaveNode_DSB29DoorWindowSensorGen2FrontDoor_BinarySensor.state + " - " + now.getMonthOfYear + “/” + now.getDayOfMonth + " | " + now.getHourOfDay + “:” + now.getMinuteOfHour)

THIS IS THE PART I CAN’T GET WORKING. None of these work.

executeCommandLine(“echo@@>>@@” FrontDoorLogString.state “@@/var/log/openhab2/FrontDoor.csv”)

executeCommandLine("echo >> " FrontDoorLogString.state “/var/log/openhab2/FrontDoor.csv”)

executeCommandLine("echo >> " + FrontDoorLogString.state + “/var/log/openhab2/FrontDoor.csv”)

Thanks

Why would you like to use >> ?
>> has special functionality in shells. executeCommandLine does not offer full shell functionality.
Try it without these characters. I assume you would like to append some stuff to the end of the file.
Create a batch file that uses FrontDoorLogString.state as an argument and then do the redirection to the end of the file in the batch file.

I use >> because i found numerous other posts for executecommandline on this forum where >> was suggested for cat and echo…that’s all.

Interesting, yes I’ll look up how to pass along an argument and test that instead i suppose.

Ok, anyone tell me why this fails: executeCommandLine(sudo "/log/var/openhab2/FrontDoor.sh " + FrontDoorLogString.state)

Still not understanding how to properly construct a command to get to write my item state to a file and append over and over

2023-02-21 18:15:49.365 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘Test.rules’ has errors, therefore ignoring it: [13,25]: missing ‘)’ at ‘"/log/var/openhab2/FrontDoor.sh "’

[13,85]: mismatched input ‘)’ expecting ‘end’

This also does not work:

executeCommandLine("sudo /log/var/openhab2/FrontDoor.sh " + FrontDoorLogString.state)

From the OH2 documentation:

Note

Simple arguments that contain no spaces can be separated with spaces, for example executeCommandLine("touch file.txt"). When one or more arguments contain spaces, use @@ instead of a space as the argument separator. For example the bash command touch -t 01010000 "some file with space.txt" will have to be written as touch@@-t@@01010000@@some file with space.txt.

Your statement has two spaces. Replace them with @@.

"sudo@@/log/var/openhab2/FrontDoor.sh@@"

There may be other issues, but that’s what jumps out to me.

1 Like

Based on previous information sudo isnt reqzired and just will make it more complicated.

Have you tried it this way?
I use sudo always at the .sh files not in the script but you have to give the Openhab user access to sudo right’s.

example with JavaScript:
actions.Exec.executeCommandLine(“/etc/openhab/scripts/ptz.sh”, “Cam5”, “GotoPreset”, “1”)

Thanks RP; unfortunately still an issue:

2023-02-22 07:07:27.275 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘Test.rules’ has errors, therefore ignoring it: [25,61]: no viable alternative at input ''

[25,62]: missing ‘)’ at ‘“”’

[25,95]: mismatched input ‘)’ expecting ‘end’

User does have access; i can run it from a command line; just can’t get it constructed in openhab 2.11 to get it to either load or run; depending on how I try to escape out of all the quotes.
problem is that im trying to send it an item state…to do that i need to escape out but still surround in in quotes…which i can’t figure out the correct syntax

the problem is; i’m trying to pass an item state to it; and i can’t figure out how to escape the double quotes and or insert them into the command properly

Perhaps try constructing your string in a variable, and then use that in your command.

I know what you mean. I had the same problem some time ago. But why don’t you use a workaround.
A door state has two states or three when you can lock it up.
You could use it this way:
if(door.state == open){
executecommandline(“open”)}

1 Like

True; good point…But i also want to capture the rest with the date and time etc…so the problem would still persist. If i took the item station out; I’d still need to construct the date/time string
I mean maybe write two scripts then; my guess is a shell script could write out a date thats possible and then use your suggestion to run one or the other script.

ZWaveNode_DSB29DoorWindowSensorGen2FrontDoor_BinarySensor.state + " - " + now.getMonthOfYear + “/” + now.getDayOfMonth + " | " + now.getHourOfDay + “:” + now.getMinuteOfHour)

Please try the following:

  • create following file: /etc/openhab/scripts/door.sh
  • content:
#!/bin/sh
echo test > /tmp/output.txt
/bin/date >> /tmp/output.txt
  • make sure that permissions are ok:
chmod ugo+rx /etc/openhab/scripts/door.sh
  • create a DSL rule file that only contains this rule
  • call it from wihtin your DSL rule:
var result = executeCommandLine( "/etc/openhab/scripts/door.sh", 5000 )
logInfo("FrontDoor", result )
  • make sure that your rule is triggered

  • is there now a file /tmp/output.txt ?

  • Once / if the above works modify the shell script it needs to contain

#!/bin/sh
state=$1
echo $state > /tmp/output.txt
date >> /tmp/output.txt
  • check that the permissions are correct / resp. do
chmod ugo+rx /etc/openhab/scripts/door.sh
  • modify your rule that it looks like:
var result = executeCommandLine( "/etc/openhab/scripts/door.sh@@" + FrontDoorLogString.state, 5000 )
logInfo("FrontDoor", result )

This is untested as I am on OH 3.4.X.

Oh wow, this is very close!!! Thanks…only thing is it overwrites the data each time; and doesn’t append it. How can I get this to just append new lines each time.

I think i got it; I put an extra > in the first line of the script; that works then.

Can you explain to me what this does: logInfo(“FrontDoor”, result )

I stand corrected; looking a bit further:
I opened the door and closed it within 5 secs.

This is what logged: the open comes before the close.
The timestamps in the string builder are way off 8 minutes in between; no way. it was 5 secs max.

CLOSED - 2/24 | 12:56
Fri Feb 24 13:04:32 EST 2023
OPEN - 2/24 | 13:4
Fri Feb 24 13:04:34 EST 2023

right. did it as I was not sure what you exactly would like to get.

That should log the return value - especially in case of problems to the openhab.log file.

The row with the datetime stamp created by the date command should nearly be the time you opened/closed the door. Your open time seems to be ok.
According to the linux datetimestamp it was two seconds between open and close.

In case you are just interested to log when door was opened/close you could look to the events.log file it already should show these state changes.

Ahhh ok; yeah nothing is showing up in openhablog or events log when that statement runs. I figured that but wanted to make sure…weird.

In case you would like to have the state and linux date on one row it should work by running:

echo `date` : $state >> /tmp/output.txt