Error occurred when executing commandLine '[php /etc/openhab/scripts/mysqltocsv.php number_airsensor1_co2]'
java.io.IOException: Cannot run program "php /etc/openhab/scripts/mysqltocsv.php number_airsensor1_co2": error=2, No such file or directory
Error occurred when executing commandLine '[/usr/bin/php /etc/openhab/scripts/mysqltocsv.php number_airsensor1_co2]'
java.io.IOException: Cannot run program "/usr/bin/php /etc/openhab/scripts/mysqltocsv.php number_airsensor1_co2": error=2, No such file or directory
Aside from the problem you’re facing, it seems to me that you should be able to do what you’re doing in php with jsscripting, thus completely avoiding the need to execute an external (php) script altogether. You can use jsscripting + JDBC to connect to mysql. Remember you have the combined features of Java AND jsscripting at your disposal. You can create a Java object (e.g. jdbc), or install third party js packages.
Getting back to the issue at hand, you’ve only shown the php script. The issue is in your jsscripting call that you claimed to log “null”. That’s the part that we need to see.
Your php script just reads from a table and outputs it to a csv, it doesn’t output anything to stdout, and only to stderr (via die) if an error occurred. So naturally no output to be expected. I presume you’re aware of this.
The other version returns a String, I think it’s the stdout. Either way, it won’t show you anything because your php script doesn’t output anything.
You could start by executing a simple php script <?php echo "test"; ?> and execute that from your jsscripting (using the version that returns you the output, i.e. use a Duration as the first argument).
That would tell you whether the script got executed.
I am not familiar with JS myself. I went from RulesDSL → Jython → Jruby. I can give it a crack in jruby if that’s the path you want to explore. Alternatively, someone else who’s more used to jsscripting might chime in.
I can’t help but wonder what you’re going to do with the CSV though? Is that yet another intermediate step to achieve the end goal that you could perhaps even skip altogether?
Lastly I also noticed that your SQL schema is a bit weird. Why have one table for each itemid? That seems to violate good database design principles. Is that from persistence or something?
I’m starting to suspect that this is an XY problem, but since we aren’t given the whole story, it’s hard to tell.
Time series data often looks like that is an SQL database unfortunately. That’s one reason specific time series databases like InfuxDB were created.
I’m not sure that the Database driver library is make available to rules so I don’t know if it works be possible to code this entirely in JS unless there is a MysQL Node library that could be installed and used.
@milo , if the script is generating errors you’d never know it. Add a Duration to the call to executeCommandLine and log what it returns.
In jruby I can use JDBC pretty easily. I’ve done it to create and access sqlite tables. I presume there should be something similar in JS, or at the very least, simply use Java’s objects, similar to what the jdbc persistence extension does. Just have to find the docs for the api.
Did you use the second form of executeCommandLine as per my link above? The first argument needs to be a Duration object. That’s when executeCommandLine would return the script’s output.
That’s the part I’m not sure is possible. Not every library used by every add-on is available in rules. Java provides a generic JDBC interface but those depend on a driver library for the specific database. I don’t think there is a way to make that driver library available to the rules, even though the JDBC persistence add-on uses it.