Openhab Rule taking time to execute

Hello everyone,

I am using Openhab2 on RPi3 B+ model. I am currently using influxdb database + Grafana to store my real time data (from ECG Sensor) and visualize it on graph.

From my node(STM32F4) i am reading ADC value from ECG in every 3.9us and sending it serially (baud rate 9600) every 180ms to RPi3 and it works fine and rules are able to execute properly.

But as soon as i send the adc data serially below 150ms, openhab rules is not able to execute on the adc data.

For referrence: I am sending “rate:%d03bpm” serially

Below are my configuration file along with and log:tail

demo.item file

String STM32F4 “STM32F4 [%s]” { serial="/dev/ttyUSB0" }
Number STM32F4_pot1 “STM32F4 Pot1 [%d bpm]”

demo.rules file

rule “STM32F4”
when
Item STM32F4 received update
then
var String EPBUpdate = STM32F4.state.toString.trim

var int pot1StartsOn = EPBUpdate.indexOf(“rate:”) + “rate:”.length
var String pot1 = EPBUpdate.mid(pot1StartsOn, EPBUpdate.indexOf(‘bpm;’)-pot1StartsOn)
var Double pot1Double = new Double(pot1)

STM32F4_pot1.postUpdate(pot1Double)
end

And demo.sitemap

sitemap demo label=“Main Menu”
{
Frame label=“STM32F4 Serial” icon=“socket” {
Text item=STM32F4
Text item=STM32F4_pot1

  }		

}

Below is the log tail, were i can find the rules aren’t splitting the string .

Openhab Log:Tail

13:08:50.096 [DEBUG] [b.internal.InfluxDBPersistenceService] - got   String value rate:071bpm;rate:070bpm;rate:070bpm;rate:062bpm;

13:08:50.097 [DEBUG] [b.internal.InfluxDBPersistenceService] - got DecimalType value 71.0
13:08:50.097 [INFO ] [smarthome.event.ItemStateChangedEvent] - STM32F4_pot1 changed from 67.0 to 71.0
13:08:54.221 [DEBUG] [b.internal.InfluxDBPersistenceService] - got String value rate:061bpm;rate:069bpm;rate:071bpm;rate:077bpm;rate:052bpm;rate:086bpm;rate:066bpm;rate:066bpm;rate:066bpm;rate:052bpm;rate:088bpm;rate:070bpm;rate:071bpm;rate:071bpm;rate:087bpm;rate:067bpm;rate:059bpm;rate:065bpm;rate:064bpm;rate:096bpm;rate:074bpm;
13:08:54.223 [INFO ] [smarthome.event.ItemStateChangedEvent] - STM32F4 changed from rate:071bpm;rate:070bpm;rate:070bpm;rate:062bpm; to rate:061bpm;rate:069bpm;rate:071bpm;rate:077bpm;rate:052bpm;rate:086bpm;rate:066bpm;rate:066bpm;rate:066bpm;rate:052bpm;rate:088bpm;rate:070bpm;rate:071bpm;rate:071bpm;rate:087bpm;rate:067bpm;rate:059bpm;rate:065bpm;rate:064bpm;rate:096bpm;rate:074bpm;
13:08:54.223 [DEBUG] [b.internal.InfluxDBPersistenceService] - got DecimalType value 61.0
13:08:54.224 [INFO ] [smarthome.event.ItemStateChangedEvent] - STM32F4_pot1 changed from 71.0 to 61.0

So is their any possible solution read data serially at every 100ms ?

I would’ve done this in a external script (ie. python). Seems a little to heavy on the timing for OH to handle and prioritize, but that’s just my opinion.

1 Like

Not reliably with openHAB. The rule may take more than 100ms to run including item updates, run the code and persistence, you will run into multiple instances of the rule running working on inconsistent item states…

As @lfs_alp5 mentioned, you should try to clean the the data in a python script and then send the cleaned up data (The value only) to OH via MQTT

openHAB never intended to be a real time system, processing with ECG is a non starter. You might have some kind of separate utility sending occasional updates to OH - “mood: excited” or suchlike?

I agree with everyone else. The answer is no, this is not something that OH can handle. See Why have my Rules stopped running? Why Thread::sleep is a bad idea for why. In your case it isn’t because you are artificially extending how long it takes a Rule to run, you are just triggering it too fast and the backlog of Rules waiting their turn to execute grows beyond the size of the thread pool.

If all you want to do is store and chart this information, bypass OH entirely and save the data straight into InfluxDB (which should be able to process that fast). You are charting with Grafana so there really is no reason that OH needs this information. If it does, see rosskko57’s suggestion to process the readings into a more generic and less frequently changing overall state.

Okay. I bypassed the OH.

I read the ECG values every 4ms and use python script to store it in influxdb. (storing data in influxdb without timestamp). Now when i try to plot it using Graffana, i dont get proper ECG waveform.

Now but when i store the same ECG values in excel and plot graph in excel it gives me correct ecg waveform.

So do you know any particular reason why i am unable to plot the correct data on graffana ?

So if you bypassed OH, why are you asking in the OH forum?
You will find better help in influx and grafana forums

BTW grafana NEEDS timestamps to create graphs…

Grafana is very flexible in how it generates your charts. There are all sorts of functions you can apply to smooth the data, stair step the data, and stuff like that. Assuming you have timestamps with your readings like Vincent indicates, it is probably an issue with which functions you chose to graph the data.

And as Vincent said, we are not Grafana experts here. You will find better help on a Grafana forum.

1 Like