IFTTT – How to Parse JSON, XML, or CSV with IFTTT

csvgoogle sheetsgoogle-apps-scriptif-this-then-thatxml

I'd like to create an IFTTT action that allows triggers based upon a certain electricity price threshold. This Xively feed can return the current price in either JSON, XML, or CSV formats. Xively's API reference is here.

Is there any IFTTT channel/tool that will allow me to read a single value and act accordingly from any of these formats? Or is there a workaround I can use to translate the data for use with a channel that might not allow it directly?

Best Answer

Based on Normal Human's answer, I've created a Google sheets script that will parse and email IFTTT based on a price threshold. Google sheet cells won't update ImportXml() or other external site data more than once every hour or so, even using workarounds. However, accessing external sites via the script works. Use the following script and set up a trigger to have it run every one minute.

I've bypassed the Xively feed and gone straight to the source at Comed, since I'm writing the parsing script. There appears to be no XML/CSV/JSON parsing channel at this time on IFTTT.

Customize the threshold (presently 5.0) to whatever you'd like. You can also modify the code and use hash tags in the subject to create more complicated actions in IFTTT based on different price thresholds. For example, if the price was >5 you could tag the email #MEDIUM_PRICE, and set your thermostat to 78 degrees. If it were >10 you could tag it #HIGH_PRICE and set your thermostat to 82 degrees.

function myFunction() {
  var lastPriceCell = getThisCell(1,1);
  var lastPrice = lastPriceCell.getValue();
  var currentPrice = getPrice();
  var threshold = 5.0;
  var notify = false;

  if(currentPrice==null) {
    currentPrice = lastPrice;
  } else {
    // Notify only if last price was below threshold and current price is above threshold
    if(lastPrice <= threshold && currentPrice > threshold)
      notify = true;
  }
  lastPriceCell.setValue(currentPrice);

  if(notify) {
    var text = "Price Alert!" + currentPrice; 
    MailApp.sendEmail("trigger@recipe.ifttt.com", text, text);
  }
}

function getThisCell(i, j) {
  return SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange(i,j);
}

function getPrice() {
  var price = (/([\d\.]+)]]/).exec(getData());
  if(price!=null) 
    return price[1];
}

function getData() {
  return UrlFetchApp.fetch("https://rrtp.comed.com/rrtp/ServletFeed?type=pricechartfiveminute");
}