"IF" statement in rule

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

"IF" statement in rule

#1 Post by mackowiakp » 09 Jul 2021, 12:54

I use the BME680 as an atmospheric pressure sensor. The measurement is made every 2 minutes. Sometimes (once in several thousand measurements) an erroneous measurement occurs. Everything is OK, if it is a completely pointless measurement of the 2500 HPa type. It is easy to omit such a measurement using the rule.
However, sometimes there is a measurement value, e.g. 970 HPa, and earlier and later measurements show, for example, something around 1020 HPa.
So I would like to omit the measurement that differs from the previous one by more than, for example, 10 HPa because it is impossible for 2 minutes to change by more than 10 HPa.
Of course, the difference between the previous and the current measurement can be negative or positive. So absolute value is important.
How do I create an "IF" statement that checks this on one line?

In my case it is as follows:
- the %v3% variable contains the value of the previous measurement
- the [BME680#Pressure] variable contains the value of the current measurement

Rule should look something like this:

Code: Select all

If  (condition)
      // rest of "if" procedure
      Let, 3, [BME680#Pressure]
EndIf
Can someone tell me what it should look like (condition)

To get rid of possible incorrect measurements after initialization of the device, the measurements are taken into account only 5 minutes after the device is started and during these 5 minutes all measurements are entered into the %v3% variable, regardless of whether they are meaningful or not, but not sent to Domoticz

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

Re: "IF" statement in rule

#2 Post by TD-er » 09 Jul 2021, 13:10

You can do this in rules, but you can also apply a filter in the formula field of the task config page.
A formula can use the previous value too.
Only problem with a 'formula' is that it cannot perform if-then like operators, but you can make an IIR filter by only adding a fraction of the difference as the new value.

Something like (pseudo code):
new = (new - old)/16 + old

This will smooth values and also cause a 'delay' in the values.
But it will reduce the effects of spikes quite a bit.
For example if someone just slammed a door during measurement.

Not sure about the BME680, but the BME280 does actually use a similar filter in the chip itself.

For using it in the rules, you cannot store the value back into the task value, so you can either store it in a dummy task (which can send it to a controller) or store it in a variable.
Only problem is how to initialize it.
What is "the truth" ?
If the first measurement is the fluke value, then you may not recover from it.
Therefore I guess it makes sense to implement an IIR filter, either in rules or in the formula section.

mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Re: "IF" statement in rule

#3 Post by mackowiakp » 09 Jul 2021, 13:25

I am not storing value in the value task but in user variable #3.
The "Truth" is that absolute value of difference between previews and current pressure value is not bigger then 10.
And - as I wrote - measurement values are taking into account 5 min after initialization (restart).
It should avoid from erroneous values during initialization.
I think that such erroneous values ocures, because of existing problem with I2C bus. Very rare, but exist.

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

Re: "IF" statement in rule

#4 Post by TD-er » 09 Jul 2021, 14:14

Yep, regardless of the value, you should copy it to the variable and only accept it as a new measurement if it does not differ significantly from the previous value.

So you then need 2 variables:
- One for previous value
- One for "last accepted measurement"

Code: Select all

on BME680#Pressure do
  // var#1 = last accepted measurement
  // var#2 = previous value
  // var#3 = difference
  
  let,3,abs([var#2]-%eventvalue1%)

  if [var#3] < 10
    let,1,%eventvalue1%
  endif
  let,2,%eventvalue1%
endon
Not tested, but I think it should be something like this.

mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Re: "IF" statement in rule

#5 Post by mackowiakp » 09 Jul 2021, 14:25

THX. Thats the way I think !

Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests