How to add a timestamp to body?

3 views (last 30 days)
Zdenek Svezen
Zdenek Svezen on 11 Nov 2019
Hello,
I have this in ThingHTTP body:
{"src":"013025819299","dst":"+420605555555","text":"ALARM"}
It works fine, but I need to insert timestamp inside it, like this:
{"src":"013025819299","dst":"+420605555555","text":"ALARM at %%timestamp%%"}
Is it possible? How to do it?
  3 Comments
Christopher Stapels
Christopher Stapels on 12 Nov 2019
Just make sure you trigger the request with a react or timecontrol, not an external service.https://www.mathworks.com/help/thingspeak/thinghttp-app.html
%%datetime%% should work.
Zdenek Svezen
Zdenek Svezen on 12 Nov 2019
If I insert %%datetime%% into body, nothing happens. It does not replace this tag with actual dat and time.
Plase help.

Sign in to comment.

Answers (5)

Christopher Stapels
Christopher Stapels on 12 Nov 2019
Edited: Christopher Stapels on 12 Nov 2019
You need to trigger the request with a TimeControl or React. I just tested it with a TimeControl, I'm sure I've tested with a React but not recently.
This is the result of a ThingHTTP with method POST, the URL https://api.thingspeak.com/update
and Body: api_key=XXXXXXXXXXXXXXXX&field1=%%datetime%% (with the write API key for XX)
I triggered it with a one time timecontrol that I set up just after reading your post.
  3 Comments
Christopher Stapels
Christopher Stapels on 12 Nov 2019
I reccomend you use
{"src":"013025819299","dst":"+420605555555","text":"ALARM at %%datetime%%"}
in your thingHTTP body.
Then make sure you trigger the ThingHTTP using the TimeControl app. Select Apps > TimeControl when you are logged in to your ThingSpeak account.
Normally you might use the ThinkHTTP API key and a rest GET call to trigger a ThingHTTP.
If you have the %%datetime%% replacement key in the body and you trigger the ThingHTTP in your browser or any other way using the URL directly, it will not replace %%datetime%%. If you schedule a TimeControl or React to trigger the ThingHTTP, it will replace the test %%datetime%% with the time it was triggered.
You can also pass the time into the ThingHTTP when you call it using custom replacement keys. Have a look at the example POSTMAN Request Using Replacement Key on the https://www.mathworks.com/help/thingspeak/thinghttp-app.htmlThingHTTP documentation page.
Zdenek Svezen
Zdenek Svezen on 12 Nov 2019
I have insert new TimeControl app.
And inserted %%datetime%% into ThingHTTP body and nothing :(

Sign in to comment.


Christopher Stapels
Christopher Stapels on 12 Nov 2019
Can you take screen shots of the thingHTTP and TimeControl settings? Make sure you blur out the api keys if you want to keep the information private.
What url are you sending to, or how are you finding out if the datetime was sucessfully replaced? Can you show the result?
  1 Comment
Zdenek Svezen
Zdenek Svezen on 12 Nov 2019
ThingSpeak.png
TimeControl.png
But this set is called when time equals what is set in TimeControl and call ThingHttp.
It does not put timecode into it :(

Sign in to comment.


Christopher Stapels
Christopher Stapels on 13 Nov 2019
What time would you want to see in the response? I would have guessed you want to see the time it was called. It might help for you to describe your whole project so we can better help you.
  1 Comment
Zdenek Svezen
Zdenek Svezen on 13 Nov 2019
From my alarm device I call ThingHTTP. This ThingHTTP sends me an SMS.
In this SMS I need time stamp.
I am not able to send the time from my device as a parameter so I need that ThingHTTP will add actual time stamp into its body (there is currently %%datetime%% tag).

Sign in to comment.


Christopher Stapels
Christopher Stapels on 13 Nov 2019
Yes this is the expected behavior. As described above, you need to call the thingHTTP with TimeControl or React to use the datetime replacement key.
You said "From my alarm device I call ThingHTTP.", which would not replace the datetime key.
But you also showed me a TimeControl set to call the thingHTTP. That workflow should work. When you allow the timeControl to call the thingHTTP, ThingSpeak should replace the datetime replacement key correctly. Do you observe that behavior?
What is the alarm device? Does it have a timestamp it could pass? You can use a custom replacement key.
  1 Comment
Zdenek Svezen
Zdenek Svezen on 13 Nov 2019
Alarm device is Arduino and I do not have real time control module there.
Sou you suggest calling TimeControl which calls ThingHTTP? And timestamp will be added, right?
But TimeControl calls itself. :( How to call TimeControl?

Sign in to comment.


Christopher Stapels
Christopher Stapels on 18 Nov 2019
You should also be able to call the THingHTTP with React and the replacement key will work.
So have the device write data (for example write the number 1) to a channel and the react tied to the channel. The react threshold could be if the number were greater than 0.
The react calls your ThinkHTTP, which writes to the external service that you are trying to use. Its a few steps, but allows you to track when everything happened.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Read Data from Channel in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!