is there something like a procedure or subroutine available ?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Stef2s
Normal user
Posts: 10
Joined: 25 Mar 2024, 20:01

is there something like a procedure or subroutine available ?

#1 Post by Stef2s » 01 Apr 2024, 13:12

hello,

I guess the answer is No, but I could be mistaken as there's quiet a lot of information to handle for a beginner.

But if not, could it not be possible to use rules2 .. rules4 as a procedure, like "call Rules2" ?

For my case I'm developing a ventilation system, where you can switch on/off the ventilation in every room. Depending on which rooms need to be ventilated, and the CO2 levels in those rooms, I need to set the total ventilation speed. So after any button change, I've to check all the buttons and then calculate the total needed ventilation flow. Yes i know this sounds more like a complete home automation system, but I've the feeling it could well be done completely within EspEasy.

Another idea would be to write dedicated plugin for my special case.

How are other people solving these kind of problems ?

cheers, Stef

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

Re: is there something like a procedure or subroutine available ?

#2 Post by TD-er » 01 Apr 2024, 13:32

Rules blocks act on events.
So you can consider such an event block as a function.
The function arguments are like eventvalues.

An event is like a string, where the event name and the eventvalues are separated by an =
The event values are separated by a comma or a space.
So if you need to have either one (comma or space) in an argument, you can wrap the argument in quotes.

For example to send an event via rules (or via sendto to another node):

Code: Select all

event,MyEvent=1,2,"This is an example"
This can be seen like the arguments to set a text on a display like line 1, column 2, text...

When a task has a new value, it will also send out an event with taskname#taskvaluename=123
Or if you have checked the checkbox to send it all in a single event: taskname#All=123,456,789

The eventvalues can be referred to in the rules using %eventvalue1%, %eventvalue2%, etc

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

Re: is there something like a procedure or subroutine available ?

#3 Post by TD-er » 01 Apr 2024, 13:36

Oh and you can just refer to other taskvalues using the [...#...] notation.
[taskname#taskvaluename]

If you need to perform calculations, you can use the command "let" which will store the result in a variable
Like: let,1,123+[foo#bar]
This will store the result in var#1 which can be referred to using [var#1]

You can also set a value as a taskvalue to a dummy task using the taskvalueset command.
Taskvalues will be kept in RTC which will be restored at reboot as long as the ESP remains powered.
So values stored in a dummy task can survive a crash or reboot.

Stef2s
Normal user
Posts: 10
Joined: 25 Mar 2024, 20:01

Re: is there something like a procedure or subroutine available ?

#4 Post by Stef2s » 01 Apr 2024, 13:40

GREAT !!
thanks very much TD-er

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

Re: is there something like a procedure or subroutine available ?

#5 Post by Ath » 01 Apr 2024, 14:12

Stef2s wrote: 01 Apr 2024, 13:12 But if not, could it not be possible to use rules2 .. rules4 as a procedure, like "call Rules2" ?
The separate Rules1..Rules4 files are processed as being a single file (if they exist), so no need to 'switch' to another set of rules.
To speed up the processing, once a rule matches with the event, the rule is executed, and after that, processing is stopped. So when having multiple handlers, you will need to place the most specific handler first, and more generic handlers later/last:

Code: Select all

On Switch#State=1 Do // Very specific handler, will only handle 'event,Switch#State=1', (with possibly extra arguments)
// actions
Endon

On Switch#State Do // Less specific handler, will f.e. handle 'event,Switch#State=0' or 'event,Switch#State' (no arguments)
// Actions
Endon

On Switch Do // Most generic handler for Switch event, will handle 'event,Switch' and 'event,Switch=1' and 'event,Switch#All=1' etc.
// Actions
Endon
NB: This is also mentioned in the documentation
Stef2s wrote: 01 Apr 2024, 13:12 Another idea would be to write dedicated plugin for my special case.
That's usually not needed, most of "my special case"s aren't so special after all, there are a lot of users with very similar 'requirements' :lol:
Having dedicated plugins like that is usually more a limitation then an enhancement, so you might end up being the only user either understanding how it works (or is intended to) or that can cope with the limitations.
/Ton (PayPal.me)

Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests