Page 1 of 1

Calculate with data from 2 different sensor

Posted: 17 Oct 2023, 23:33
by Cover1987
Hi guys,
i'm struggling with some rules.
I try to calculate the solved CO2 in a liquid with an Pressure sensor (12MPa sensor as internal imput) and the temperature (bme280) - and show it on an OLEd display (thats not the problem).
The sensors are working and i read the rules manual... but i cant find a solution.

i have the formula:
co2 = (pressure[bar] + 1,013) * (2,71828182845904^(-10,73797 + (2617,25/(Temperatur[°C] + 273,15)))) * 10

I guess i need a new variable (co2) but it can't be an uint32_t. Can i use another type of var?

I Tried to start with something like

Code: Select all

on BME#Temp do
   logentry,"Aenderung Temp: [BME#Temp] "
   <here should be the the definition of the variable and the calculation>
endon

on Drucksensor#Druck do
   logentry,"Aenderung Druck: [Drucksensor#Druck]"
   <here should be the the definition of the variable and the calculation>
endon
Everytime when 1 of the 2 sensor values is changing the formula should be newly calculated (and showed on the OLED)

Can anyone help me with it?

Re: Calculate with data from 2 different sensor

Posted: 18 Oct 2023, 00:11
by TD-er
On each update of either value, you should generate an event (preferrably an async event)

This way you only need to have the computational code only once.

Internal variables in rules (those in memory, thus lost after a reboot) are of type double (in C++)
So I doubt you need to store it as an uint32 to keep resolution.

Code: Select all


on BME#Temp do
   logentry,"Aenderung Temp: [BME#Temp] "
   asyncevent,ComputeCO2
endon

on Drucksensor#Druck do
   logentry,"Aenderung Druck: [Drucksensor#Druck]"
   asyncevent,ComputeCO2
endon

on ComputeCO2 do
  // Split this into several steps
  // let,1,(2617.25/([BME#Temp] + 273.15))-10.73797
  let,1,[BME#Temp]+273.15  // To Kelvin
  let,1,2617.25/[var#1]
  let,1,[var#1]-10.73797
 
  let,1,2.71828182845904^[var#1]
  let,1,10*[var#1]
  let,2,[Drucksensor#Druck]+1.013
  let,1,[var#2]*[var#1]  // [var#1] is final CO2 value

endon

Re: Calculate with data from 2 different sensor

Posted: 18 Oct 2023, 00:56
by Cover1987
Oh wow, that was fast! Thank you very very much!


(There was only on typo in the last line that i could figure out thanks to the great comments ;) there is 2 times var#2 )

Re: Calculate with data from 2 different sensor

Posted: 18 Oct 2023, 01:08
by TD-er
Ah check!
I changed it for future reference :)

It also means it is time for me to turn off my head and put my computer to sleep... or something like that ;)