UDP and MQTT but with different Intervals?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Affinite
Normal user
Posts: 57
Joined: 03 Apr 2018, 18:46

UDP and MQTT but with different Intervals?

#1 Post by Affinite » 24 Feb 2021, 17:22

I have a ESP8266NodeMCU happily reading a voltage from an ADS1115 ADC
I send %value% to a UDP Controller and then out on the local LAN. I display the result in a Node Red dashboard
I'd like to send this value as frequently as possible so Ive set the Device message interval to 1 second.
So far so good

I'd also like to send the same %value% to a public MQTT broker but I only every 10 minutes

I can send %value% to both ESPEASY controllers but I cant figure out a way to get the controllers to send out/publish at different intervals.
Is there a way to do this ?
Is there a way for the "Publish" command to specify a specific controller so I could do it in a Rule ?

BTW - I can (sort of) send data from the 2 controllers at different intervals but only by creating 2 identical devices (one for each controller)
The first device sends to the UDP controller every 1 sec delay and the other one publishes to MQTT every 3600 seconds
Not very elegant !

Hope this makes sense - any guidance appreciated

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

Re: UDP and MQTT but with different Intervals?

#2 Post by Ath » 24 Feb 2021, 18:11

The simplest way to achieve this would be to start a looptimer on the MQTT#Connected event set to 600 seconds (10 min.) that uses the publish command:

Code: Select all

on mqtt#connected do
  LoopTimerSet,1,600
  asyncevent,sendvaluestomqtt
endon

