How to add a timestamp to body?
3 views (last 30 days)
Show older comments
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
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.
Answers (5)
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.
See the sucessful output here https://thingspeak.com/channels/898224/field/1/last.json
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
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.
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?
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.
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.
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.
0 Comments
Communities
More Answers in the ThingSpeak Community
See Also
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!