Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tibber] Add channel for tomorrows prices and timestamps as JSON array #13416

Merged
merged 4 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bundles/org.openhab.binding.tibber/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Tibber Default:
| Hourly Consumption | Hourly Consumption (last/previous hour) | True |
| Hourly From | Timestamp (hourly from) | True |
| Hourly To | Timestamp (hourly to) | True |
| Tomorrow | JSON array of tomorrow's prices | True |
seime marked this conversation as resolved.
Show resolved Hide resolved

Tibber Pulse (optional):

Expand Down Expand Up @@ -139,4 +140,5 @@ Number:Power TibberAPILivePowerProduction "Live Power Pro
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"}
```
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class TibberBindingConstants {
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 TOMORROW = "tomorrow";
public static final String DAILY_FROM = "daily_from";
public static final String DAILY_TO = "daily_to";
public static final String DAILY_COST = "daily_cost";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
Expand Down Expand Up @@ -149,20 +150,24 @@ public void getURLInput(String url) throws IOException {
updateStatus(ThingStatus.ONLINE);
}

JsonObject object = (JsonObject) JsonParser.parseString(jsonResponse);
JsonObject rootJsonObject = (JsonObject) JsonParser.parseString(jsonResponse);

if (jsonResponse.contains("total")) {
try {
JsonObject myObject = object.getAsJsonObject("data").getAsJsonObject("viewer")
JsonObject current = rootJsonObject.getAsJsonObject("data").getAsJsonObject("viewer")
.getAsJsonObject("home").getAsJsonObject("currentSubscription").getAsJsonObject("priceInfo")
.getAsJsonObject("current");

updateState(CURRENT_TOTAL, new DecimalType(myObject.get("total").toString()));
String timestamp = myObject.get("startsAt").toString().substring(1, 20);
updateState(CURRENT_TOTAL, new DecimalType(current.get("total").toString()));
String timestamp = current.get("startsAt").toString().substring(1, 20);
updateState(CURRENT_STARTSAT, new DateTimeType(timestamp));
updateState(CURRENT_LEVEL,
new StringType(myObject.get("level").toString().replaceAll("^\"|\"$", "")));
new StringType(current.get("level").toString().replaceAll("^\"|\"$", "")));

JsonArray tomorrow = rootJsonObject.getAsJsonObject("data").getAsJsonObject("viewer")
.getAsJsonObject("home").getAsJsonObject("currentSubscription").getAsJsonObject("priceInfo")
.getAsJsonArray("tomorrow");
updateState(TOMORROW, new StringType(tomorrow.toString()));
} catch (JsonSyntaxException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Error communicating with Tibber API: " + e.getMessage());
Expand All @@ -171,7 +176,7 @@ public void getURLInput(String url) throws IOException {
if (jsonResponse.contains("daily") && !jsonResponse.contains("\"daily\":{\"nodes\":[]")
&& !jsonResponse.contains("\"daily\":null")) {
try {
JsonObject myObject = (JsonObject) object.getAsJsonObject("data").getAsJsonObject("viewer")
JsonObject myObject = (JsonObject) rootJsonObject.getAsJsonObject("data").getAsJsonObject("viewer")
.getAsJsonObject("home").getAsJsonObject("daily").getAsJsonArray("nodes").get(0);

String timestampDailyFrom = myObject.get("from").toString().substring(1, 20);
Expand All @@ -191,7 +196,7 @@ public void getURLInput(String url) throws IOException {
if (jsonResponse.contains("hourly") && !jsonResponse.contains("\"hourly\":{\"nodes\":[]")
&& !jsonResponse.contains("\"hourly\":null")) {
try {
JsonObject myObject = (JsonObject) object.getAsJsonObject("data").getAsJsonObject("viewer")
JsonObject myObject = (JsonObject) rootJsonObject.getAsJsonObject("data").getAsJsonObject("viewer")
.getAsJsonObject("home").getAsJsonObject("hourly").getAsJsonArray("nodes").get(0);

String timestampHourlyFrom = myObject.get("from").toString().substring(1, 20);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}} 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 }}} 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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ channel-type.tibber.timestamp.label = Timestamp
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.label = Prices for tomorrow as a JSON array
channel-type.tibber.tomorrow.description = JSON array of tuples startsAt,total, ie {["startsAt": "2022-09-10T00:00:00+02:00", "total": 5.332}, {"startsAt": ...}]}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<channel id="current_total" typeId="price"/>
<channel id="current_startsAt" typeId="timestamp"/>
<channel id="current_level" typeId="level"/>
<channel id="tomorrow" typeId="tomorrow"/>
<channel id="daily_from" typeId="timestamp"/>
<channel id="daily_to" typeId="timestamp"/>
<channel id="daily_cost" typeId="cost"/>
Expand Down Expand Up @@ -115,4 +116,10 @@
<description>Accumulated Production since Midnight</description>
<state pattern="%.3f %unit%"></state>
</channel-type>
<channel-type id="tomorrow">
seime marked this conversation as resolved.
Show resolved Hide resolved
<item-type>String</item-type>
<label>Prices for tomorrow as a JSON array</label>
<description>JSON array of tuples startsAt,total, ie {["startsAt": "2022-09-10T00:00:00+02:00", "total": 5.332},
seime marked this conversation as resolved.
Show resolved Hide resolved
{"startsAt": ...}]}</description>
</channel-type>
</thing:thing-descriptions>