Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Retrieve productivity score from RescueTime HTML widget
const globalHomeAssistant = global.get('homeassistant');
var html = msg.payload;
var attributeName = "efficiency_percent";
var start = html.indexOf(attributeName);
var attr = html.substr(start + attributeName.length + 2, 5);
msg = {};
msg.attr = attr;
msg.start = start;
msg.productivity = parseInt(attr.replace(/\"/g, "").trim());
var previousScore = parseInt(globalHomeAssistant.homeAssistant.states["sensor.productivity_score"].state);
msg.previousScore = previousScore;
if(msg.previousScore < msg.productivity){
msg.scoreState = "up";
}
else if(msg.previousScore > msg.productivity){
var diff = msg.previousScore - msg.productivity;
msg.notification = "Your productivity score went down ";
msg.notification += diff;
if(diff == 1){
msg.notification += " point";
}
else msg.notification += " points";
msg.notification += " to " + msg.productivity + " percent";
msg.scoreState = "down";
msg.notificationTitle = "Productivity going down: " + msg.productivity + "%";
}
else msg.scoreState = "same";
return msg;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment