Send values to MQTT only when changed

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Marv21
New user
Posts: 8
Joined: 16 Jan 2024, 02:00

Send values to MQTT only when changed

#1 Post by Marv21 » 16 Jan 2024, 02:06

Hello,
I try to watch contacts if they are closed or not. If they are they have 5V, if not 0V. (Analog input - ADS1115)
I need to use a voltage messurement.
This works pretty well, but the values are transmitted every second - even if they didnt change.
I dont want to set the "interval" option to something higher, because i need instant reaction.
Is there something else i can set, so the values are only transmitted to MQTT if they changed?


Thanks

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

Re: Send values to MQTT only when changed

#2 Post by TD-er » 16 Jan 2024, 07:20

You can try to detect changes in the rules and then call publish from the rules instead of via the controller.
So you need to uncheck sending to the controller from the task.
Assuming your task is called "Analog" and the task value is "value" (change the rules accordingly when the names differ)

Code: Select all

on Analog#value do
  if [int#1]=0 and %eventvalue1%>2.5
    // Value has changed
    let,1,1
    publish,YourTopicHere,%eventvalue1%  
  elseif [int#1]=1 and %eventvalue1%<=2.5  
    // Value has changed
    let,1,0
    publish,YourTopicHere,%eventvalue1%  
  endif
endon

Marv21
New user
Posts: 8
Joined: 16 Jan 2024, 02:00

Re: Send values to MQTT only when changed

#3 Post by Marv21 » 17 Jan 2024, 00:06

TD-er wrote: 16 Jan 2024, 07:20 You can try to detect changes in the rules and then call publish from the rules instead of via the controller.
So you need to uncheck sending to the controller from the task.
Assuming your task is called "Analog" and the task value is "value" (change the rules accordingly when the names differ)

Code: Select all

on Analog#value do
  if [int#1]=0 and %eventvalue1%>2.5
    // Value has changed
    let,1,1
    publish,YourTopicHere,%eventvalue1%  
  elseif [int#1]=1 and %eventvalue1%<=2.5  
    // Value has changed
    let,1,0
    publish,YourTopicHere,%eventvalue1%  
  endif
endon

Hi,
thank you for your help.
I wanted to have every changed value send, not only 2,5 <>. ( i didnt say that, my bad...)

I tried to change it to:

Code: Select all

on ADS1115_0x49_erste#Serverraum_Fenster do
  if [int#1]!=%Serverraum_Fenster1%
    // Serverraum_Fenster has changed
    let,1,%Serverraum_Fenster1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x49_erste/Serverraum_Fenster,%Serverraum_Fenster1%
  endif
endon
But that didnt work.
Attachments
task.jpg
task.jpg (56.4 KiB) Viewed 1691 times

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

Re: Send values to MQTT only when changed

#4 Post by TD-er » 17 Jan 2024, 00:27

variables wrapped in percent chars like this are 'system variables' or 'global variables' (or variables in the scope of an event handling block) %...%
To refer to a task value, you need square brackets like this: [...#...] where the # is used to separate a task name and task value name.


For example: [ads1115_0x49_erste#Serverraum_Fenster]
And when handling an event sent by this exact task/taskvalue combination, you essentially get an event like this:

Code: Select all

ads1115_0x49_erste#Serverraum_Fenster=123
Thus with the eventvalue included in the event.
So instead of referring to the taskvalue in the rules, you can here also refer to %eventvalue1%

Code: Select all

on ADS1115_0x49_erste#Serverraum_Fenster do
  if [int#1]!=%eventvalue1%
    // Serverraum_Fenster has changed
    let,1,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x49_erste/Serverraum_Fenster,%eventvalue1%
  endif
endon

Marv21
New user
Posts: 8
Joined: 16 Jan 2024, 02:00

Re: Send values to MQTT only when changed

#5 Post by Marv21 » 17 Jan 2024, 00:48

TD-er wrote: 17 Jan 2024, 00:27 variables wrapped in percent chars like this are 'system variables' or 'global variables' (or variables in the scope of an event handling block) %...%
To refer to a task value, you need square brackets like this: [...#...] where the # is used to separate a task name and task value name.


For example: [ads1115_0x49_erste#Serverraum_Fenster]
And when handling an event sent by this exact task/taskvalue combination, you essentially get an event like this:

Code: Select all

ads1115_0x49_erste#Serverraum_Fenster=123
Thus with the eventvalue included in the event.
So instead of referring to the taskvalue in the rules, you can here also refer to %eventvalue1%

Code: Select all

on ADS1115_0x49_erste#Serverraum_Fenster do
  if [int#1]!=%eventvalue1%
    // Serverraum_Fenster has changed
    let,1,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x49_erste/Serverraum_Fenster,%eventvalue1%
  endif
endon
That totally worked! Thank you!

Marv21
New user
Posts: 8
Joined: 16 Jan 2024, 02:00

Re: Send values to MQTT only when changed

#6 Post by Marv21 » 17 Jan 2024, 01:19

Sorry, could you help me out once more.

I wanted to add the other sensors. But that seems to crash the ESP (100% Load).

Code: Select all

on ADS1115_0x49_erste#Serverraum_Fenster do
  if [int#1]!=%eventvalue1%
    // Serverraum_Fenster has changed
    let,1,%eventvalue1%%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x49_erste/Serverraum_Fenster,%eventvalue1%
  endif
Endon

on ADS1115_0x4B_dritte#Arbeitszimmer_Fenster_links do
  if [int#2]!=%eventvalue1%
    // Arbeitszimmer_Fenster_links has changed
    let,2,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x4B_dritte/Arbeitszimmer_Fenster_links,%eventvalue1%
  endif
endon

on ADS1115_0x4B_dritte#Arbeitszimmer_Fenster_rechts do
  if [int#3]!=%eventvalue1%
    let,3,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x4B_dritte/Arbeitszimmer_Fenster_rechts,%eventvalue1%
  endif
endon
%eventvalue% is global or just "event handling block"(on->endon)? Didnt understood that the last time you mentioned.
Is [int#3] global, do i need to increse it with every sensor?

Marv21
New user
Posts: 8
Joined: 16 Jan 2024, 02:00

Re: Send values to MQTT only when changed

#7 Post by Marv21 » 17 Jan 2024, 01:47

Code: Select all

on ADS1115_0x4B_dritte#Arbeitszimmer_Fenster_links do
  if [int#2]!=%eventvalue1%
    // Arbeitszimmer_Fenster_links has changed
    let,2,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x4B_dritte/Arbeitszimmer_Fenster_links,%eventvalue1%
  endif
endon

on ADS1115_0x4B_dritte#Arbeitszimmer_Fenster_rechts do
  if [int#3]!=%eventvalue1%
    let,3,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x4B_dritte/Arbeitszimmer_Fenster_rechts,%eventvalue1%
  endif
endon
worked.
eventvalue stays 1, int increase

Marv21
New user
Posts: 8
Joined: 16 Jan 2024, 02:00

Re: Send values to MQTT only when changed

#8 Post by Marv21 » 17 Jan 2024, 02:11

one more problem. If the Value is greater 0, they are refreshing every second...


EDIT: Found it, int is ofc an integer, values with decimal dont work. Changed it to var. ;)

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

Re: Send values to MQTT only when changed

#9 Post by TD-er » 17 Jan 2024, 09:33

Great!
You found it yourself, meaning you really understand what's happening.
That's the best way.

Maybe I should rephrase my description of the %...% syntax.
It is more like "not task specific".
"Global" is perhaps not the best description as it is indeed only referring to the local scope of the "on ... do ... endon" block in the rules.

So [...#...] is related to a specific task, but [int#xx] and [var#xx] are the exception to this.
For [var#xx] there is also an alias %vxx% (e.g. %v1% for [var#1])

Marv21
New user
Posts: 8
Joined: 16 Jan 2024, 02:00

Re: Send values to MQTT only when changed

#10 Post by Marv21 » 17 Jan 2024, 12:35

TD-er wrote: 17 Jan 2024, 09:33 Great!
You found it yourself, meaning you really understand what's happening.
That's the best way.

Maybe I should rephrase my description of the %...% syntax.
It is more like "not task specific".
"Global" is perhaps not the best description as it is indeed only referring to the local scope of the "on ... do ... endon" block in the rules.

So [...#...] is related to a specific task, but [int#xx] and [var#xx] are the exception to this.
For [var#xx] there is also an alias %vxx% (e.g. %v1% for [var#1])
I hoped so. But over the night the load increased to 100% again, webserver not responding anylonger. If i disable the rules, it responds again.
If I activate the rules now, the load is instant 100%. I didnt changed anything. Before i went to sleep it was around 40% load with 10 Rules... (is this even normal?)

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

Re: Send values to MQTT only when changed

#11 Post by Ath » 17 Jan 2024, 14:17

Can you show all your rules, here, so we can see if there is something incorrect in there?
/Ton (PayPal.me)

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

Re: Send values to MQTT only when changed

#12 Post by TD-er » 17 Jan 2024, 14:42

Does the ESP have a stable connection to the MQTT broker?
Is the user setup to publish values to that MQTT broker allowed to publish to the broker?

Marv21
New user
Posts: 8
Joined: 16 Jan 2024, 02:00

Re: Send values to MQTT only when changed

#13 Post by Marv21 » 17 Jan 2024, 23:44

Ath wrote: 17 Jan 2024, 14:17 Can you show all your rules, here, so we can see if there is something incorrect in there?

Code: Select all

on ADS1115_0x49_erste#Serverraum_Fenster do
  if [var#1]!=%eventvalue1%
    let,1,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x49_erste/Serverraum_Fenster,%eventvalue1%
  endif
endon

on ADS1115_0x49_erste#Bad_Fenster_Rechts do
  if [var#2]!=%eventvalue1%
    let,2,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x49_erste/Bad_Fenster_Rechts,%eventvalue1%
  endif
endon

on ADS1115_0x49_erste#Bad_Fenster_Links do
  if [var#3]!=%eventvalue1%
    let,3,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x49_erste/Bad_Fenster_Links,%eventvalue1%
  endif
endon

on ADS1115_0x49_erste#Utilityraum_Fenster do
  if [var#4]!=%eventvalue1%
    let,4,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x49_erste/Utilityraum_Fenster,%eventvalue1%
  endif
endon

on ADS1115_0x48_zweite#Utilityraum_Fenster_Glasbruch do
  if [var#5]!=%eventvalue1%
    let,5,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x48_zweite/Utilityraum_Fenster_Glasbruch,%eventvalue1%
  endif
endon

on ADS1115_0x48_zweite#Treppenhaus_Fenster do
  if [var#6]!=%eventvalue1%
    let,6,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x48_zweite/Treppenhaus_Fenster,%eventvalue1%
  endif
endon

on ADS1115_0x48_zweite#Haustuere do
  if [var#7]!=%eventvalue1%
    let,7,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x48_zweite/Haustuere,%eventvalue1%
  endif
endon

on ADS1115_0x4B_dritte#Arbeitszimmer_Fenster_links do
  if [var#8]!=%eventvalue1%
    // Arbeitszimmer_Fenster_links has changed
    let,8,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x4B_dritte/Arbeitszimmer_Fenster_links,%eventvalue1%
  endif
endon

on ADS1115_0x4B_dritte#Arbeitszimmer_Fenster_rechts do
  if [var#9]!=%eventvalue1%
    let,9,%eventvalue1%
    publish,esp/Espeasy_EG_Alarmanlage/ADS1115_0x4B_dritte/Arbeitszimmer_Fenster_rechts,%eventvalue1%
  endif
endon
All the rules.
TD-er wrote: 17 Jan 2024, 14:42 Does the ESP have a stable connection to the MQTT broker?
Is the user setup to publish values to that MQTT broker allowed to publish to the broker?
Hmm, dont know how stable the connection is. In the Dashboard i got "RSSI:-64 dBm".
The user is allowed, and i get values in the MQTT Broker every second. The user and client id is unique.
To make things more complicated, after i got home today, i was back to 40% load...

Disabling the rules and sending every second via "Send to Controller" i go down to 30%, so 40% seems ok.
Sending every 2 seconds, and i go down to 20%.

But why i was at 100% this morning, i cant recreate.

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

Re: Send values to MQTT only when changed

#14 Post by TD-er » 18 Jan 2024, 00:47

You should look at the logs in ESPEasy or the MQTT broker.

If for whatever reason the connection to the MQTT broker lost, the CPU load can be quite high.

Also check the timeout settings in the enabled MQTT controller.
If this is too short and the host running the MQTT broker can be slow, you can also run into these reconnect issues.

Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests