mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[energidataservice] Improve rule example for calculating totals (#18024)
* Improve rule example for calculating totals Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk> Co-authored-by: Jimmy Tanagra <jcode@tanagra.id.au>
This commit is contained in:
parent
705feffd54
commit
563caa0762
@ -87,19 +87,22 @@ In this example file-based using Rule Builder:
|
|||||||
rules.when()
|
rules.when()
|
||||||
.channel('energidataservice:service:energidataservice:electricity#event').triggered('DAY_AHEAD_AVAILABLE')
|
.channel('energidataservice:service:energidataservice:electricity#event').triggered('DAY_AHEAD_AVAILABLE')
|
||||||
.then(event => {
|
.then(event => {
|
||||||
var timeSeries = new items.TimeSeries('REPLACE');
|
// Short delay because persistence is asynchronous.
|
||||||
var start = time.LocalDate.now().atStartOfDay().atZone(time.ZoneId.systemDefault());
|
setTimeout(() => {
|
||||||
var spotPrices = items.SpotPrice.persistence.getAllStatesBetween(start, start.plusDays(2));
|
var timeSeries = new items.TimeSeries('REPLACE');
|
||||||
for (var spotPrice of spotPrices) {
|
var start = time.LocalDate.now().atStartOfDay().atZone(time.ZoneId.systemDefault());
|
||||||
var totalPrice = spotPrice.quantityState
|
var spotPrices = items.SpotPrice.persistence.getAllStatesBetween(start, start.plusDays(2));
|
||||||
.add(items.GridTariff.persistence.persistedState(spotPrice.timestamp).quantityState)
|
for (var spotPrice of spotPrices) {
|
||||||
.add(items.SystemTariff.persistence.persistedState(spotPrice.timestamp).quantityState)
|
var totalPrice = spotPrice.quantityState
|
||||||
.add(items.TransmissionGridTariff.persistence.persistedState(spotPrice.timestamp).quantityState)
|
.add(items.GridTariff.persistence.persistedState(spotPrice.timestamp).quantityState)
|
||||||
.add(items.ElectricityTax.persistence.persistedState(spotPrice.timestamp).quantityState);
|
.add(items.SystemTariff.persistence.persistedState(spotPrice.timestamp).quantityState)
|
||||||
|
.add(items.TransmissionGridTariff.persistence.persistedState(spotPrice.timestamp).quantityState)
|
||||||
|
.add(items.ElectricityTax.persistence.persistedState(spotPrice.timestamp).quantityState);
|
||||||
|
|
||||||
timeSeries.add(spotPrice.timestamp, totalPrice);
|
timeSeries.add(spotPrice.timestamp, totalPrice);
|
||||||
}
|
}
|
||||||
items.TotalPrice.persistence.persist(timeSeries);
|
items.TotalPrice.persistence.persist(timeSeries);
|
||||||
|
}, 5000);
|
||||||
})
|
})
|
||||||
.build("Calculate total price");
|
.build("Calculate total price");
|
||||||
```
|
```
|
||||||
@ -112,23 +115,25 @@ rules.when()
|
|||||||
rule "Calculate total price" do
|
rule "Calculate total price" do
|
||||||
channel "energidataservice:service:energidataservice:electricity#event", triggered: "DAY_AHEAD_AVAILABLE"
|
channel "energidataservice:service:energidataservice:electricity#event", triggered: "DAY_AHEAD_AVAILABLE"
|
||||||
run do
|
run do
|
||||||
# Persistence methods will call LocalDate#to_zoned_date_time which converts it
|
after 5.seconds do # Short delay because persistence is asynchronous.
|
||||||
# to a ZonedDateTime in the default system zone, with 00:00 as its time portion
|
# Persistence methods will call LocalDate#to_zoned_date_time which converts it
|
||||||
start = LocalDate.now
|
# to a ZonedDateTime in the default system zone, with 00:00 as its time portion
|
||||||
spot_prices = SpotPrice.all_states_between(start, start + 2.days)
|
start = LocalDate.now
|
||||||
|
spot_prices = SpotPrice.all_states_between(start, start + 2.days)
|
||||||
|
|
||||||
next unless spot_prices # don't proceed if the persistence result is nil
|
next unless spot_prices # don't proceed if the persistence result is nil
|
||||||
|
|
||||||
time_series = TimeSeries.new # the default policy is replace
|
time_series = TimeSeries.new # the default policy is replace
|
||||||
spot_prices.each do |spot_price|
|
spot_prices.each do |spot_price|
|
||||||
total_price = spot_price +
|
total_price = spot_price +
|
||||||
GridTariff.persisted_state(spot_price.timestamp) +
|
GridTariff.persisted_state(spot_price.timestamp) +
|
||||||
SystemTariff.persisted_state(spot_price.timestamp) +
|
SystemTariff.persisted_state(spot_price.timestamp) +
|
||||||
TransmissionGridTariff.persisted_state(spot_price.timestamp) +
|
TransmissionGridTariff.persisted_state(spot_price.timestamp) +
|
||||||
ElectricityTax.persisted_state(spot_price.timestamp)
|
ElectricityTax.persisted_state(spot_price.timestamp)
|
||||||
time_series.add(spot_price.timestamp, total_price)
|
time_series.add(spot_price.timestamp, total_price)
|
||||||
|
end
|
||||||
|
TotalPrice.persist(time_series)
|
||||||
end
|
end
|
||||||
TotalPrice.persist(time_series)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user