Functions in rules. For example, ABS.

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
User avatar
Andrew Mamohin
Normal user
Posts: 95
Joined: 12 Jun 2020, 08:30
Location: Belarus, Gomel
Contact:

Functions in rules. For example, ABS.

#1 Post by Andrew Mamohin » 22 Sep 2020, 13:55

I need an ABS function in the rules. I need to execute a block based on the difference between two temperatures (regardless of which direction) How can you do this?
Andy.

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

Re: Functions in rules. For example, ABS.

#2 Post by TD-er » 22 Sep 2020, 16:37

Right now we don't have an abs call, but you could store the result in a variable and simply check to see if it is negative.
If it is negative you can multiply its value by -1.
Or just check the 2 values you want to subtract.
If A > B do A-B else B-A.

User avatar
Andrew Mamohin
Normal user
Posts: 95
Joined: 12 Jun 2020, 08:30
Location: Belarus, Gomel
Contact:

Re: Functions in rules. For example, ABS.

#3 Post by Andrew Mamohin » 22 Sep 2020, 17:57

TD-er wrote: 22 Sep 2020, 16:37 Right now we don't have an abs call
Well, it's ass-backwards! :)
Thanks for the help, I did:

Code: Select all

on T_1#T do
 if [T_1#T] > [T_2#T]
  TaskValueSet 12,3,[T_1#T]-[T_2#T]
 else
  TaskValueSet 12,3,[T_2#T]-[T_1#T]
 endif
endon

on T_2#T do
 if [T_1#T] > [T_2#T]
  TaskValueSet 12,3,[T_1#T]-[T_2#T]
 else
  TaskValueSet 12,3,[T_2#T]-[T_1#T]
 endif
endon
How can I do it in one block? There are no subroutines in the rules.
Andy.

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Functions in rules. For example, ABS.

#4 Post by Micha_he » 22 Sep 2020, 17:58

or...

[Device#Value]^2^.5

for ABS.

User avatar
Andrew Mamohin
Normal user
Posts: 95
Joined: 12 Jun 2020, 08:30
Location: Belarus, Gomel
Contact:

Re: Functions in rules. For example, ABS.

#5 Post by Andrew Mamohin » 22 Sep 2020, 18:09

Micha_he wrote: 22 Sep 2020, 17:58 [Device#Value]^2^.5
Wow! It's beautiful!
Andy.

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

Re: Functions in rules. For example, ABS.

#6 Post by Ath » 22 Sep 2020, 21:19

Andrew Mamohin wrote: 22 Sep 2020, 17:57 How can I do it in one block? There are no subroutines in the rules.
User defined events are the substitute for subroutines:

Code: Select all

on T_1#T do
  event,CalcAbsDiff
endon

on T_2#T do
  event,CalcAbsDiff
endon

on CalcAbsDiff do
 if [T_1#T] > [T_2#T]
  TaskValueSet 12,3,[T_1#T]-[T_2#T]
 else
  TaskValueSet 12,3,[T_2#T]-[T_1#T]
 endif
endon
Edit: Fixed typo
Last edited by Ath on 23 Sep 2020, 11:24, edited 1 time in total.
/Ton (PayPal.me)

User avatar
Andrew Mamohin
Normal user
Posts: 95
Joined: 12 Jun 2020, 08:30
Location: Belarus, Gomel
Contact:

Re: Functions in rules. For example, ABS.

#7 Post by Andrew Mamohin » 23 Sep 2020, 08:02

Ath wrote: 22 Sep 2020, 21:19 User defined events are the substitute for subroutines:
Thank! I did this.
You got a typo:

on T_2#T do
event,CalcAbsDiff
enddo
need endon
Andy.

Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests