[Solved][Rules] Dynamic Name - ok for device not for variable

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Bben
New user
Posts: 8
Joined: 17 May 2021, 10:15

[Solved][Rules] Dynamic Name - ok for device not for variable

#1 Post by Bben » 17 May 2021, 10:55

Hi there,
I'm rebuilding my water system on :

espeasy mega-20210503 - stable release / ESPEasy_normal_ESP8266_4M1M
wemos D1 mini
(relay board, PCF8575, ADS1115, OLED SSD1306, TTP223 tactil button, ...)


As devices, I've got 2 dummies :

-1st : manager
* menu | from 0 to 2 -> create a custom menu disply
* selected | from 0 to 3 -> select a zone to be watered
* status |tbd
* duration |tbd

-2nd : duration
* dur_1
* dur_2
* dur_3

I've got 4 switches basic with their states :
* relay_0 | state
* relay_1 | state
* relay_2 | state
* relay_3 | state

reading pin state from PCF8574

and some other switch/button

in rules, the writing :

Code: Select all

[relay_[manager#selected]#state]
give me the state of the selected relay, but :

Code: Select all

[duration#dur_[manager#selected]]
just resul in :

Code: Select all

]
And i'm not able to get my selected duration.

Is there any way to get it works, write it differently, make any modification in the code and rebuild it? preventing from multi if statement.

I've try to search, but I may not have the right keywords...
Last edited by Bben on 17 May 2021, 18:41, edited 1 time in total.

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

Re: [Rules] Dynamic Name - ok for device not for variable

#2 Post by Ath » 17 May 2021, 11:14

You might get away by first assigning that value to an internal variable and use that in the replacement:

Code: Select all

  Let,100,[manager#selected]
  [duration#dur_%v100%]
This will probably work as the %vX% variables are processed before the [task#value] variables, and the first one is working because it (now) searches for the first # to replace, and then continues with the rest.

NB: A bit of tricky code you made there ;) not sure that will keep working if/once we (might) switch the rules engine.
/Ton (PayPal.me)

Bben
New user
Posts: 8
Joined: 17 May 2021, 10:15

Re: [Rules] Dynamic Name - ok for device not for variable

#3 Post by Bben » 17 May 2021, 11:34

Thanks for the trick Ath,

I will test it asap and post back here.


I'm trying to save some space as well as enhance code maintenance/readability.

If I manage to get it work I would'nt do so much update on that kind of system which take care of my plants ;)

For my curiosity, could you point me where is that rules engines, I haven't dig so much in the espeasy code as there is a little too much files (for my too much little time i can spent in it).

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

Re: [Rules] Dynamic Name - ok for device not for variable

#4 Post by Ath » 17 May 2021, 12:27

Best place to start is the documentation: https://espeasy.readthedocs.io/en/lates ... Rules.html
/Ton (PayPal.me)

Bben
New user
Posts: 8
Joined: 17 May 2021, 10:15

Re: [Rules] Dynamic Name - ok for device not for variable

#5 Post by Bben » 17 May 2021, 13:22

thks

I've read it extensively, especially #Referring values for that purpose (even the wiki : https://www.letscontrolit.com/wiki/inde ... rial_Rules)

But nowhere is mentionned wether it's possible to call variable with dynamic name, and don't found any tuto/blog/post/... where it is.

I was surprised/pleased it works in case #1 i've noted above :D

I would be interest by code where string from rules are processed,
retroingeneered code is something pleasant and educationnal, but it's easyer with a starting point!

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

Re: [Rules] Dynamic Name - ok for device not for variable

#6 Post by Ath » 17 May 2021, 13:41

You are pointing to the Wiki, but that is a) no longer updated, and b) quite outdated in places (though that Rules page does still have some quite useful info, and the images obviously haven't been moved over to the new documentation).
New and updated documentation will be placed on RTD (Read The Docs), as that can be maintained from commits in Github, so docs are part of the pull request, most of the time (at least that's what I try to do when making a PR).

The part where you replace a part of a variable name with something else is a bit of a hack, as I wrote, and only partially works, as it is processed from left to right, and not recursive, as would be required to have it working like you thought it would. And my 'inside knowledge' that the %vX% variables are processed before replacing the [task#value] stuff, well, that could have been the other way around as well... But this is the way it works now, and shouldn't be changed too much or many existing rules will break.

Strings are currently only second class citizens in ESPEasy rules, there is some string manipulation possible, f.w. using {substring...} et al, but you can't store a string value in a variable.
/Ton (PayPal.me)

Bben
New user
Posts: 8
Joined: 17 May 2021, 10:15

Re: [Rules] Dynamic Name - ok for device not for variable

#7 Post by Bben » 17 May 2021, 18:40

Ath wrote: 17 May 2021, 11:14 You might get away by first assigning that value to an internal variable and use that in the replacement:

Code: Select all

  Let,100,[manager#selected]
  [duration#dur_%v100%]
Thanks Ath,

Work perfectly unitls rules parsing change

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 39 guests