frandai / tarifeitor

0 stars 2 forks source link

Conseguir datos en tiempo real del precio de cada tarifa #5

Open frandai opened 3 years ago

frandai commented 3 years ago

Usar algo como esto (mejorar):

const lightPrice = async () => {
  const current = (new Date()).toLocaleDateString("sv", {
    timeZone: "Europe/Madrid",
  });

  const from = current + "T00:00";
  const to = current + "T23:59";

  const data = await request({
    json: true,
    method: "GET",
    url:
      "https://apidatos.ree.es/en/datos/mercados/precios-mercados-tiempo-real?" +
      new URLSearchParams({
        start_date: from,
        end_date: to,
        time_trunc: "hour",
      }),
    followRedirect: true,
  });

  if (data.included) {
    let pvpc, spotmarket;

    //TODO: Fix get datetime of current datetime
    pvpc = data.included.filter((inc) => inc.id === "1001")[0].attributes
      .values;
    spotmarket = data.included.filter((inc) => inc.id === "600")[0].attributes
      .values;

    return `Ahora mismo pagas ${
      pvpc[pvpc.length - 1].value
    } € por MWh en mercado regulado y ${
      spotmarket[spotmarket.length - 1].value
    } € por MWh en mercado libre (precios peninsulares).`;
  }
  return "Ha habido un error :S";
};

---

      await sendMessageToChat(token, msg.chat.id, await lightPrice());
Jose3212 commented 3 years ago

Podriamos intentar compararlos con los precios del mismo día en el mismo anterior en ese tramo horario para ir viendo progresivamente la subida de la luz.

frandai commented 3 years ago

Creo que eso sería otra funcionalidad extra, dependiente previamente de esta funcionalidad. Puedes darla de alta como nueva issue :)