Formula >40 char with log()

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
demJanus
New user
Posts: 9
Joined: 25 Apr 2023, 10:22

Formula >40 char with log()

#1 Post by demJanus » 25 Apr 2023, 10:27

Hi,

I would like to use a formula to convert an NTC reading to a temperature.
The formula would be: T = (1/(log(%value%/(32768-%value%))/3988+0.003354)-278.4235

The isses are
a) > 40characters
b) log() needed and at least du to the doc not available.

What are the best options to solve this?
Preferably within the formula of a device, I'd like not to fill up slots with dummy devices because I have a lot of entities to contol.

Thanks for your support :)

p.s. any suggestions for a good tutorial that goes a bit more in depth about the architecture of the source code, rules, etc.?

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

Re: Formula >40 char with log()

#2 Post by TD-er » 25 Apr 2023, 10:44

You can also act on the generated event and handle the value in the rules.
Then just store it in a variable.

Let's assume your current task is called "ntc" and the value name is "t"
So right now you're using the task variable as [ntc#t]
With a variable you would use [var#1] (or whatever nr you gave it)

Rules would look like this:

Code: Select all

On ntc#t do
  let,1,1/(log(%eventvalue1%/(32768-%eventvalue1%))/3988+0.003354)-278.4235
endon

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

Re: Formula >40 char with log()

#3 Post by TD-er » 25 Apr 2023, 10:52

By the way, you're missing a parenthesis, so not sure whether I can simplify the 1/ part or not.

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

Re: Formula >40 char with log()

#4 Post by TD-er » 25 Apr 2023, 11:06

Just based on the 2^15 value in your formula, I assume you're measuring using some ADC which isn't the internal ADC.

Would it be useful to have filtering/binning/interpolation options for other ADC plugins as well, like how it is now done for the internal ADC?

See: https://espeasy.readthedocs.io/en/lates ... #p002-page

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

Re: Formula >40 char with log()

#5 Post by TD-er » 25 Apr 2023, 11:07

Oh and documentation for the rules: https://espeasy.readthedocs.io/en/lates ... Rules.html

demJanus
New user
Posts: 9
Joined: 25 Apr 2023, 10:22

Re: Formula >40 char with log()

#6 Post by demJanus » 27 Apr 2023, 18:24

It is an ADS1115, and yes, some kind of filter would be nice there too.

So in my case I have a "Device ADS1115_CH0".
For the "Voltage" value I use the formula to calculate it as %value%*4.096/32768

I then add a Rule as follows (parenthesis was mising, you are right)

On ADS1115_CH0#Voltage do
let,1,(1/(log(%eventvalue1%/(32768-%eventvalue1%))/3988+0.003354)-278.4235)
endon

Can I now visualize this value somehow using a Dummy device?

User avatar
Ath
Normal user
Posts: 3506
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Formula >40 char with log()

#7 Post by Ath » 27 Apr 2023, 18:58

demJanus wrote: 27 Apr 2023, 18:24 On ADS1115_CH0#Voltage do
let,1,(1/(log(%eventvalue1%/(32768-%eventvalue1%))/3988+0.003354)-278.4235)
endon

Can I now visualize this value somehow using a Dummy device?
This will put it in the Dummy Device called 'dummy', in the value named 'value'

Code: Select all

On ADS1115_CH0#Voltage do
  taskvalueset,dummy,value,(1/(log(%eventvalue1%/(32768-%eventvalue1%))/3988+0.003354)-278.4235)
endon
When connecting that dummy to a controller, you can also send it out immediately by using TaskValueSetAndRun command, instead of TaskValueSet ;)
/Ton (PayPal.me)

demJanus
New user
Posts: 9
Joined: 25 Apr 2023, 10:22

Re: Formula >40 char with log()

#8 Post by demJanus » 27 Apr 2023, 22:15

Thanks,

that works. The only Problem with that is that it fills up a slot and I am running out of them already :roll:
Can one have more than 12?

Btw. is this a bug in the rules parser? I need a parenthesis around the ln...

this returns a wrong value:
ln((10000*%eventvalue1%)/(3.3-%eventvalue1%)/10000)/3988

this returns the correct value:
(ln((10000*%eventvalue1%)/(3.3-%eventvalue1%)/10000))/3988

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

Re: Formula >40 char with log()

#9 Post by TD-er » 27 Apr 2023, 22:54

Not sure what has precedence, ln or /
So not sure if this actually is a bug.

User avatar
Ath
Normal user
Posts: 3506
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Formula >40 char with log()

#10 Post by Ath » 27 Apr 2023, 23:00

demJanus wrote: 27 Apr 2023, 22:15 that works. The only Problem with that is that it fills up a slot and I am running out of them already :roll:
Can one have more than 12?
When running ESPEasy on an ESP32, you have, besides more GPIO pins, memory and clock speed, 32 task :).

You can also send it to an external destination, like MQTT or Domoticz, etc., using either the Publish command (MQTT) or SendToHTTP/PostToHTTP/PutToHTTP commands, details are in the Command reference documentation.
demJanus wrote: 27 Apr 2023, 22:15 this returns a wrong value:
ln((10000*%eventvalue1%)/(3.3-%eventvalue1%)/10000)/3988

this returns the correct value:
(ln((10000*%eventvalue1%)/(3.3-%eventvalue1%)/10000))/3988
That depends where/how you use it. When in a Let command, or prefixed with = it should work without the extra parenthesis, when used in other statements or commands it sometimes needs some extra 'persuasion' to have the full calculated result, because of the order of processing functions and other calculations.
/Ton (PayPal.me)

demJanus
New user
Posts: 9
Joined: 25 Apr 2023, 10:22

Re: Formula >40 char with log()

#11 Post by demJanus » 28 Apr 2023, 09:03

TD-er wrote: 27 Apr 2023, 22:54 Not sure what has precedence, ln or /
Ath wrote: 27 Apr 2023, 23:00 That depends where/how you use it.
Imho it should be out of question in that case.
ln(x)/y == (ln(x))/y
At least from a mathematicians view I think no one would expect the observed behaviour?
Ath wrote: 27 Apr 2023, 23:00 When running ESPEasy on an ESP32, you have, besides more GPIO pins, memory and clock speed, 32 task :).
Yeah, but for economical reasons that is out of question :)
Ath wrote: 27 Apr 2023, 23:00 You can also send it to an external destination, like MQTT or Domoticz, etc., using either the Publish command (MQTT) or SendToHTTP/PostToHTTP/PutToHTTP commands, details are in the Command reference documentation.
Thats how its gonna be used primarily. So yes, it is somewhat a luxury problem. I could in deed do the calculation on the target side as well, but I like to have a good abstraction, so all senders should provide their stuff in a SI standardized format. And I would like to be able to verify plausibility in the easeasy web interface as well.

p.s.If I would like to add a special function to the formula parser of the tasks to execute my formula in the source code, where would I have to look? Do you have a suggestion how that could look like?

Thanks for your support :)

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

Re: Formula >40 char with log()

#12 Post by TD-er » 28 Apr 2023, 11:52

I think we could make the formula boundaries a bit more flexible.
In recent changes, all access to the formulas is done via the cache struct, so fetching the formula from the settings only has to be changed in that cache object.
Regarding settings compatibility, the formula is a 2-dimensional array, thus we can also consider it to be 1 single array and still maintain compatibility with previous settings.

I have to look into it a bit more, to truly assess the impact of such a change.

User avatar
Ath
Normal user
Posts: 3506
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Formula >40 char with log()

#13 Post by Ath » 28 Apr 2023, 21:02

demJanus wrote: 28 Apr 2023, 09:03 p.s.If I would like to add a special function to the formula parser of the tasks to execute my formula in the source code, where would I have to look? Do you have a suggestion how that could look like?
Most single-argument math functions are handled in method apply_unary_operator() in source Rules_calculate.cpp, you could have a look if your function could fit in there (though it may be a tad complicated to add a new function)

Alternative could be that you add a rule that handles the calculation, and would return the result in an internal var, that can be retrieved by [var#x] where x is the number matching with the 'let' command you used (let,10,..., used via [var#10]).

Code: Select all

on myCalculation do
  let,10,%eventvalue1%*2 // result available in [var#10], [int#10] and %v10%
endon

// use as:
...
  event,myCalculation=5.21 // Pass value 5.21 to your special calculation
  // do something with [var#10]
...
/Ton (PayPal.me)

Post Reply

Who is online

Users browsing this forum: No registered users and 49 guests