on sendvaluesmqtt do
  Publish,topic/to/send/value,[task#value]
endon

// Code provided by jgrad:
On Rules#Timer=1 do
 asyncevent,sendvaluestomqtt
endon
Edit: Added the timer event handler.
Last edited by Ath on 24 Feb 2021, 20:27, edited 1 time in total.
/Ton (PayPal.me)

jgrad
Normal user
Posts: 92
Joined: 29 Aug 2016, 22:03
Location: Slovenia

Re: UDP and MQTT but with different Intervals?

#3 Post by jgrad » 24 Feb 2021, 18:54

what about solution to (under Devices) define 2 device tasks, both connected to same ADS HW, but each with different interval and sending data to different controller?

above proposal with rules send values only once - I think there is missing code to send data everytime looptimer1 generate event

Code: Select all

On Rules#Timer=1 do
 asyncevent,sendvaluestomqtt
endon

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

Re: UDP and MQTT but with different Intervals?

#4 Post by Ath » 24 Feb 2021, 20:11

Ah, yes, I got interrupted while typing that up from the tablet. Good call.

Edit: I updated the script with the timer event handler.
/Ton (PayPal.me)

Affinite
Normal user
Posts: 57
Joined: 03 Apr 2018, 18:46

Re: UDP and MQTT but with different Intervals?

#5 Post by Affinite » 24 Feb 2021, 22:10

Ath & jgrad
I tried using Publish on a timer in Rules but the problem I had was that the Publish command only appears to work if the MQTT controller is enabled.
If the MQTT controller is enabled it sends values at a frequency determined by "Interval" in Devices.
ie Interval setting in Devices effectively overrides Publish/Timer in Rules
Catch 22 ?

Thanks for helping

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

Re: UDP and MQTT but with different Intervals?

#6 Post by Ath » 24 Feb 2021, 22:31

You should disable the MQTT Controller from the Task configuration (Devices tab), but leave the Controller itself enabled (Controllers tab), then it should not send out via MQTT.
/Ton (PayPal.me)

Affinite
Normal user
Posts: 57
Joined: 03 Apr 2018, 18:46

Re: UDP and MQTT but with different Intervals?

#7 Post by Affinite » 24 Feb 2021, 23:26

Ath wrote: 24 Feb 2021, 22:31 You should disable the MQTT Controller from the Task configuration (Devices tab), but leave the Controller itself enabled (Controllers tab), then it should not send out via MQTT.
OK Thanks
I'll have another go tomorrow.

Affinite
Normal user
Posts: 57
Joined: 03 Apr 2018, 18:46

Re: UDP and MQTT but with different Intervals?

#8 Post by Affinite » 24 Feb 2021, 23:47

Ath wrote: 24 Feb 2021, 22:31 You should disable the MQTT Controller from the Task configuration (Devices tab), but leave the Controller itself enabled (Controllers tab), then it should not send out via MQTT.
OK I couldn't resist a quick test before updating the Rules

Your suggested configuration does stop MQTT being sent out but it seems that the Publish command is disabled also

To test Publish I typed "Publish #,#" into the Command box in Tools Tab
With the MQTT Controller Enabled and Devices, Send to controller #1 (The MQTT controller) disabled I do not get MQTT message out
With the MQTT Controller Enabled and Devices, Send to controller #1 (The MQTT controller) enabled I do get a MQTT message out

Time for bed and hope I can get my head around this tomorrow.
Thanks once again for taking the time to help

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

Re: UDP and MQTT but with different Intervals?

#9 Post by Ath » 25 Feb 2021, 08:01

The Publish command sends out to the first enabled MQTT Controller, independent of any 'Send to Controller' checkbox on the Devices pages.
/Ton (PayPal.me)

Affinite
Normal user
Posts: 57
Joined: 03 Apr 2018, 18:46

Re: UDP and MQTT but with different Intervals?

#10 Post by Affinite » 25 Feb 2021, 14:56

Ath wrote: 25 Feb 2021, 08:01 The Publish command sends out to the first enabled MQTT Controller, independent of any 'Send to Controller' checkbox on the Devices pages.
Are you sure ?
Unfortunately mine doesn't.
I've tried again with a very simple setup with only 1 device but Publish only sends out a message when the MQTT controller is selected in Devices/Tasks/Send to Controller

Even with your kind assistance I dont seem to be able make this work so I think I will have to abandon and opt for 2 Tasks per Device
(1 sending MQTT and 1 sending UDP at different intervals)
Thanks again

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

Re: UDP and MQTT but with different Intervals?

#11 Post by Ath » 25 Feb 2021, 15:26

Can you show screenshots of the Controller page for your MQTT controller, and the Device page for your task?

The Publish command is not specific to a task, but you send a value to a MQTT topic, recognized by the receiver.

Please also show the rules/command you used to test this so far.
/Ton (PayPal.me)

Affinite
Normal user
Posts: 57
Joined: 03 Apr 2018, 18:46

Re: UDP and MQTT but with different Intervals?

#12 Post by Affinite » 25 Feb 2021, 16:26

I really appreciate you sticking with me on this.

Hope the screenshots are adequate.
For obvious security reasons I've scrubbed out (?????) the first part of my Publish and Subscribe Topic on the screenshots
On my MQTT client I subscribe to ?????/#

The behaviour is exactly as I said earlier.
When I type (in the ESPEASY Tools command box)

Publish #,#

I do not get a message on the Hive public MQTT broker unless Controller 1 is ticked in the Device (Task 1) Data Acquisition tab

I have cleared any rules I set in order to simplify the test
Attachments
Controllers.png
Controllers.png (45.07 KiB) Viewed 7929 times
Controller 1 top.png
Controller 1 top.png (53.67 KiB) Viewed 7929 times
Device 1 top.png
Device 1 top.png (33.44 KiB) Viewed 7929 times
Command.png
Command.png (24.63 KiB) Viewed 7929 times
Device 1.png
Device 1.png (34.4 KiB) Viewed 7929 times
Devices.png
Devices.png (94.01 KiB) Viewed 7929 times
Controller 1 bottom.png
Controller 1 bottom.png (80.09 KiB) Viewed 7929 times

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

Re: UDP and MQTT but with different Intervals?

#13 Post by TD-er » 25 Feb 2021, 16:49

I'm not sure if you should use the loop timer, or maybe act on the event sent when a new value is being read.
N.B. Both use cases may be valid, but you may need to know their differences.

A task set to run at interval N may not always run exactly at interval N, but it may take slightly longer to get rescheduled.
This depends on the plugin used for that task (e.g. BME280 is known to drift, still have to look into that)

The loop timer however does loop with a very constant interval. It may start slightly late on a loop, but the rescheduled one is based on the last scheduled time so (almost) no drift.

If you collect the data based on the loop timer, then this timer will collect the latest sample and process it at a constant interval.
But this may not be the N'th sample.
So it depends on the use case which approach is best.

N.B. you can also call a taskrun command from the loop timer to execute a task and thus send data to the connected controller on a given interval.



There is also a number of alternative ways to achieve a decimation of the number of messages.
You could add a dummy task in which you copy your values and the call "taskrun" on that dummy task.
Any connected values of that dummy task will then be sent to the connected controller.
By keeping track of a timer in the rules (see variables in rules) you can decimate when to copy what values to the dummy task and call taskrun on it.
N.B. this way you can share a single dummy task for a number of values to collect and send as the values are kept in the prepared messages queued in the controller queue.

Another way (less elegant and a bit tricky to use on multiple tasks) is to set the minimal send interval of a controller.
If you only need to send values from a single task, this may be the simplest way to handle this.
Just set the minimal send interval to 10000 msec and "delete oldest" for when the queue is full.
Maybe also set the queue length to 1 and no retries.

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

Re: UDP and MQTT but with different Intervals?

#14 Post by Ath » 25 Feb 2021, 16:59

I'm not sure what the Publish #,# is supposed to do, but AFAIK, the # is the wildcard used by MQTT for subscribing to topics. Don't know what it does when sending.

My example used a more explicit publish,topic/name/value,[task#value] so in your case that could be:

Code: Select all

  publish,electrical.batteries.house.voltage,[electrical.batteries.house#voltage]
/Ton (PayPal.me)

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

Re: UDP and MQTT but with different Intervals?

#15 Post by TD-er » 25 Feb 2021, 17:28

According to the MQTT specifications, you can only use topic wildcards in a subscribe.
Not in a publish.

Affinite
Normal user
Posts: 57
Joined: 03 Apr 2018, 18:46

Re: UDP and MQTT but with different Intervals?

#16 Post by Affinite » 25 Feb 2021, 18:07

Thanks guys - I'll be more specific re: Publish <topic>,<value>
Dont know where I picked up Publish #,#

Edit: SUCCESS :D
Im publishing now
Now to play with Timer Rules :shock:
You guys have been wonderful - Thanks a mill

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

Re: UDP and MQTT but with different Intervals?

#17 Post by TD-er » 25 Feb 2021, 20:23

Maybe also nice to read a bit more on the loop timers here: https://espeasy.readthedocs.io/en/lates ... oop-timers

Affinite
Normal user
Posts: 57
Joined: 03 Apr 2018, 18:46

Re: UDP and MQTT but with different Intervals?

#18 Post by Affinite » 25 Feb 2021, 21:06

TD-er wrote: 25 Feb 2021, 20:23 Maybe also nice to read a bit more on the loop timers here: https://espeasy.readthedocs.io/en/lates ... oop-timers
Reading Now thanks

Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests