add timestamp to mqtt messages

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
horstbrack
New user
Posts: 4
Joined: 29 Nov 2023, 21:59

add timestamp to mqtt messages

#1 Post by horstbrack » 29 Nov 2023, 22:40

Hi there,

I want to log the temperatures of my central heating, so I installed two One-Wire-Sensors, one at the beginning of the loop, one at the end.
I'm using ESP-Easy Mega (git-build mega-20231013 / ESP_Easy_mega_20231013_collection_B_ESP8266_4M1M).
Values are sent to a MQTT broker running on an old Raspberry Pi

mosquitto/now 1.4.10-3+deb9u2
mosquitto-clients/now 1.4.10-3+deb9u2

Collecting the temperature values via ESP-Easy works fine, and logging them via mqtt on the RasPi works also.
What I am missing is a timestamp for the values.
Sounds like the easiest thing in the world, but I am running into dead ends whatever I try ...

Unfortunately, the mosquitto-client on the RasPi does not support output formatting (-F option is missing ...). So I tried to use awk on the RasPi, but it only runs mawk which does not support sprintf to format datetime ...
Also, the RasPi and the image it runs are quite old and struggle to update to newer package versions.

So here's the question: Is there an easy way to send formatted output to the mqtt broker in the first place?
In the Values section of the Devices-config, there is a Formula field, but it only seems to support arithmetic conversions, not the concatenation of system variables like %systime%.
I spent some time reading about rules, but I am not sure if this can be the solution to my problem.

Can anyone point me into the right direction?

Best Regards

Horst Brack

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

Re: add timestamp to mqtt messages

#2 Post by Ath » 29 Nov 2023, 23:14

You may have missed the fun part of ESPEasy, Rules :D
That feature has to be enabled in Tools/Advanced settings (top checkbox). And documentation for that, it can be a bit overwhelming..., can be found here: https://espeasy.readthedocs.io/en/lates ... Rules.html

You can respond on an event for the temperature sensor (I named it DS1 here), like this:

Code: Select all

on DS1#temperature do
  // Only send the time-stamp, the temperature data is handled by the Controller connected to the task
  Publish,your/DS1/mqtt/timestamp/topic,"%lcltime%"
endon
/Ton (PayPal.me)

horstbrack
New user
Posts: 4
Joined: 29 Nov 2023, 21:59

Re: add timestamp to mqtt messages

#3 Post by horstbrack » 30 Nov 2023, 18:30

Thanks for your quick reply and your hint, Ath!
I'll dive into rules and try your solution :-)

horstbrack
New user
Posts: 4
Joined: 29 Nov 2023, 21:59

Re: add timestamp to mqtt messages

#4 Post by horstbrack » 30 Nov 2023, 22:17

Tested it and it works, but ...
Now I get an additional message with the timestamp in my output, in other words alternating lines with timestamps and values.
That will do the job with a little postprocessing, but ideally I would like to have a single mqtt message for each measurement with a timestamp succeeded by the value.
Any ideas if and how that can be achieved?

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

Re: add timestamp to mqtt messages

#5 Post by Ath » 30 Nov 2023, 23:19

Hmm, AFAIK, MQTT is a single-content messaging system. If the server doesn't register the date/time of a received message, or you can't retrieve it, you're out of luck, I guess. Or we're talking about different things here...
/Ton (PayPal.me)

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

Re: add timestamp to mqtt messages

#6 Post by TD-er » 30 Nov 2023, 23:34

A MQTT message is just a string, so you can also create a combined message like this:

Code: Select all

on DS1#temperature do
  // Only send the time-stamp, the temperature data is handled by the Controller connected to the task
  Publish,your/DS1/mqtt/timestamp/topic,"%eventvalue%;%lcltime%"
endon

horstbrack
New user
Posts: 4
Joined: 29 Nov 2023, 21:59

Re: add timestamp to mqtt messages

#7 Post by horstbrack » 03 Dec 2023, 11:18

@Ath:
understood, I was looking for a way to concatenate timestamp and teperature value before it is written to the MQTT message primarily.

@TD-er:
Your example gets me exactly the output that I want, thanks!
What was still bothering me a bit was the fact that I then got 2 messages because I published the message under the same name as the sensor message.
But meanwhile I found out that you can define a new message in the rule, so now I have exactly what I want.

@Ath:
Your were right, rules are the fun part :D

Thanks so much for your help!

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 31 guests