Plugin: Covid Tracker Canada - Cases and vaccine progress

This simple plugin loads a device with data from the Canadian Covid Tracker API.
Attributes:

date
total_cases
total_fatalities
total_tests
total_hospitalizations
total_criticals
total_recoveries
total_vaccinations
total_vaccines_distributed
total_vaccinated
people_1_dose
percent_doses_admin
percent_pop_1_dose

Code:

plugin.Name = “covidtracker”;
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 600000;
plugin.DefaultSettings = {};

var http = new HTTPClient();

function onChangeRequest(device, attribute, value) { }
function onConnect() { console.log(“connected covid”); }
function onDisconnect() { console.log(“disconnected covid”); }

function onPoll() {
console.log(“polling covid”);
var response = http.get(“https://api.covid19tracker.ca/summary”);
var json = response.data.data[0];
var last_updated = response.data.last_updated;
//console.log(JSON.stringify(json));
//console.log(last_updated);
var mainDevice = plugin.Devices[“100”];
mainDevice.date = last_updated;
mainDevice.people_1_dose = json.total_vaccinations - json.total_vaccinated;
mainDevice.percent_doses_admin = (100 * json.total_vaccinations / json.total_vaccines_distributed);
mainDevice.percent_doses_admin = mainDevice.percent_doses_admin.toFixed(2)+’%’;
mainDevice.percent_pop_1_dose = (100 * mainDevice.people_1_dose / 38000000);
mainDevice.percent_pop_1_dose = mainDevice.percent_pop_1_dose.toFixed(2)+’%’;
for (var i in json) mainDevice[i] = json[i].replace(/\B(?=(\d{3})+(?!\d))/g, ‘,’);
}

function onSynchronizeDevices() {}

1 Like

Do you know if theres an api for provincial cases?

Yes, there is: https://api.covid19tracker.ca/summary/split shows the same data, split by provinces.

If I wanted to just report say BC’s value would I just change the http.get request?

@gregkinney posted these (USA) APIs in the old google groups forum. Wow… look at the stats in the attachment!

https://groups.google.com/g/thehomeremote/c/k-kQpAu46vo

There are a few changes: change the request as you mention, but also loop through all the entries in the json to identify the province you want.

@Jdamore WOW. 11 months ago. Feels like 11 years ago.

Here is how to get BC numbers, without adding a loop (this may stop working if they send the provinces in different order)

  • Change the get line to

var response = http.get(“https://api.covid19tracker.ca/summary/split”);

  • change the json line to (here we are assuming that BC will always come in 5th place)

var json = response.data.data[5];

  • change the for line to (for some reason the data comes as numbers instead of strings, so must be converted)

for (var i in json) { mainDevice[i] = json[i].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ‘,’); }

  • change the population number from Canada (38 millions) to BC

mainDevice.percent_pop_1_dose = (100 * mainDevice.people_1_dose / 5110917);

Cheers

1 Like

I keep seeing this in the log “Line 35:Unexpected end of Input”
Heres my plugin

plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 600000;
plugin.DefaultSettings = {};

var http = new HTTPClient();

function onChangeRequest(device, attribute, value) { }
function onConnect() {
}
function onDisconnect() {
}

function onPoll() {
console.log("polling covid");
var response = http.get("https://api.covid19tracker.ca/summary/split");
var json = response.data.data[5];
var last_updated = response.data.last_updated;
//console.log(JSON.stringify(json));
//console.log(last_updated);
var mainDevice = plugin.Devices["100"];
mainDevice.date = last_updated;
mainDevice.people_1_dose = json.total_vaccinations - json.total_vaccinated;
mainDevice.percent_doses_admin = (100 * json.total_vaccinations / json.total_vaccines_distributed);
mainDevice.percent_doses_admin = mainDevice.percent_doses_admin.toFixed(2)+'%';
mainDevice.percent_pop_1_dose = (100 * mainDevice.people_1_dose / 5110917);
mainDevice.percent_pop_1_dose = mainDevice.percent_pop_1_dose.toFixed(2)+'%';
for (var i in json) { mainDevice[i] = json[i].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}

function onSynchronizeDevices() {}

It seems the for line is missing a closing bracket } at the end.

(The closing bracket of the next line is for the function itself)

1 Like

Thanks for the help @torontonian was able to add some text to my idle page for daily province cases updates!

1 Like