Rule <action> statement, to read and publish device value?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
User avatar
uxhamby
Normal user
Posts: 132
Joined: 29 Dec 2016, 18:13
Location: Toronto Canada

Rule <action> statement, to read and publish device value?

#1 Post by uxhamby » 31 Mar 2023, 22:43

Hi,

Ok so, I have a single "Environment - DS18b20" device named 'Ambient' with a value called "Temperature".

What <action> statement can I put in a rule, to read and publish the Temperature value when the rule event executes?

I am aware that I can create a rule trigger event like:

Code: Select all

on Clock#Time=All,**:30 do //will run half past every hour
To trigger an event at a specific time but I seem to be lacking knowledge on how to format the subsequent <action> statement, to read and publish the Temperature value prevailing at that moment.

Code: Select all

on Ambient#Temperature do 
      Publish,UxStuff/%sysname%/,"%lcltime%, %eventvalue%,C, Htub"
endon
will publish the Temperature value based on each polling interval, but I want the temperature to be read and published triggered by my rule event.

Is this possible? Assistance / advice please.

Thanks,

Brian H.

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

Re: Rule <action> statement, to read and publish device value?

#2 Post by TD-er » 31 Mar 2023, 23:04

You can call TaskRun to trigger the task to take a sample.

When a new value is available, the task will send out an event with the new value and also send the new value(s) to the linked controllers of that task.

Since you want to create your own MQTT publish call, I guess you simply have to act on the event of the new value.

By default, a task will send out a single event per task value.
Thus an event like this:

Code: Select all

Ambient#Temperature=23.4
If you check the "Single event with all values:", the event will be something like this: (assuming you have 2 task values for this task)

Code: Select all

Ambient#All=23.4,24.0
The event value(s) can be used in the rules via %eventvalue1% ... %eventvalueN% for N being the nr. of event values present in your event.
See: https://espeasy.readthedocs.io/en/lates ... tem-events

User avatar
uxhamby
Normal user
Posts: 132
Joined: 29 Dec 2016, 18:13
Location: Toronto Canada

Re: Rule <action> statement, to read and publish device value?

#3 Post by uxhamby » 01 Apr 2023, 02:33

Thanks.

Brian H.

User avatar
uxhamby
Normal user
Posts: 132
Joined: 29 Dec 2016, 18:13
Location: Toronto Canada

Re: Rule <action> statement, to read and publish device value?

#4 Post by uxhamby » 01 Apr 2023, 04:31

Is the use of the 'runtask' command documented more comprehensively somewhere? I seem to be missing the boat on getting the temperature from %eventvalue% in all my attempts at using it so far.

Thanks,

Brian H.

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

Re: Rule <action> statement, to read and publish device value?

#5 Post by Ath » 01 Apr 2023, 09:27

uxhamby wrote: 01 Apr 2023, 04:31 Is the use of the 'runtask' command documented more comprehensively somewhere? I seem to be missing the boat on getting the temperature from %eventvalue% in all my attempts at using it so far.
The actual command is TaskRun, not runtask... that should improve the score on search-results ;) (scroll down a bit, can't link to it directly)

It all depends on wether you have enabled "Single event with all values:" or not (by default that is not enabled), resulting in an event to be generated for Ambient#Temperature or Ambient#All. (as stated above)
To use the value from that event you will need to add an eventhandler in rules like this (assuming the default for "Single event with all values:", being disabled)

Code: Select all

On Ambient#Temperature Do
  Publish,your/mqtt/topic,%eventvalue1%
Endon
For this event there is only 1 value appended to the event, like: Ambient#Temperature=23.4
For sensors that have multiple Values available, a separate event for each Value will be generated, each with only their own current value included

If you have ENabled "Single event with all values:", and possibly connected multiple DS18b20 sensors to the same GPIO (up to 4 are supported per task for this plugin), you could handle these 4 temperature readings in a single eventhandler (aka: rule).
The generated event (with 4 sensors configured) could look like this: Ambient#All=23.4,21.5,16.8,10.5

Code: Select all

On Ambient#All Do
  Publish,your/mqtt/topic/living,%eventvalue1%
  Publish,your/mqtt/topic/kitchen,%eventvalue2%
  Publish,your/mqtt/topic/hallway,%eventvalue3%
  Publish,your/mqtt/topic/outside,%eventvalue4%
Endon
NB: These events will be generated at each Interval, except when that is set to 0 (when allowed, not for the DS18b20), then only an initial event is generated.
/Ton (PayPal.me)

User avatar
uxhamby
Normal user
Posts: 132
Joined: 29 Dec 2016, 18:13
Location: Toronto Canada

Re: Rule <action> statement, to read and publish device value?

#6 Post by uxhamby » 01 Apr 2023, 17:22

Here is what I am using:

Code: Select all

on Clock#Time=All,**:*7 do //will run every 10 minutes
  taskrun,Ambient
  On Ambient#Temperature
    Publish,UxStuff/%sysname%/,"%lcltime%,%eventvalue% , C"
    endon
Endon
And here is what I see getting published:

Code: Select all

2023-04-01 11:17:00,Sat , C
Where am I going wrong?

User avatar
uxhamby
Normal user
Posts: 132
Joined: 29 Dec 2016, 18:13
Location: Toronto Canada

Re: Rule <action> statement, to read and publish device value?

#7 Post by uxhamby » 01 Apr 2023, 17:26

FWIW, my version info on this unit is as follows:

Code: Select all

Firmware
Build:⋄	20230306 - Mega
System Libraries:⋄	ESP82xx Core 2843a5ac, NONOS SDK 2.2.2-dev(38a443e), LWIP: 2.1.2 PUYA support
Git Build:⋄	mega-20230306
Plugin Count:⋄	47 [Normal]
Build Origin:	GitHub Actions
Build Time:⋄	Mar 6 2023 20:35:41
Binary Filename:⋄	ESP_Easy_mega_20230306_normal_ESP8266_4M1M
Build Platform:⋄	Linux-5.15.0-1033-azure-x86_64-with-glibc2.35
Git HEAD:⋄	HEAD_2c4500
Thanks,

Brian H.

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

Re: Rule <action> statement, to read and publish device value?

#8 Post by TD-er » 01 Apr 2023, 17:40

Don't use "nested" on...do...endon stuff.

You also missed a "do"

Code: Select all

on Clock#Time=All,**:*7 do //will run every 10 minutes
  taskrun,Ambient
Endon

On Ambient#Temperature do
    Publish,UxStuff/%sysname%/,"%lcltime%,%eventvalue% , C"
Endon

User avatar
uxhamby
Normal user
Posts: 132
Joined: 29 Dec 2016, 18:13
Location: Toronto Canada

Re: Rule <action> statement, to read and publish device value?

#9 Post by uxhamby » 01 Apr 2023, 19:48

Perfect, thanks!

Brian H.

Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests