Variables for use in rules??

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
bastibasti
Normal user
Posts: 25
Joined: 31 Jul 2018, 11:41

Variables for use in rules??

#1 Post by bastibasti » 09 Jan 2019, 21:19

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??

Domosapiens
Normal user
Posts: 307
Joined: 06 Nov 2016, 13:45

Re: Variables for use in rules??

#2 Post by Domosapiens » 09 Jan 2019, 21:25

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]
30+ ESP units for production and test. Ranging from control of heating equipment, flow sensing, floor temp sensing, energy calculation, floor thermostat, water usage, to an interactive "fun box" for my grandson. Mainly Wemos D1.

Jackill
New user
Posts: 5
Joined: 10 Jan 2019, 08:38

Re: Variables for use in rules??

#3 Post by Jackill » 11 Jan 2019, 01:28

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?

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Variables for use in rules??

#4 Post by grovkillen » 11 Jan 2019, 05:35

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.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

Domosapiens
Normal user
Posts: 307
Joined: 06 Nov 2016, 13:45

Re: Variables for use in rules??

#5 Post by Domosapiens » 11 Jan 2019, 10:06

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
30+ ESP units for production and test. Ranging from control of heating equipment, flow sensing, floor temp sensing, energy calculation, floor thermostat, water usage, to an interactive "fun box" for my grandson. Mainly Wemos D1.

Jackill
New user
Posts: 5
Joined: 10 Jan 2019, 08:38

Re: Variables for use in rules??

#6 Post by Jackill » 11 Jan 2019, 14:44

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.

bastibasti
Normal user
Posts: 25
Joined: 31 Jul 2018, 11:41

Re: Variables for use in rules??

#7 Post by bastibasti » 14 Jan 2019, 12:11

any idea how to increment a value?

let 1,%v1%+1??

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Variables for use in rules??

#8 Post by grovkillen » 14 Jan 2019, 13:45

Code: Select all

Let,1,[VAR#1]+1
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

User avatar
EDsteve
Normal user
Posts: 61
Joined: 11 May 2018, 21:13

Re: Variables for use in rules??

#9 Post by EDsteve » 08 Feb 2020, 07:40

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.

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Variables for use in rules??

#10 Post by grovkillen » 08 Feb 2020, 09:42

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.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Variables for use in rules??

#11 Post by grovkillen » 08 Feb 2020, 09:45

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.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

User avatar
EDsteve
Normal user
Posts: 61
Joined: 11 May 2018, 21:13

Re: Variables for use in rules??

#12 Post by EDsteve » 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.

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Variables for use in rules??

#13 Post by grovkillen » 08 Feb 2020, 11:54

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
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

User avatar
DMike92
Normal user
Posts: 29
Joined: 28 Jul 2020, 02:06
Location: Paris - France

Re: Variables for use in rules??

#14 Post by DMike92 » 01 Aug 2021, 18:30

Hi,
I'm a bit confused.
I read :

Code: Select all

In rules you can use the command let
let,1,10 this will assign the value 10 to %v1%.
- Is there any differences between v and var? %v1% represent [var#1]? Or [v#1]?
- Does these variables must exists somehere (Devices, maybe)?
- Is it possible to get the variable value with a http command (control?cmd=???)

Sorry for all these begginer questions

Michel

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

Re: Variables for use in rules??

#15 Post by Ath » 01 Aug 2021, 19:04

DMike92 wrote: 01 Aug 2021, 18:30

Code: Select all

In rules you can use the command let
let,1,10 this will assign the value 10 to %v1%.
- Is there any differences between v and var? %v1% represent [var#1]? Or [v#1]?
- Does these variables must exists somehere (Devices, maybe)?
- Is it possible to get the variable value with a http command (control?cmd=???)
a) There are 3 ways to use a variable set via the 'let,n,value' command: use %vn% where n > 0, so %v1% would be correct, use [var#n], so [var#1] would result in the same output, or use [int#n], with [int#1] resulting in the rounded integer result of the value in that variable you set.
b) These variables are stored in memory only, so putting the device in deepsleep will wipe them out. The currently assigned variables and their values can be viewed on the Tools/System Variables page.
c) No, you can not access them directly, not even by getting the /json output. You could put the values you really need in the max. 4 Values of a Dummy Device task (there can be multiple of these), using a "TaskValueSet,taskname,valuename,[var#1]" command, that can be retrieved from the json output or sent to a Controller. It is also possible via rules to send the values directly using a "SendToHttp,ip_or_hostname,port,/url/to/send/data?value=[var#1]" command.
/Ton (PayPal.me)

User avatar
DMike92
Normal user
Posts: 29
Joined: 28 Jul 2020, 02:06
Location: Paris - France

Re: Variables for use in rules??

#16 Post by DMike92 » 01 Aug 2021, 20:12

Ath wrote: 01 Aug 2021, 19:04
DMike92 wrote: 01 Aug 2021, 18:30

Code: Select all

In rules you can use the command let
let,1,10 this will assign the value 10 to %v1%.
- Is there any differences between v and var? %v1% represent [var#1]? Or [v#1]?
- Does these variables must exists somehere (Devices, maybe)?
- Is it possible to get the variable value with a http command (control?cmd=???)
a) There are 3 ways to use a variable set via the 'let,n,value' command: use %vn% where n > 0, so %v1% would be correct, use [var#n], so [var#1] would result in the same output, or use [int#n], with [int#1] resulting in the rounded integer result of the value in that variable you set.
b) These variables are stored in memory only, so putting the device in deepsleep will wipe them out. The currently assigned variables and their values can be viewed on the Tools/System Variables page.
c) No, you can not access them directly, not even by getting the /json output. You could put the values you really need in the max. 4 Values of a Dummy Device task (there can be multiple of these), using a "TaskValueSet,taskname,valuename,[var#1]" command, that can be retrieved from the json output or sent to a Controller. It is also possible via rules to send the values directly using a "SendToHttp,ip_or_hostname,port,/url/to/send/data?value=[var#1]" command.
Thank you very much for your answer :-)
c) If I do the "TaskValueSet,taskname,valuename,[var#1]" command," or anything else, is there a way (via Rules or Devices, or... ?) to get the state of this variable with a http command ?
I'm curently issuing a event,action command, maybe this event can answer the caller with that value ? That would be ideal.

I deal with blinds, so the first this when I open my app (I'm writting the app) is to get the curent status of the blinds (open/close).
Then when I issue a cmd I would like to have this status update and confirmed by the ESP.
Does that makes any sense?

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

Re: Variables for use in rules??

#17 Post by Ath » 01 Aug 2021, 20:23

DMike92 wrote: 01 Aug 2021, 20:12 I'm curently issuing a event,action command, maybe this event can answer the caller with that value ? That would be ideal.
DMike92 wrote: 01 Aug 2021, 20:12 I deal with blinds, so the first this when I open my app (I'm writting the app) is to get the curent status of the blinds (open/close).
Then when I issue a cmd I would like to have this status update and confirmed by the ESP.
Does that makes any sense?
Does ESPEasy control the relays for the blinds too? If so, it would probably be more efficient to use the state of the relay (can easily be reflected using a Switch device on that GPIO pin), and you can get the current state from the http://ip_of_espeasy_device/json url (try it in your browser, or use the Tools/Show JSON button), where you get data like this:
(partial, formatted)

Code: Select all

{
	"TaskValues": [
		{
			"ValueNumber": 1,
			"Name": "State",
			"NrDecimals": 0,
			"Value": 0
		}
	],
	"DataAcquisition": [
		{
			"Controller": 1,
			"IDX": 0,
			"Enabled": "false"
		},
		{
			"Controller": 2,
			"IDX": 0,
			"Enabled": "false"
		},
		{
			"Controller": 3,
			"IDX": 0,
			"Enabled": "false"
		}
	],
	"TaskInterval": 0,
	"Type": "Switch input - Switch",
	"TaskName": "Switch",
	"TaskDeviceNumber": 1,
	"TaskEnabled": "true",
	"TaskNumber": 9
},
/Ton (PayPal.me)

User avatar
DMike92
Normal user
Posts: 29
Joined: 28 Jul 2020, 02:06
Location: Paris - France

Re: Variables for use in rules??

#18 Post by DMike92 » 04 Sep 2021, 12:23

Thanks a lot Ath,
After a second reading, I get everything:)
How can I close this ?

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

Re: Variables for use in rules??

#19 Post by Ath » 04 Sep 2021, 13:00

No need to close, you have now mentioned that your question is answered, that's enough. ;)
/Ton (PayPal.me)

Post Reply

Who is online

Users browsing this forum: No registered users and 25 guests