I made a pythonscript that establishes a serial connection with a device and receives every minute a string from that device. There are only two important substrings that I need: pH and redoxvalue.
This is my python script:
Blockquotecat /home/openhab/scripts/readserial.py
#!/bin/env python
import time
import serial
import re
not entirely sure what is calling the script, but other than including MQTT, you could just do a REST API call from yr script to update 2 items, provided they are on the same network. Only takes 2 lines
need to convert your floats to string though which I think is done with: PHvalue=str(var_ph)
But maybe I am far off and don’t fully grasp what you want
Then the JSONPATH transform will be able to get the two values easily:
Items:
Number WaterPH "Water PH: [%.1f PH]"
Number WaterRedox "Redox Potential: [%.3f mV]"
Rule:
rule "Get water data"
when
Time cron "0 0/5 * * * ? *" //Every 5 minutes change that as you wish
then
val waterJson = executeCommandLine("python /home/openhab/scripts/readserial.py", 5000) //5 seconds time out for script to execute
WaterPH.postUpdate(transform("JSONPATH", "$.PH", waterJson))
WaterRedox.postUpdate(transform("JSONPATH", "$.redoxPotential", waterJson))
end
I agree with Josar, this sort of thing is what the Serial Binding is made for. Failing that, it is probably better to have your script report to OH rather than have OH polling the script by executing it over and over like Kees recommends.
Finally, as Vincent demonstrates, it is very possible to have OH call the script (note there is a missing open quote in the rule trigger Time "cron 0 0/5 * * * ? *"). However, you will have to take the while True part out of the script and have the script exit once one line has been read from the device.
It would also be possible to use the Exec binding for this, but since the script returns two values on one line you will need a Rule like this one anyway so it doesn’t buy you much.
No, it should work, I think
There is obviously an error somewhere
Change the rule to:
Every 1 minute just for debugging
rule "Get water data"
when
Time cron "0 0/1 * * * ? *" //Every 5 minutes change that as you wish
then
val waterJson = executeCommandLine("python /home/openhab/scripts/readserial.py", 5000) //5 seconds time out for script to execute
logInfo("WATER", waterJson)
WaterPH.postUpdate(transform("JSONPATH", "$.PH", waterJson))
WaterRedox.postUpdate(transform("JSONPATH", "$.redoxPotential", waterJson))
end