Skip to content

Commit

Permalink
Add JavaScript example code
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed May 19, 2023
1 parent 8dc2569 commit d512666
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions bundles/org.openhab.binding.energidataservice/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ To override filters, the channel `net-tariff` has the following configuration pa
| notes | text | Comma-separated list of notes | | no | yes |
| start | text | Query start date parameter expressed as either YYYY-MM-DD or dynamically as one of StartOfDay, StartOfMonth or StartOfYear | | no | yes |

The parameters `chargeTypeCodes` and `notes` are logically combined with "AND", so if only one parameter is needed for filter, only provide this parameter and leave the other one empty.
The parameters `chargeTypeCodes` and `notes` are logically combined with "AND", so if only one parameter is needed for the filter, only provide this parameter and leave the other one empty.
Using any of these parameters will override the pre-configured filter entirely.

The parameter `start` can be used independently to override the query start date parameter.
Expand Down Expand Up @@ -165,7 +165,7 @@ The result is a `Map` with the following keys:

This is a convenience method that can be used when the power consumption is not known.
The calculation will assume linear consumption and will find the best timeslot based on that.
The this reason the resulting `Map` will not contain the keys `LowestPrice` and `HighestPrice`.
For this reason the resulting `Map` will not contain the keys `LowestPrice` and `HighestPrice`.

Example:

Expand Down Expand Up @@ -338,15 +338,19 @@ String HourlyPrices "Hourly Prices" <price> { channel="energidataservice:service

### Thing Actions Example

:::: tabs

::: tab DSL

```javascript
import java.time.Duration
import java.util.ArrayList
import java.util.Map
import java.time.temporal.ChronoUnit

val actions = getActions("energidataservice", "energidataservice:service:energidataservice");
val actions = getActions("energidataservice", "energidataservice:service:energidataservice")

var priceMap = actions.getPrices(null);
var priceMap = actions.getPrices(null)
var hourStart = now.toInstant().truncatedTo(ChronoUnit.HOURS)
logInfo("Current total price excl. VAT", priceMap.get(hourStart).toString)

Expand Down Expand Up @@ -397,3 +401,24 @@ durationPhases.add(Duration.ofMinutes(104))

var Map<String, Object> result = actions.calculateCheapestPeriod(now.toInstant(), now.plusHours(24).toInstant(), Duration.ofMinutes(236), phases, 0.1 | kWh)
```

:::

::: tab JavaScript

```javascript
edsActions = actions.get("energidataservice", "energidataservice:service:energidataservice");

// Get prices and convert to JavaScript Map with Instant string representation as keys.
var priceMap = new Map();
utils.javaMapToJsMap(edsActions.getPrices()).forEach((value, key) => {
priceMap.set(key.toString(), value);
});

var hourStart = time.Instant.now().truncatedTo(time.ChronoUnit.HOURS);
console.log("Current total price excl. VAT: " + priceMap.get(hourStart.toString()));
```

:::

::::

0 comments on commit d512666

Please sign in to comment.