Page 1 of 1

Variables for use in rules??

Posted: 09 Jan 2019, 21:19
by bastibasti
Hi,

I'm trying to do something like this
(its a simple dimmer, a push button increases a variable, and the pwm output is set according to one of the 5 available dimmer levels)

Code: Select all

on button#switch do
 
  if [button#switch]=0
   $a=$a+1
  endif

  if $a=5
   $a=0
  endif

 if $a=0
  pwm,2,1024,750
 endif

 if $a=1
  pwm,2,1000,750
 endif

 if $a=2
  pwm,2,800,750
 endif

 if $a=3
  pwm,2,500,750
 endif

 if $a=4
  pwm,2,0,750
 endif

endon 
any idea how to achieve this??

Re: Variables for use in rules??

Posted: 09 Jan 2019, 21:25
by Domosapiens
In rules you can use the command let
let,1,10 this will assign the value 10 to %v1%.

To get the value of %v1%, you can use %v1% or [VAR#1] depending on the rule syntax:
if %v1%=10
or
taskvalueset,1,1,%v1%
or
taskvalueset,1,1,[VAR#1]

Re: Variables for use in rules??

Posted: 11 Jan 2019, 01:28
by Jackill
Domosapiens wrote: 09 Jan 2019, 21:25 In rules you can use the command let
let,1,10 this will assign the value 10 to %v1%.
WOW!
Great info! Searched for it resultless..

Where did you find the list of commands to use in rules?

Re: Variables for use in rules??

Posted: 11 Jan 2019, 05:35
by grovkillen
I'm doing my best but need to work on my daytime job too. But the commands are found here: https://espeasy.readthedocs.io/en/lates ... mmand.html

Not all are listed, I will do my best adding them but I don't have the the time just now.

Re: Variables for use in rules??

Posted: 11 Jan 2019, 10:06
by Domosapiens
Where did you find the list of commands to use in rules?
While reading the forum, I collect snippets of Rules as examples in a document.

The (new) "Let" statement one was found here:
viewtopic.php?f=4&t=5961&p=32580&hilit= ... ill#p32580

Re: Variables for use in rules??

Posted: 11 Jan 2019, 14:44
by Jackill
grovkillen wrote: 11 Jan 2019, 05:35 I'm doing my best but need to work on my daytime job too.
Shure. This is not even discussed, daytime job on first place.
Nevertheless, system variables can save much.
Face this when I had to do calculations for my solar heating system.
Code like this: "if [Boiler#Temp]<[Solar#Temp]+5 <do smthng> endif" not worked, it need to be passed thru dummy (taskValueSet).
Now I'll save 1 of 12 tasks.
Domosapiens wrote: 11 Jan 2019, 10:06 While reading the forum, I collect snippets of Rules as examples in a document.
Good practice! I will put into service.

Re: Variables for use in rules??

Posted: 14 Jan 2019, 12:11
by bastibasti
any idea how to increment a value?

let 1,%v1%+1??

Re: Variables for use in rules??

Posted: 14 Jan 2019, 13:45
by grovkillen

Code: Select all

Let,1,[VAR#1]+1

Re: Variables for use in rules??

Posted: 08 Feb 2020, 07:40
by EDsteve
Somehow i can't get it working 100% with the Let command.

When soil humidity is higher than 900 it should set varia#2 to 2. And if this var#2 is 2 it should trigger a relay and increment a second variable (The incrementation of the second variable works). But somehow it doesn't enter the "if [var#2]=2 do".

Code: Select all

on Soil#Analog do
 SendToHTTP,192.168.8.115,8080,/json.htm?param=udevice&type=command&idx=25&nvalue=[Soil#Analog]
 if [soil#Analog]>900 do
   timerSet,1,2
   Let,2,[var#2]=2
 endif
endon

On Rules#Timer=1 do
 if [var#2]=2 do
  GPIO,12,1
  Let,1,[var#1]+1
  timerSet,2,2
 endif
endon

On Rules#Timer=2 do
  if [var#1]=3 do
   GPIO,12,0
   Let,1,[var#1]=0
   Let,2,[var#2]=0
  else
   GPIO,12,0
   timerSet,1,6
  endif
endon
I am also not really sure how it all works with the Let command.

What exactly does the number 1 stand for: Let,1,[VAR#1]+1
And my Dummy devices don't change it's value. So with the Let command i can have a variable without connected to a Dummy device?
So the Let command has nothing to do with TaskValueSet ?

Sorry for my maybe stupid questions.

Re: Variables for use in rules??

Posted: 08 Feb 2020, 09:42
by grovkillen
Your syntax is wrong, see here:

Code: Select all

on Soil#Analog do
 SendToHTTP,192.168.8.115,8080,/json.htm?param=udevice&type=command&idx=25&nvalue=[Soil#Analog]
 if [soil#Analog]>900 do
   timerSet,1,2
   Let,2,2  //<---- no equal sign.
 endif
endon

On Rules#Timer=1 do
 if [var#2]=2 do
  GPIO,12,1
  Let,1,[var#1]+1  //THIS WILL ADD 1 to the variable one i.e. "Let,1,1" would make the var one = 1 and "Let,15,33" would make var fifteen = 33
  timerSet,2,2
 endif
endon

On Rules#Timer=2 do
  if [var#1]=3 do
   GPIO,12,0
   Let,1,0  //<---- no equal sign.
   Let,2,0  //<---- no equal sign.
  else
   GPIO,12,0
   timerSet,1,6
  endif
endon
I guess the most user friendly way would be the syntax "Let,v1=..." but that would make the syntax for Let be completely different from the other commands. So we will keep it using the comma instead of the equal sign for consistency.

Re: Variables for use in rules??

Posted: 08 Feb 2020, 09:45
by grovkillen
Let are internal variables, the task values are just that, task values. So they have nothing to do with each other, BUT one can be used to influence the other using formulas and task value set. So if you want to use an internal value for a task value you would need to use a dummy variable and set it by using the TaskValueSet (or TaskValueSetAndRun) to populate it with the number from the internal variable.

Re: Variables for use in rules??

Posted: 08 Feb 2020, 10:47
by EDsteve
Ohh. Thanks so much. Now i understand. I should have thought about that :D
I know... script kiddies like me are annoying.

Re: Variables for use in rules??

Posted: 08 Feb 2020, 11:54
by grovkillen
EDsteve wrote: 08 Feb 2020, 10:47 Ohh. Thanks so much. Now i understand. I should have thought about that :D
I know... script kiddies like me are annoying.
Don't say that, I'm a script kid myself. :D