Page 1 of 1

Tips for shortening rules

Posted: 10 Sep 2018, 18:02
by Prutsium
Hello all,

I am looking for some help to be able to shorten the below rule as when i would add all the tags i require i will exceed the character limit from the rules.
This is for a small project with RFID tags to open a door by various users.

The idea is that each tag sends a "Text" string and if the Tag matches it activates the Timer,1,5 so that the relay stays On for 5 seconds and after that the relay is set to off.

What is below is working so thats no issue it's a matter of cleaning up

Code: Select all

On RFID#Tag do
if [RFID#Tag]=1899267376
gpio,13,0
gpio,14,1
publish domoticz/in,{"command": "switchlight", "idx": 125, "switchcmd": "On" }
publish domoticz/in,{"idx":124,"nvalue":0,"svalue":"Card"}
timerSet,1,5

if [RFID#Tag]=2271020827 // Jerome
gpio,13,0
gpio,14,1
publish domoticz/in,{"command": "switchlight", "idx": 125, "switchcmd": "On" }
publish domoticz/in,{"idx":124,"nvalue":0,"svalue":"Tag"}
timerSet,1,5
endif
endon

On Rules#Timer=1 do 
gpio,13,1
gpio,14,0
publish domoticz/in,{"command": "switchlight", "idx": 125, "switchcmd": "Off" }
endon
Thanks for help :)

Re: Tips for shortening rules

Posted: 10 Sep 2018, 18:11
by grovkillen
How many tags are we talking about?

Re: Tips for shortening rules

Posted: 10 Sep 2018, 18:13
by grovkillen
If we could use multiple eventvalues we could shorten the rule some. I'd like you to add that as a suggestion on GitHub. Or maybe even better a array variable that we could use in rules! :)

Re: Tips for shortening rules

Posted: 10 Sep 2018, 18:33
by Prutsium
grovkillen wrote: 10 Sep 2018, 18:11 How many tags are we talking about?
In the range of 15-20 tags (As it act as a central door opener)

Re: Tips for shortening rules

Posted: 10 Sep 2018, 18:36
by Prutsium
grovkillen wrote: 10 Sep 2018, 18:13 If we could use multiple eventvalues we could shorten the rule some. I'd like you to add that as a suggestion on GitHub. Or maybe even better a array variable that we could use in rules! :)
Would it be possible within a rule to activate for example Rule2 or a sort of script?

As then i could move the Gpio and send to Domoticz ON to a rule / script and would cleanup the most already a bit.
Since the Bold commands could move out then:
On RFID#Tag do
if [RFID#Tag]=1899267376
gpio,13,0
gpio,14,1
publish domoticz/in,{"command": "switchlight", "idx": 125, "switchcmd": "On" }

publish domoticz/in,{"idx":124,"nvalue":0,"svalue":"Card"}
timerSet,1,5

Re: Tips for shortening rules

Posted: 10 Sep 2018, 18:49
by grovkillen
Maybe we could have a rule chunk replacement similar to values... Hmm, I'm thinking out loud now. I need to discuss it with @TD-er

Re: Tips for shortening rules

Posted: 02 Oct 2018, 10:52
by marion9
Very useful thread, you raised important question!

Re: Tips for shortening rules

Posted: 02 Oct 2018, 13:07
by TD-er
At company level security, it is customary to have a central point where authorization and authentication is configured.
Often this is done via some kind of Active Directory or something similar.
So I guess it would make sense to create a query somehow to ask if some key is valid for that access.

It may be an issue that this will introduce some delay and doesn't work when the wifi is offline or the server you would send your query to.
So this is a trade-off for ease of access management and availability.

Another detail is that we currently have no user management system, nor an ActiveDirectory component in ESPeasy.
Maybe we can add an "Active Directory" like plugin to which you can send a value and get a true/false response.

The easiest work-around for now is to have a set of lines like this and use that to set a value in a Dummy parameter

Code: Select all

if [RFID#Tag]=1899267376
Then use that dummy to trigger the rule for those with access.
These "check for access" rules can be very short.

You also have up-to 4 rules files in which you can store rules.

Re: Tips for shortening rules

Posted: 02 Oct 2018, 14:19
by toffel969
Hi

Maybe a small improvement only,but I think you could already shorten the rules quite a bit.

Code: Select all


On [RFID#Tag]=1899267376 do
gpio,13,0
gpio,14,1
event,open
Endon

On [RFID#Tag]=2271020827 // Jerome
gpio,13,0
gpio,14,1
event,open
endon

On Rules#Timer=1 do 
gpio,13,1
gpio,14,0
publish domoticz/in,{"command": "switchlight", "idx": 125, "switchcmd": "Off" }
endon

On open do
timerSet,1,5
publish domoticz/in,{"command": "switchlight", "idx": 125, "switchcmd": "On" }
publish domoticz/in,{"idx":124,"nvalue":0,"svalue":"Card"}
Endon

Thanks for help :)
[/quote]

Re: Tips for shortening rules

Posted: 02 Oct 2018, 20:47
by budman1758

Code: Select all

on rfid#tag do
if [rfid#tag]=1626454649  //joe blo
Event,goodtag
if [rfid#tag]=1626454648  //john doe
Event,goodtag
if [rfid#tag]=1626454647  // etc
Event,goodtag
if [rfid#tag]=1626454646  //etc
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
if [rfid#tag]=1626454649
Event,goodtag
if [rfid#tag]=1626454648
Event,goodtag
if [rfid#tag]=1626454647
Event,goodtag
if [rfid#tag]=1626454646
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
if [rfid#tag]=1626454649
Event,goodtag
if [rfid#tag]=1626454648
Event,goodtag
if [rfid#tag]=1626454647
Event,goodtag
if [rfid#tag]=1626454646
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
if [rfid#tag]=1626454649
Event,goodtag
if [rfid#tag]=1626454648
Event,goodtag
if [rfid#tag]=1626454647
Event,goodtag
if [rfid#tag]=1626454646
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
endif
endon

On goodtag do
Whatever you are doing...
endon
This takes less than 1 page of rules. I don't know if its "efficient" but it does work.
I use something similar in my keypad rules to determine what to do with the scan code output.

Re: Tips for shortening rules

Posted: 02 Oct 2018, 21:37
by grovkillen
Cool budman! Didn't know the rules engine could parse that many ifs.

Re: Tips for shortening rules

Posted: 02 Oct 2018, 22:14
by budman1758
grovkillen wrote: 02 Oct 2018, 21:37 Cool budman! Didn't know the rules engine could parse that many ifs.
To be honest I dd not try the above but in my keypad setup it does the 12 possibles that the keypad puts out. I would imagine that there is some upper limit but have no idea what it would be.
As a side note the speed problems I was having a log time ago are pretty much gone. Rules parsing speed is WAYYYYYYYY better than it was.

Re: Tips for shortening rules

Posted: 02 Oct 2018, 22:51
by Prutsium
budman1758 wrote: 02 Oct 2018, 20:47

Code: Select all

on rfid#tag do
if [rfid#tag]=1626454649  //joe blo
Event,goodtag
if [rfid#tag]=1626454648  //john doe
Event,goodtag
if [rfid#tag]=1626454647  // etc
Event,goodtag
if [rfid#tag]=1626454646  //etc
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
if [rfid#tag]=1626454649
Event,goodtag
if [rfid#tag]=1626454648
Event,goodtag
if [rfid#tag]=1626454647
Event,goodtag
if [rfid#tag]=1626454646
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
if [rfid#tag]=1626454649
Event,goodtag
if [rfid#tag]=1626454648
Event,goodtag
if [rfid#tag]=1626454647
Event,goodtag
if [rfid#tag]=1626454646
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
if [rfid#tag]=1626454649
Event,goodtag
if [rfid#tag]=1626454648
Event,goodtag
if [rfid#tag]=1626454647
Event,goodtag
if [rfid#tag]=1626454646
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
endif
endon

On goodtag do
Whatever you are doing...
endon
This takes less than 1 page of rules. I don't know if its "efficient" but it does work.
I use something similar in my keypad rules to determine what to do with the scan code output.
Hmmmm thats a nice one ... i lose only still a lot of date as i want to send to Domoticz who comes in so each line of your version would still have:
publish domoticz/in,{"idx":124,"nvalue":0,"svalue":"Name of Person"}
(If only dummy sensors could hold text then i could reduce about 50% of the text :) )

But .... it saves quite a bunch with your idea will try this later this week.

Thanks for the tip!

Re: Tips for shortening rules

Posted: 03 Oct 2018, 01:41
by budman1758
Prutsium wrote: 02 Oct 2018, 22:51
budman1758 wrote: 02 Oct 2018, 20:47

Code: Select all

on rfid#tag do
if [rfid#tag]=1626454649  //joe blo
Event,goodtag
if [rfid#tag]=1626454648  //john doe
Event,goodtag
if [rfid#tag]=1626454647  // etc
Event,goodtag
if [rfid#tag]=1626454646  //etc
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
if [rfid#tag]=1626454649
Event,goodtag
if [rfid#tag]=1626454648
Event,goodtag
if [rfid#tag]=1626454647
Event,goodtag
if [rfid#tag]=1626454646
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
if [rfid#tag]=1626454649
Event,goodtag
if [rfid#tag]=1626454648
Event,goodtag
if [rfid#tag]=1626454647
Event,goodtag
if [rfid#tag]=1626454646
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
if [rfid#tag]=1626454649
Event,goodtag
if [rfid#tag]=1626454648
Event,goodtag
if [rfid#tag]=1626454647
Event,goodtag
if [rfid#tag]=1626454646
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
endif
endon

On goodtag do
Whatever you are doing...
endon
This takes less than 1 page of rules. I don't know if its "efficient" but it does work.
I use something similar in my keypad rules to determine what to do with the scan code output.
Hmmmm thats a nice one ... i lose only still a lot of date as i want to send to Domoticz who comes in so each line of your version would still have:
publish domoticz/in,{"idx":124,"nvalue":0,"svalue":"Name of Person"}
(If only dummy sensors could hold text then i could reduce about 50% of the text :) )

But .... it saves quite a bunch with your idea will try this later this week.

Thanks for the tip!
Write the tag number to a dummy value. Send the tag number to Domoticz and let Domoticz figure out who the tag belongs to. (maybe?)

Re: Tips for shortening rules

Posted: 03 Oct 2018, 10:57
by Prutsium
budman1758 wrote: 03 Oct 2018, 01:41
Prutsium wrote: 02 Oct 2018, 22:51
budman1758 wrote: 02 Oct 2018, 20:47

Code: Select all

on rfid#tag do
if [rfid#tag]=1626454649  //joe blo
Event,goodtag
if [rfid#tag]=1626454648  //john doe
Event,goodtag
if [rfid#tag]=1626454647  // etc
Event,goodtag
if [rfid#tag]=1626454646  //etc
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
if [rfid#tag]=1626454649
Event,goodtag
if [rfid#tag]=1626454648
Event,goodtag
if [rfid#tag]=1626454647
Event,goodtag
if [rfid#tag]=1626454646
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
if [rfid#tag]=1626454649
Event,goodtag
if [rfid#tag]=1626454648
Event,goodtag
if [rfid#tag]=1626454647
Event,goodtag
if [rfid#tag]=1626454646
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
if [rfid#tag]=1626454649
Event,goodtag
if [rfid#tag]=1626454648
Event,goodtag
if [rfid#tag]=1626454647
Event,goodtag
if [rfid#tag]=1626454646
Event,goodtag
if [rfid#tag]=1626454645
Event,goodtag
endif
endon

On goodtag do
Whatever you are doing...
endon
This takes less than 1 page of rules. I don't know if its "efficient" but it does work.
I use something similar in my keypad rules to determine what to do with the scan code output.
Hmmmm thats a nice one ... i lose only still a lot of date as i want to send to Domoticz who comes in so each line of your version would still have:
publish domoticz/in,{"idx":124,"nvalue":0,"svalue":"Name of Person"}
(If only dummy sensors could hold text then i could reduce about 50% of the text :) )

But .... it saves quite a bunch with your idea will try this later this week.

Thanks for the tip!
Write the tag number to a dummy value. Send the tag number to Domoticz and let Domoticz figure out who the tag belongs to. (maybe?)
The part of Tag ID > Name is an idea yeah but makes it bit more tricky too.
Anyway with your idea i save already alot of space so thats gonna help.

Here is my version of it (short test version now still)

Code: Select all

On RFID#Tag do
if [RFID#Tag]=1899267376
publish domoticz/in,{"idx":124,"nvalue":0,"svalue":"Card 1"}
Event,OkTag

if [RFID#Tag]=82431090
publish domoticz/in,{"idx":124,"nvalue":0,"svalue":"Card 2"}
Event,OkTag
endon
endif

On OkTag do
gpio,13,0
gpio,14,1
timerSet,1,5
endon

On Rules#Timer=1 do 
gpio,13,1
gpio,14,0
endon
So if a tag is scanned it triggers OkTag and OkTag has timer to keep lock unlocked for 5 seconds and switches all off after that.

Again thanks for your help!

Re: Tips for shortening rules

Posted: 04 Oct 2018, 20:17
by tozett
great ideas, to solve rfid-keyopen with rules.
did you ever thought of time-slots, on which some key is only valid?
i send tag-id to my gira-homeserver, wich checks if the key comes in on an allowed day.
and i check, if the key comes in at daytime (for parcel-delivery) and some key-groups are forbidden at night-time.
only some ideas for more rules...maybe..

Re: Tips for shortening rules

Posted: 08 Oct 2018, 09:29
by Prutsium
tozett wrote: 04 Oct 2018, 20:17 great ideas, to solve rfid-keyopen with rules.
did you ever thought of time-slots, on which some key is only valid?
i send tag-id to my gira-homeserver, wich checks if the key comes in on an allowed day.
and i check, if the key comes in at daytime (for parcel-delivery) and some key-groups are forbidden at night-time.
only some ideas for more rules...maybe..
I did not implement timeslots or additional checks as this ESP must run without Internet connection (read: its mounted in a dead zone of my wifi)
When i get some coverage in that area (it's actually about 100m away from my own entry) i for sure want to work with additional checks but it creates also an extra point of failure.

Further within the rules i assume it would be possible to work with timeslots so this might also be an area to play with but i needed to compress my rules due the fact i had to add about 30 key;s and with my original rules it would have never fitted :)