From 8ee8a1e2edcaa4337080d579472b6c39df8ce029 Mon Sep 17 00:00:00 2001 From: Markus Eckhardt Date: Sun, 22 Jan 2023 12:25:37 +0100 Subject: [PATCH] [Tibber] Added "today" API channel (#14236) * Added "today" API channel Signed-off-by: Markus Eckhardt --- bundles/org.openhab.binding.tibber/README.md | 14 ++++++++------ .../tibber/internal/TibberBindingConstants.java | 1 + .../tibber/internal/handler/TibberHandler.java | 4 ++++ .../handler/TibberPriceConsumptionHandler.java | 2 +- .../main/resources/OH-INF/i18n/tibber.properties | 4 +++- .../main/resources/OH-INF/thing/thing-types.xml | 9 ++++++++- 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/bundles/org.openhab.binding.tibber/README.md b/bundles/org.openhab.binding.tibber/README.md index 4935e15263c7b..3b8bd9abc8837 100644 --- a/bundles/org.openhab.binding.tibber/README.md +++ b/bundles/org.openhab.binding.tibber/README.md @@ -33,6 +33,7 @@ Tibber Default: | Hourly From | Timestamp (hourly from) | True | | Hourly To | Timestamp (hourly to) | True | | Tomorrow prices | JSON array of tomorrow's prices. See below for example. | True | +| Today prices | JSON array of today's prices. See below for example. | True | Tibber Pulse (optional): @@ -98,9 +99,9 @@ Retrieve personal token and HomeId from description above, and initialize/start Tibber API will be auto discovered if provided input is correct. -## Tomorrow prices +## Tomorrow and Today prices -Example of tomorrow prices data structure - an array of tuples: +Example of tomorrow and today prices data structure - an array of tuples: ```json [ @@ -232,7 +233,7 @@ Number:Energy TibberAPILiveAccumulatedConsumption "Accumulated Co Number:Dimensionless TibberAPILiveAccumulatedCost "Accumulated Cost [%.2f NOK]" {channel="tibber:tibberapi:7cfae492:live_accumulatedCost"} String TibberAPILiveCurrency "Currency" {channel="tibber:tibberapi:7cfae492:live_currency"} Number:Power TibberAPILiveMinPower "Min Power Consumption [%.0f W]" {channel="tibber:tibberapi:7cfae492:live_minPower"} -Number:Power TibberAPILiveAveragePower "Average Power Consumption [%.0f W]" {channel="tibber:tibberapi:7cfae492:live_averagePower"} +Number:Power TibberAPILiveAveragePower "Average Power Consumption [%.0f W]" {channel="tibber:tibberapi:7cfae492:live_averagePower"} Number:Power TibberAPILiveMaxPower "Max Power Consumption [%.0f W]" {channel="tibber:tibberapi:7cfae492:live_maxPower"} Number:ElectricPotential TibberAPILiveVoltage1 "Live Voltage Phase 1 [%.0 V]" {channel="tibber:tibberapi:7cfae492:live_voltage1"} Number:ElectricPotential TibberAPILiveVoltage2 "Live Voltage Phase 2 [%.0 V]" {channel="tibber:tibberapi:7cfae492:live_voltage2"} @@ -240,9 +241,10 @@ Number:ElectricPotential TibberAPILiveVoltage3 "Live Voltage P Number:ElectricCurrent TibberAPILiveCurrent1 "Live Current Phase 1 [%.1 A]" {channel="tibber:tibberapi:7cfae492:live_current1"} Number:ElectricCurrent TibberAPILiveCurrent2 "Live Current Phase 2 [%.1 A]" {channel="tibber:tibberapi:7cfae492:live_current2"} Number:ElectricCurrent TibberAPILiveCurrent3 "Live Current Phase 3 [%.1 A]" {channel="tibber:tibberapi:7cfae492:live_current3"} -Number:Power TibberAPILivePowerProduction "Live Power Production [%.0f W]" {channel="tibber:tibberapi:7cfae492:live_powerProduction"} -Number:Power TibberAPILiveMinPowerproduction "Min Power Production [%.0f W]" {channel="tibber:tibberapi:7cfae492:live_minPowerproduction"} -Number:Power TibberAPILiveMaxPowerproduction "Max Power Production [%.0f W]" {channel="tibber:tibberapi:7cfae492:live_maxPowerproduction"} +Number:Power TibberAPILivePowerProduction "Live Power Production [%.0f W]" {channel="tibber:tibberapi:7cfae492:live_powerProduction"} +Number:Power TibberAPILiveMinPowerproduction "Min Power Production [%.0f W]" {channel="tibber:tibberapi:7cfae492:live_minPowerproduction"} +Number:Power TibberAPILiveMaxPowerproduction "Max Power Production [%.0f W]" {channel="tibber:tibberapi:7cfae492:live_maxPowerproduction"} Number:Energy TibberAPILiveAccumulatedProduction "Accumulated Production [%.2f kWh]" {channel="tibber:tibberapi:7cfae492:live_accumulatedProduction"} String TibberAPITomorrowPrices "Price per hour tomorrow JSON array" {channel="tibber:tibberapi:7cfae492:tomorrow_prices"} +String TibberAPITodayPrices "Price per hour today JSON array" {channel="tibber:tibberapi:7cfae492:today_prices"} ``` diff --git a/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/TibberBindingConstants.java b/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/TibberBindingConstants.java index 0efd6794c2546..d1dd24e4cd9bc 100644 --- a/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/TibberBindingConstants.java +++ b/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/TibberBindingConstants.java @@ -45,6 +45,7 @@ public class TibberBindingConstants { public static final String CURRENT_STARTSAT = "current_startsAt"; public static final String CURRENT_LEVEL = "current_level"; + public static final String TODAY_PRICES = "today_prices"; public static final String TOMORROW_PRICES = "tomorrow_prices"; public static final String DAILY_FROM = "daily_from"; public static final String DAILY_TO = "daily_to"; diff --git a/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberHandler.java b/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberHandler.java index 3094c62827d46..64565af2e7a63 100644 --- a/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberHandler.java +++ b/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberHandler.java @@ -187,6 +187,10 @@ public void getURLInput(String url) throws IOException { .getAsJsonObject("home").getAsJsonObject("currentSubscription").getAsJsonObject("priceInfo") .getAsJsonArray("tomorrow"); updateState(TOMORROW_PRICES, new StringType(tomorrow.toString())); + JsonArray today = rootJsonObject.getAsJsonObject("data").getAsJsonObject("viewer") + .getAsJsonObject("home").getAsJsonObject("currentSubscription").getAsJsonObject("priceInfo") + .getAsJsonArray("today"); + updateState(TODAY_PRICES, new StringType(today.toString())); } catch (JsonSyntaxException e) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Error communicating with Tibber API: " + e.getMessage()); diff --git a/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberPriceConsumptionHandler.java b/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberPriceConsumptionHandler.java index 75e330e37faf8..62f5d0f90b01f 100644 --- a/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberPriceConsumptionHandler.java +++ b/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberPriceConsumptionHandler.java @@ -33,7 +33,7 @@ public InputStream connectionInputStream(String homeId) { public InputStream getInputStream(String homeId) { String query = "{\"query\": \"{viewer {home (id: \\\"" + homeId - + "\\\") {currentSubscription {priceInfo {current {total startsAt level } tomorrow { startsAt total }}} daily: consumption(resolution: DAILY, last: 1) {nodes {from to cost unitPrice consumption consumptionUnit}} hourly: consumption(resolution: HOURLY, last: 1) {nodes {from to cost unitPrice consumption consumptionUnit}}}}}\"}"; + + "\\\") {currentSubscription {priceInfo {current {total startsAt level } tomorrow { startsAt total } today { startsAt total }}} daily: consumption(resolution: DAILY, last: 1) {nodes {from to cost unitPrice consumption consumptionUnit}} hourly: consumption(resolution: HOURLY, last: 1) {nodes {from to cost unitPrice consumption consumptionUnit}}}}}\"}"; return new ByteArrayInputStream(query.getBytes(StandardCharsets.UTF_8)); } diff --git a/bundles/org.openhab.binding.tibber/src/main/resources/OH-INF/i18n/tibber.properties b/bundles/org.openhab.binding.tibber/src/main/resources/OH-INF/i18n/tibber.properties index ce2963d31adcc..c79af77bbb047 100644 --- a/bundles/org.openhab.binding.tibber/src/main/resources/OH-INF/i18n/tibber.properties +++ b/bundles/org.openhab.binding.tibber/src/main/resources/OH-INF/i18n/tibber.properties @@ -40,4 +40,6 @@ channel-type.tibber.timestamp.description = Timestamp for measurement/change channel-type.tibber.voltage.label = Voltage channel-type.tibber.voltage.description = Voltage on given Phase channel-type.tibber.tomorrow_prices.label = Prices for tomorrow as a JSON array -channel-type.tibber.tomorrow_prices.description = JSON array of tuples startsAt,total, e.g. {["startsAt": "2022-09-10T00:00:00+02:00", "total": 5.332}, {"startsAt": ...}]}. See binding documantation for full example. +channel-type.tibber.tomorrow_prices.description = JSON array of tuples startsAt,total, e.g. {["startsAt": "2022-09-10T00:00:00+02:00", "total": 5.332}, {"startsAt": ...}]}. See binding documentation for full example. +channel-type.tibber.today_prices.label = Prices for today as a JSON array +channel-type.tibber.today_prices.description = JSON array of tuples startsAt,total, e.g. {["startsAt": "2022-09-10T00:00:00+02:00", "total": 5.332}, {"startsAt": ...}]}. See binding documentation for full example. diff --git a/bundles/org.openhab.binding.tibber/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.tibber/src/main/resources/OH-INF/thing/thing-types.xml index d990328aec375..9118147f3dbdb 100644 --- a/bundles/org.openhab.binding.tibber/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.tibber/src/main/resources/OH-INF/thing/thing-types.xml @@ -12,6 +12,7 @@ + @@ -120,6 +121,12 @@ String JSON array of tuples startsAt,total, e.g. {["startsAt": "2022-09-10T00:00:00+02:00", "total": 5.332}, - {"startsAt": ...}]. See binding documantation for full example. + {"startsAt": ...}]. See binding documentation for full example. + + + String + + JSON array of tuples startsAt,total, e.g. {["startsAt": "2022-09-10T00:00:00+02:00", "total": 5.332}, + {"startsAt": ...}]. See binding documentation for full example.