Send Task value only if it's different from the previous one - Help with Rules

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
iz8mbw
Normal user
Posts: 35
Joined: 14 Apr 2023, 08:43
Location: Napoli, Italy

Send Task value only if it's different from the previous one - Help with Rules

#1 Post by iz8mbw » 22 Jun 2023, 08:27

Hi.
As requested on Github (https://github.com/letscontrolit/ESPEasy/issues/4709) I would like to have the possibility to tell to ESPEasy to send via MQTT the "new" value only if it's different from the previous one, in my case Temperature and Humidity values from a sensor.
For now it's not (yet) supported so I would like to try to have it via Rules (as suggested into Github Issue).

This is the Rule now I set:

Code: Select all

on sht41#Temperature do
  if [var#1]!=%eventvalue1%
    let,1,%eventvalue1%
    TaskValueSet,sht41new,Temperature,%eventvalue1%
    taskrun,sht41new#Temperature
    endif
  endon
  
  on sht41#Humidity do
  if [var#2]!=%eventvalue1%
    let,2,%eventvalue1%
    TaskValueSet,sht41new,Humidity,%eventvalue1%
    taskrun,sht41new#Humidity
  endif
Endon
On the log I can see this:

Code: Select all

1715779: sht41: Temperature: 27.37
1715782: sht41: Humidity: 56.72
1715797: EVENT: sht41#Temperature=27.37
1715808: ACT : let,1,27.37
1715814: ACT : TaskValueSet,sht41new,Temperature,27.37
1715822: ACT : taskrun,sht41new#Temperature
1715834: EVENT: sht41#Humidity=56.72
1715848: ACT : let,2,56.72
1715853: ACT : TaskValueSet,sht41new,Humidity,56.72
1715861: ACT : taskrun,sht41new#Humidity
but I don't have any MQTT output.

I have tried different Rules but when I have it working it looks like the "taskrun" will always send both fields (Temperature and Humidity), I did not found a way to let's send via MQTT only the new values for Temperature and Humidity (separately) when one or both changes.

The only really working is the Rules having two different TASK (two different DUMMY device) like that:

Code: Select all

on sht41#Temperature do
  if [var#1]!=%eventvalue1%
    let,1,%eventvalue1%
    TaskValueSet,sht41temp,Temperature,%eventvalue1%
    taskrun,sht41temp
    endif
  endon
  
  on sht41#Humidity do
  if [var#2]!=%eventvalue1%
    let,2,%eventvalue1%
    TaskValueSet,sht41hum,Humidity,%eventvalue1%
    taskrun,sht41hum
  endif
endon
Can you help me with Rules having only one "Dummy" Device?

Thanks.

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

Re: Send Task value only if it's different from the previous one - Help with Rules

#2 Post by Ath » 22 Jun 2023, 08:48

TaskRun should get only the name (or number) of the task, not the value name attached.
It will send all values to the Controller(s) attached to that task, and also generate events for either each value separate or all values combined, depending on the setting 'Single event with all values'.
/Ton (PayPal.me)

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

Re: Send Task value only if it's different from the previous one - Help with Rules

#3 Post by TD-er » 22 Jun 2023, 08:52

If you need to distinguish per value, you need 2 dummy tasks.

Just to explain the concepts and data-flow in ESPEasy...

A plugin: Piece of code to interact with some hardware like a sensor.
A task: An instance of a plugin. So you can have multiple instances of the same plugin, for example for having series of 18b20 temperature sensors.

To take a sample, the internal function PLUGIN_READ is called.
When successful, it will forward all the task values to any of the connected controllers of that task and generate event(s).
This can either be a single event with all task values attached to it (accessible via %eventvalue1%...%eventvalue4%) or an event per task value, depending on the checkbox whether all taskvalues should be sent in a single event.

Now back to the "task sending to controller(s)" part.
You put a value in the dummy task using taskvalueset.
The command "taskrun" triggers this task to call the function PLUGIN_READ, which will forward all taskvalues of that task to the controller(s) linked to that task.

Thus from the (dummy)task perspective, it is just flushing all task values to the controller(s).

So if you need to filter per task value, you either need multiple dummy tasks, or you can format your own MQTT message in those same rules.
You can directly interact with the (first enabled) MQTT controller using the command "publish".
N.B. To format the arguments, you can use system variables.
See: https://espeasy.readthedocs.io/en/lates ... nt-command

See here for documentation on eventvalues: https://espeasy.readthedocs.io/en/lates ... eventvalue
You could even use %eventname% or %eventpar% to use the original taskname or taskvaluename in the topic.
See: https://espeasy.readthedocs.io/en/lates ... r-eventpar

iz8mbw
Normal user
Posts: 35
Joined: 14 Apr 2023, 08:43
Location: Napoli, Italy

Re: Send Task value only if it's different from the previous one - Help with Rules

#4 Post by iz8mbw » 22 Jun 2023, 10:56

Thanks all for answers and clarifications.
So since "TaskRun" only get the name of the task and not the value name attached I created 2 dummy tasks.
More, since I wants values in MQTT but I want also the same MQTT Topic (example: espeasy_2/sht/) I disabled the "Send to Controller" (MQTT) feature and I managed the MQTT Publish messages via Rules.

This is my final Rule that Publish MQTT messages on the same Topic and covering to send values only if them are different from the previous ones read from the sensor:

Code: Select all

on shtpre#Temperature do
  if [var#1]!=%eventvalue1%
    let,1,%eventvalue1%
    TaskValueSet,sht35temp,Temperature,%eventvalue1%
    taskrun,sht35temp
    Publish,%sysname%/sht/Temperature,%eventvalue1%
    endif
  endon
  
  on shtpre#Humidity do
  if [var#2]!=%eventvalue1%
    let,2,%eventvalue1%
    TaskValueSet,sht35hum,Humidity,%eventvalue1%
    taskrun,sht35hum
    Publish,%sysname%/sht/Humidity,%eventvalue1%
  endif
endon
Just another question about MQTT publishing in Rules

Code: Select all

Publish,%sysname%/sht/Temperature,%eventvalue1%
is there a way to enable the RETAIN flag for "Publish" in Rules messages?
Last edited by iz8mbw on 22 Jun 2023, 13:16, edited 1 time in total.

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

Re: Send Task value only if it's different from the previous one - Help with Rules

#5 Post by TD-er » 22 Jun 2023, 11:14

It takes the 'Publish retain flag' setting from the controller settings.
But that's an all retained or none retained, so I can see why it might be useful to have a variant of the publish command where this is optional.

However it currently isn't possible in the current code to set the retained flag per publish command.

iz8mbw
Normal user
Posts: 35
Joined: 14 Apr 2023, 08:43
Location: Napoli, Italy

Re: Send Task value only if it's different from the previous one - Help with Rules

#6 Post by iz8mbw » 22 Jun 2023, 13:17

Many thanks!

Post Reply

Who is online

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