diff --git a/bundles/org.openhab.binding.tibber/README.md b/bundles/org.openhab.binding.tibber/README.md index 2e71ca04f..cd25861c4 100644 --- a/bundles/org.openhab.binding.tibber/README.md +++ b/bundles/org.openhab.binding.tibber/README.md @@ -23,6 +23,7 @@ Tibber Default: |--------------------|-----------------------------------------|-----------| | Current Total | Current Total Price (energy + tax) | True | | Starts At | Current Price Timestamp | True | +| Current Level | Current Price Level | True | | Daily Cost | Daily Cost (last/previous day) | True | | Daily Consumption | Daily Consumption (last/previous day) | True | | Daily From | Timestamp (daily from) | True | @@ -104,6 +105,7 @@ demo.items: ``` Number:Dimensionless TibberAPICurrentTotal "Current Total Price [%.2f NOK]" {channel="tibber:tibberapi:7cfae492:current_total"} DateTime TibberAPICurrentStartsAt "Timestamp - Current Price" {channel="tibber:tibberapi:7cfae492:current_startsAt"} +String TibberAPICurrentLevel "Price Level" {channel="tibber:tibberapi:7cfae492:current_level"} DateTime TibberAPIDailyFrom "Timestamp - Daily From" {channel="tibber:tibberapi:7cfae492:daily_from"} DateTime TibberAPIDailyTo "Timestamp - Daily To" {channel="tibber:tibberapi:7cfae492:daily_to"} Number:Dimensionless TibberAPIDailyCost "Total Daily Cost [%.2f NOK]" {channel="tibber:tibberapi:7cfae492:daily_cost"} @@ -113,22 +115,22 @@ DateTime TibberAPIHourlyTo "Timestamp - Ho Number:Dimensionless TibberAPIHourlyCost "Total Hourly Cost [%.2f NOK]" {channel="tibber:tibberapi:7cfae492:hourly_cost"} Number:Energy TibberAPIHourlyConsumption "Total Hourly Consumption [%.2f kWh]" {channel="tibber:tibberapi:7cfae492:hourly_consumption"} DateTime TibberAPILiveTimestamp "Timestamp - Live Measurement" {channel="tibber:tibberapi:7cfae492:live_timestamp"} -Number:Power TibberAPILivePower "Live Power Consumption [W]" {channel="tibber:tibberapi:7cfae492:live_power"} +Number:Power TibberAPILivePower "Live Power Consumption [%.0f W]" {channel="tibber:tibberapi:7cfae492:live_power"} Number:Energy TibberAPILiveLastMeterConsumption "Last Meter Consumption [%.2f kWh]" {channel="tibber:tibberapi:7cfae492:live_lastMeterConsumption"} Number:Energy TibberAPILiveAccumulatedConsumption "Accumulated Consumption [%.2f kWh]" {channel="tibber:tibberapi:7cfae492:live_accumulatedConsumption"} 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 [W]" {channel="tibber:tibberapi:7cfae492:live_minPower"} -Number:Power TibberAPILiveAveragePower "Average Power Consumption [W]" {channel="tibber:tibberapi:7cfae492:live_averagePower"} -Number:Power TibberAPILiveMaxPower "Max Power Consumption [W]" {channel="tibber:tibberapi:7cfae492:live_maxPower"} -Number:ElectricPotential TibberAPILiveVoltage1 "Live Voltage Phase 1 [V]" {channel="tibber:tibberapi:7cfae492:live_voltage1"} -Number:ElectricPotential TibberAPILiveVoltage2 "Live Voltage Phase 2 [V]" {channel="tibber:tibberapi:7cfae492:live_voltage2"} -Number:ElectricPotential TibberAPILiveVoltage3 "Live Voltage Phase 3 [V]" {channel="tibber:tibberapi:7cfae492:live_voltage3"} -Number:ElectricCurrent TibberAPILiveCurrent1 "Live Current Phase 1 [A]" {channel="tibber:tibberapi:7cfae492:live_current1"} -Number:ElectricCurrent TibberAPILiveCurrent2 "Live Current Phase 2 [A]" {channel="tibber:tibberapi:7cfae492:live_current2"} -Number:ElectricCurrent TibberAPILiveCurrent3 "Live Current Phase 3 [A]" {channel="tibber:tibberapi:7cfae492:live_current3"} -Number:Power TibberAPILivePowerProduction "Live Power Production [W]" {channel="tibber:tibberapi:7cfae492:live_powerProduction"} -Number:Power TibberAPILiveMinPowerproduction "Min Power Production [W]" {channel="tibber:tibberapi:7cfae492:live_minPowerproduction"} -Number:Power TibberAPILiveMaxPowerproduction "Max Power Production [W]" {channel="tibber:tibberapi:7cfae492:live_maxPowerproduction"} +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 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"} +Number:ElectricPotential TibberAPILiveVoltage3 "Live Voltage Phase 3 [%.0 V]" {channel="tibber:tibberapi:7cfae492:live_voltage3"} +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:Energy TibberAPILiveAccumulatedProduction "Accumulated Production [%.2f kWh]" {channel="tibber:tibberapi:7cfae492:live_accumulatedProduction"} ``` 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 1b74ac06d..206cbd90d 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 @@ -43,6 +43,7 @@ public class TibberBindingConstants { // List of all Channel ids - Used for queries public static final String CURRENT_TOTAL = "current_total"; public static final String CURRENT_STARTSAT = "current_startsAt"; + public static final String CURRENT_LEVEL = "current_level"; public static final String DAILY_FROM = "daily_from"; public static final String DAILY_TO = "daily_to"; public static final String DAILY_COST = "daily_cost"; 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 8c5fe16f1..2e45e8014 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 @@ -102,6 +102,7 @@ public class TibberHandler extends BaseThingHandler { } public void getTibberParameters() { + String response = ""; try { httpHeader.put("cache-control", "no-cache"); httpHeader.put("content-type", JSON_CONTENT_TYPE); @@ -109,12 +110,10 @@ public class TibberHandler extends BaseThingHandler { TibberPriceConsumptionHandler tibberQuery = new TibberPriceConsumptionHandler(); InputStream connectionStream = tibberQuery.connectionInputStream(tibberConfig.getHomeid()); - String response = HttpUtil.executeUrl("POST", BASE_URL, httpHeader, connectionStream, null, - REQUEST_TIMEOUT); + response = HttpUtil.executeUrl("POST", BASE_URL, httpHeader, connectionStream, null, REQUEST_TIMEOUT); if (!response.contains("error") && !response.contains("")) { updateStatus(ThingStatus.ONLINE); - getURLInput(BASE_URL); InputStream inputStream = tibberQuery.getRealtimeInputStream(tibberConfig.getHomeid()); @@ -141,10 +140,11 @@ public class TibberHandler extends BaseThingHandler { } public void getURLInput(String url) throws IOException { + String jsonResponse = ""; TibberPriceConsumptionHandler tibberQuery = new TibberPriceConsumptionHandler(); InputStream inputStream = tibberQuery.getInputStream(tibberConfig.getHomeid()); - String jsonResponse = HttpUtil.executeUrl("POST", url, httpHeader, inputStream, null, REQUEST_TIMEOUT); + jsonResponse = HttpUtil.executeUrl("POST", url, httpHeader, inputStream, null, REQUEST_TIMEOUT); logger.debug("API response: {}", jsonResponse); if (!jsonResponse.contains("error") && !jsonResponse.contains("")) { @@ -163,13 +163,15 @@ public class TibberHandler extends BaseThingHandler { updateState(CURRENT_TOTAL, new DecimalType(myObject.get("total").toString())); String timestamp = myObject.get("startsAt").toString().substring(1, 20); updateState(CURRENT_STARTSAT, new DateTimeType(timestamp)); + updateState(CURRENT_LEVEL, new StringType(myObject.get("level").toString())); } catch (JsonSyntaxException e) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Error communicating with Tibber API: " + e.getMessage()); } } - if (jsonResponse.contains("daily")) { + if (jsonResponse.contains("daily") && !jsonResponse.contains("\"daily\":{\"nodes\":[]") + && !jsonResponse.contains("\"daily\":null")) { try { JsonObject myObject = (JsonObject) object.getAsJsonObject("data").getAsJsonObject("viewer") .getAsJsonObject("home").getAsJsonObject("daily").getAsJsonArray("nodes").get(0); @@ -188,7 +190,8 @@ public class TibberHandler extends BaseThingHandler { "Error communicating with Tibber API: " + e.getMessage()); } } - if (jsonResponse.contains("hourly")) { + if (jsonResponse.contains("hourly") && !jsonResponse.contains("\"hourly\":{\"nodes\":[]") + && !jsonResponse.contains("\"hourly\":null")) { try { JsonObject myObject = (JsonObject) object.getAsJsonObject("data").getAsJsonObject("viewer") .getAsJsonObject("home").getAsJsonObject("hourly").getAsJsonArray("nodes").get(0); @@ -257,7 +260,7 @@ public class TibberHandler extends BaseThingHandler { updateState(channelID, new QuantityType<>(new BigDecimal(channelValue), Units.WATT)); } else if (channelID.contains("voltage")) { updateState(channelID, new QuantityType<>(new BigDecimal(channelValue), Units.VOLT)); - } else if (channelID.contains("live_current")) { + } else if (channelID.contains("current")) { updateState(channelID, new QuantityType<>(new BigDecimal(channelValue), Units.AMPERE)); } else { updateState(channelID, new DecimalType(channelValue)); @@ -307,6 +310,7 @@ public class TibberHandler extends BaseThingHandler { WebSocketClient client = this.client; if (client == null) { client = new WebSocketClient(sslContextFactory, websocketExecutor); + client.setMaxIdleTimeout(600 * 1000); this.client = client; } @@ -352,7 +356,7 @@ public class TibberHandler extends BaseThingHandler { } catch (IOException e) { logger.warn("Websocket Close Exception: {}", e.getMessage()); } - session.close(0, "Tibber websocket disposed"); + session.close(); this.session = null; this.socket = null; } @@ -470,14 +474,14 @@ public class TibberHandler extends BaseThingHandler { if (myObject.has("voltagePhase3")) { updateChannel(LIVE_VOLTAGE3, myObject.get("voltagePhase3").toString()); } - if (myObject.has("currentPhase1")) { - updateChannel(LIVE_CURRENT1, myObject.get("currentPhase1").toString()); + if (myObject.has("currentL1")) { + updateChannel(LIVE_CURRENT1, myObject.get("currentL1").toString()); } - if (myObject.has("currentPhase2")) { - updateChannel(LIVE_CURRENT2, myObject.get("currentPhase2").toString()); + if (myObject.has("currentL2")) { + updateChannel(LIVE_CURRENT2, myObject.get("currentL2").toString()); } - if (myObject.has("currentPhase3")) { - updateChannel(LIVE_CURRENT3, myObject.get("currentPhase3").toString()); + if (myObject.has("currentL3")) { + updateChannel(LIVE_CURRENT3, myObject.get("currentL3").toString()); } if (myObject.has("powerProduction")) { updateChannel(LIVE_POWERPRODUCTION, myObject.get("powerProduction").toString()); @@ -508,7 +512,7 @@ public class TibberHandler extends BaseThingHandler { String query = "{\"id\":\"1\",\"type\":\"start\",\"payload\":{\"variables\":{},\"extensions\":{},\"operationName\":null,\"query\":\"subscription {\\n liveMeasurement(homeId:\\\"" + tibberConfig.getHomeid() + "\\\") {\\n timestamp\\n power\\n lastMeterConsumption\\n accumulatedConsumption\\n accumulatedCost\\n currency\\n minPower\\n averagePower\\n maxPower\\n" - + "voltagePhase1\\n voltagePhase2\\n voltagePhase3\\n currentPhase1\\n currentPhase2\\n currentPhase3\\n powerProduction\\n accumulatedProduction\\n minPowerProduction\\n maxPowerProduction\\n }\\n }\\n\"}}"; + + "voltagePhase1\\n voltagePhase2\\n voltagePhase3\\n currentL1\\n currentL2\\n currentL3\\n powerProduction\\n accumulatedProduction\\n minPowerProduction\\n maxPowerProduction\\n }\\n }\\n\"}}"; try { TibberWebSocketListener socket = TibberHandler.this.socket; if (socket != null) { 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 ec977721c..dd487eb82 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 class TibberPriceConsumptionHandler { public InputStream getInputStream(String homeId) { String Query = "{\"query\": \"{viewer {home (id: \\\"" + homeId - + "\\\") {currentSubscription {priceInfo {current {total startsAt }}} 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 }}} 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/thing/thing-types.xml b/bundles/org.openhab.binding.tibber/src/main/resources/OH-INF/thing/thing-types.xml index 0a7bd7ebc..ef320c4d1 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 @@ -10,6 +10,7 @@ + @@ -68,6 +69,11 @@ Total Price: Energy + Tax + + String + + Current price level + Number:Dimensionless