Populate things from a python script

  • Platform information:
    • Hardware: RASPY
    • OS: Openhabian2

Hi, I am trying to populate two things from python script that access to my solar panel and obtains data.
The python script currently writes a json file. My idea (I am opened to new ones) is to call the script from a rule and load the results to the things.
However, I am able to run the script but completely unable to find the output file.
Specifically:

rule "SUNNYBOY_GET_POWER"
when
    Time cron "0 0/1 * 1/1 * ? *"
then
    logInfo("SunnyBoy", "Starting update of parameters")
    val result = executeCommandLine("python /etc/openhab2/scripts/currentconsumption.py", 5000)
    logInfo("SunnyBoy", "Result: " + result1)
end

I know that the script is running since I can see any print that I add in the resulting log. However, it can not write the file.
I have also checked permissions in the output folder and:
sudo -u openhab python /etc/openhab2/scripts/currentconsumption.py
works.
I have also tried to write a simple file with “touch hello.txt” and the problem is the same, it seems it is unable to write the file. On the other side, “echo hello” works.
I have seen that there are other ways to populate things, like using the exect binding or using an API, but I am not sure it will work for my case (Multiple values are obtained and I want to populate more than one thing at the same time)

Any clue?

Your problem is outside of openHAB. It is your Python script that is failing to write the file so you need to look to the Python script to figure out why. It is almost certainly producing a stack trace or some other error telling you it doesn’t have permission to open the file or something like that.

It’s important to use the correct terminology. You don’t load anything into Things. You can possibly load the JSON file into a variable in a Rule, and then you can postUpdate it to an Item. But you cannot load anything into a Thing and you can’t really directly load the JSON file into an Item.

A better approach is to have your Python script print the JSON out instead of saving it to a file. Of course you can’t have anything else printed out. That will let you load the JSON straight into an Item using the Exec binding. Or you can get the JSON saved in result in your Rule if you use executeCommandLine and you can further process it from there. Or you can add the Paho library to your Python script and publish the JSON to OH on an MQTT topic. Or you can have your Python script PUT or POST the JSON to an openHAB Item directly using HTTP.

But ultimately, the reason the file isn’t created is something wholly outside of openHAB and not something we will likely be able to help you with.

1 Like

You should be able to run the individual parts of your script manually from the python command line. That sometimes points out errors.

1 Like

Thanks for the advice. The python script is working properly, actually if I run it as openhab user it is able to write the file. Anyway, I will try it without writing but loading in the variable. You opened some possibilities I can try now. Thanks!

I understand this is an old thread, but did you ever get this resolved? If so what was the end result? I’m looking at a similar situaiton.

Thanks!

Can’t say anything about the outcome of the OP.
However I think that “writes a json file” was not correct. IMHO such a script would or should have written to the console, hence the var result should have got the output ( the OP did populate result and tried to use result1???).
I have a working example using a PHP script, look Here.

Thanks for sharing that! I may go with HABApp instead. It seems way simpiler and it appears to do exactly what I am after. I appreciate the response back and example. :smiley: