Calculate with data from 2 different sensor

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Cover1987
New user
Posts: 5
Joined: 07 Dec 2021, 11:57

Calculate with data from 2 different sensor

#1 Post by Cover1987 » 17 Oct 2023, 23:33

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?

TD-er
Core team member
Posts: 8756
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Calculate with data from 2 different sensor

#2 Post by TD-er » 18 Oct 2023, 00:11

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

Cover1987
New user
Posts: 5
Joined: 07 Dec 2021, 11:57

Re: Calculate with data from 2 different sensor

#3 Post by Cover1987 » 18 Oct 2023, 00:56

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 )

TD-er
Core team member
Posts: 8756
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Calculate with data from 2 different sensor

#4 Post by TD-er » 18 Oct 2023, 01:08

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 ;)

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 29 guests