Send values to MQTT only when changed
Moderators: grovkillen, Stuntteam, TD-er
Send values to MQTT only when changed
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
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
Re: Send values to MQTT only when changed
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)
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
Re: Send values to MQTT only when changed
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
- Attachments
-
- task.jpg (56.4 KiB) Viewed 2508 times
Re: Send values to MQTT only when changed
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:
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%
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
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
Re: Send values to MQTT only when changed
That totally worked! Thank you!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:Thus with the eventvalue included in the event.Code: Select all
ads1115_0x49_erste#Serverraum_Fenster=123
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
Re: Send values to MQTT only when changed
Sorry, could you help me out once more.
I wanted to add the other sensors. But that seems to crash the ESP (100% Load).
%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?
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
Is [int#3] global, do i need to increse it with every sensor?
Re: Send values to MQTT only when changed
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
eventvalue stays 1, int increase
Re: Send values to MQTT only when changed
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.
EDIT: Found it, int is ofc an integer, values with decimal dont work. Changed it to var.
Re: Send values to MQTT only when changed
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])
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])
Re: Send values to MQTT only when changed
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.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])
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?)
Re: Send values to MQTT only when changed
Can you show all your rules, here, so we can see if there is something incorrect in there?
/Ton (PayPal.me)
Re: Send values to MQTT only when changed
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?
Is the user setup to publish values to that MQTT broker allowed to publish to the broker?
Re: Send values to MQTT only when changed
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
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.
Re: Send values to MQTT only when changed
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.
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.
Who is online
Users browsing this forum: No registered users and 2 guests