Skip to content

Commit

Permalink
Merge PR #730: Awattar: Add a ranking state for simple use in rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins committed Jan 30, 2024
2 parents 2a5dfe5 + 3c07011 commit 8bb448d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions awattar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 19 additions & 3 deletions awattar/integrationpluginawattar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> 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;
Expand All @@ -188,6 +194,7 @@ void IntegrationPluginAwattar::processPriceData(Thing *thing, const QVariantMap
}
}


// calculate averagePrice and mean deviation
averagePrice = sum / count;

Expand All @@ -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);

}

18 changes: 18 additions & 0 deletions awattar/integrationpluginawattar.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
},
Expand Down Expand Up @@ -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
}
]
}
Expand Down

0 comments on commit 8bb448d

Please sign in to comment.