diff --git a/awattar/README.md b/awattar/README.md index 52c2eca4..fbffec6e 100644 --- a/awattar/README.md +++ b/awattar/README.md @@ -16,6 +16,8 @@ In the following chart you can see an example of the market prices from -12 hour * +100 % current price equals highest price in the interval [-12h `<` now `<` + 12h] ![aWATTar graph](https://raw.githubusercontent.com/guh/nymea-plugins/master/awattar/docs/images/awattar-graph.png "aWATTar graph") + +Additionally, the plugin holds a rank for the current slot. 0 Indicates the cheapest slot of the interval [-12h `<` now `<` + 12h]. ## Requirements diff --git a/awattar/integrationpluginawattar.cpp b/awattar/integrationpluginawattar.cpp index 067f2200..da2de913 100644 --- a/awattar/integrationpluginawattar.cpp +++ b/awattar/integrationpluginawattar.cpp @@ -153,25 +153,31 @@ void IntegrationPluginAwattar::processPriceData(Thing *thing, const QVariantMap int deviation = 0; double maxPrice = -1000; double minPrice = 1000; - foreach (QVariant element, dataElements) { + QList prices; + for (int i = 0; i < dataElements.count(); i++) { + QVariant element = dataElements.at(i); QVariantMap elementMap = element.toMap(); QDateTime startTime = QDateTime::fromMSecsSinceEpoch(elementMap.value("start_timestamp").toLongLong()); QDateTime endTime = QDateTime::fromMSecsSinceEpoch(elementMap.value("end_timestamp").toLongLong()); double price = elementMap.value("marketprice").toDouble(); // check interval [-12h < x < + 12h] - if ((startTime >= currentTime.addSecs(-3600 * 12) && endTime <= currentTime ) || - (endTime <= currentTime.addSecs(3600 * 12) && startTime >= currentTime )) { + if (startTime >= currentTime.addSecs(-3600 * 12) && endTime <= currentTime.addSecs(3600 * 12)) { sum += price; count++; + prices.append(price); + qCDebug(dcAwattar()) << "Adding price" << startTime.toString() << price; if (price > maxPrice) maxPrice = price; if (price < minPrice) minPrice = price; + } else { + qCDebug(dcAwattar()) << "Not adding price" << startTime.toString() << price; } + if (currentTime >= startTime && currentTime <= endTime) { currentPrice = price; sum += price; @@ -188,6 +194,7 @@ void IntegrationPluginAwattar::processPriceData(Thing *thing, const QVariantMap } } + // calculate averagePrice and mean deviation averagePrice = sum / count; @@ -201,5 +208,14 @@ void IntegrationPluginAwattar::processPriceData(Thing *thing, const QVariantMap thing->setStateValue(m_lowestPriceStateTypeIds.value(thing->thingClassId()), minPrice / 10.0); thing->setStateValue(m_highestPriceStateTypeIds.value(thing->thingClassId()), maxPrice / 10.0); thing->setStateValue(m_averageDeviationStateTypeIds.value(thing->thingClassId()), deviation); + + qCDebug(dcAwattar()) << "AVG:" << averagePrice << "Min:" << minPrice << "Max:" << maxPrice << "Curr:" << currentPrice; + qSort(prices.begin(), prices.end()); + int rank = prices.indexOf(currentPrice); + if (rank < 0) { + rank = 100; + } + thing->setStateValue("rank", rank); + } diff --git a/awattar/integrationpluginawattar.json b/awattar/integrationpluginawattar.json index bc664aa7..03053070 100644 --- a/awattar/integrationpluginawattar.json +++ b/awattar/integrationpluginawattar.json @@ -79,6 +79,15 @@ "type": "double", "unit": "EuroCentPerKiloWattHour", "defaultValue": 0 + }, + { + "id": "8af49ba8-0aba-4f09-ab11-533dcd19e873", + "name": "rank", + "displayName": "Current rank (lower is better)", + "type": "uint", + "minValue": 0, + "maxValue": 100, + "defaultValue": 100 } ] }, @@ -153,6 +162,15 @@ "type": "double", "unit": "EuroCentPerKiloWattHour", "defaultValue": 0 + }, + { + "id": "f3a2a46f-e281-4f60-9d27-345264b67f9a", + "name": "rank", + "displayName": "Current rank (lower is better)", + "type": "uint", + "minValue": 0, + "maxValue": 100, + "defaultValue": 100 } ] }