Create Template Tag to be used in a Template opened with `New Draft with Template`Action

Hello,

I created a simple script action which pulls the current temperature from the OpenWeatherMap API. This works fairly well. But I would like to use the action as template tag in one of my templates that I create with the excellent New Draft with Template Action. But it seems that the template tag is not handed through in the New Draft with Template Action even if I include the Temperature Action before the New Draft with Template.

Here is my action so far:


let credential = Credential.create("Open Weather Map API Key", "Enter your Open Weather Map API key, which can be copied from the Open Weather Map developer console page after creating a free account.");

credential.addTextField("key", "API Key");
credential.authorize();

let apiKey = credential.getValue("key");

let getTemperature = (latitude, longtitide, units = 'metric') => {
	let endpoint = `http://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longtitide}&units=${units}&appid=${apiKey}`;
	let http = HTTP.create();
	let response = http.request({
		"url": endpoint, 
		"method": "GET"
	});
	if (response.statusCode != 200) {
		context.fail();
		console.log("Openweathermap Error: " + response.error);
		return null;
	}
	// parse response JSON to object
	let data = JSON.parse(response.responseText);
	if (!data) {
		context.fail();
		console.log("OpenweathermapError: Unable to parse response");
		return null;
	}

	// return the forecast data object
	return data;	
}

let lat = draft.createdLatitude;
let lon = draft.createdLongitude;
let units = 'metric';
let data = getTemperature(lat, lon, units);

if (data) {
	let temperature = data.main.temp + '°C';
	alert(temperature);
	draft.setTemplateTag("temperature", temperature);
}

The temperature will be returned in the alert but the [[temperature]] is not rendered in the new draft. What am I missing @agiletortoise?

Custom template tags are specific to a draft. By calling draft.setTemplateTag you are setting a tag value for the current draft.

The “New Draft with Template” action creates a new draft, held in the d var in the script, and runs it through the template engine. It is a different draft, so does not contain the new tag value you assigned to draft - hopefully that makes some sense.

Several ways to solve this problem. Assuming you have modified the “New Draft with Template” action to contain an additional script above (I guess in a separate step?), you could change the end of that script as follows:

let temperature = "No temperature retrieved"; // default value
if (data) {
    temperature = data.main.temp + '°C'; // remove `let`!
    alert(temperature);
}

Then in the “new draft” script from the template action, look for this section and add the indicated line:

    let d = Draft.create();
    for (let tag of template.tags) {
        if (tag != "template") {
            d.addTag(tag);
        }
    }
    // BEGIN ADD
    d.setTemplateTag("temperature", temperature);
    // END ADD
    d.content = d.processTemplate(template.content);
    d.update();

That will set the template value on the newly created draft and it will work.

Perfect this works for now - the only thing is that I need to figure out why the temperature is wrong the first time I execute the command and only if I execute the action twice shortly after each other the temperature is right. It has probably something to do with the location - seems to be pretty unreliable, sometimes they are just 0.00 - is this a timing issue?