Skip to content

Commit ec378ab

Browse files
[shelly] Add totalKWH channels for Shelly Pro 3EM (openhab#17602)
* added totalkwh channel Signed-off-by: Jonathan van de Giessen <[email protected]>
1 parent d3c9204 commit ec378ab

File tree

8 files changed

+51
-5
lines changed

8 files changed

+51
-5
lines changed

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/ShellyBindingConstants.java

+1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ public class ShellyBindingConstants {
271271
public static final String CHANNEL_DEVST_ACCUWATTS = "accumulatedWatts";
272272
public static final String CHANNEL_DEVST_ACCUTOTAL = "accumulatedWTotal";
273273
public static final String CHANNEL_DEVST_ACCURETURNED = "accumulatedReturned";
274+
public static final String CHANNEL_DEVST_TOTALKWH = "totalKWH";
274275
public static final String CHANNEL_DEVST_RESETTOTAL = CHANNEL_EMETER_RESETTOTAL;
275276

276277
public static final String CHANNEL_DEVST_CHARGER = "charger";

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api1/Shelly1ApiJsonDTO.java

+1
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ public static class ShellySettingsStatus {
772772

773773
public Double totalCurrent;
774774
public Double totalPower;
775+
public Double totalKWH;
775776
public Double totalReturned;
776777

777778
@SerializedName("ext_temperature")

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiClient.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
package org.openhab.binding.shelly.internal.api2;
1414

15-
import static org.openhab.binding.shelly.internal.ShellyBindingConstants.CHANNEL_INPUT;
15+
import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
1616
import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.*;
1717
import static org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.*;
1818
import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
@@ -61,6 +61,7 @@
6161
import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult;
6262
import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult.Shelly2CoverStatus;
6363
import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult.Shelly2DeviceStatusEm;
64+
import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult.Shelly2DeviceStatusEmData;
6465
import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult.Shelly2DeviceStatusHumidity;
6566
import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult.Shelly2DeviceStatusIlluminance;
6667
import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult.Shelly2DeviceStatusPower;
@@ -190,7 +191,7 @@ protected boolean fillDeviceStatus(ShellySettingsStatus status, Shelly2DeviceSta
190191
updated |= updateRelayStatus(status, result.switch3, channelUpdate);
191192
updated |= updateRelayStatus(status, result.switch100, channelUpdate);
192193
updated |= updateRelayStatus(status, result.pm10, channelUpdate);
193-
updated |= updateEmStatus(status, result.em0, channelUpdate);
194+
updated |= updateEmStatus(status, result.em0, result.emdata0, channelUpdate);
194195
updated |= updateEmStatus(status, result.em10, channelUpdate);
195196
updated |= updateEmStatus(status, result.em11, channelUpdate);
196197
updated |= updateRollerStatus(status, result.cover0, channelUpdate);
@@ -354,8 +355,8 @@ private boolean updateEmStatus(ShellySettingsStatus status, @Nullable Shelly2Sta
354355
}
355356

356357
private boolean updateEmStatus(ShellySettingsStatus status, @Nullable Shelly2DeviceStatusEm em,
357-
boolean channelUpdate) throws ShellyApiException {
358-
if (em == null) {
358+
@Nullable Shelly2DeviceStatusEmData emData, boolean channelUpdate) throws ShellyApiException {
359+
if (em == null || emData == null) {
359360
return false;
360361
}
361362

@@ -369,11 +370,18 @@ private boolean updateEmStatus(ShellySettingsStatus status, @Nullable Shelly2Dev
369370
status.totalReturned = em.totalAprtPower;
370371
}
371372

373+
if (emData.totalKWH != null) {
374+
status.totalKWH = emData.totalKWH;
375+
}
376+
372377
ShellySettingsMeter sm = new ShellySettingsMeter();
373378
ShellySettingsEMeter emeter = status.emeters.get(0);
374379
if (em.aActPower != null) {
375380
sm.power = emeter.power = em.aActPower;
376381
}
382+
if (emData.aTotal != null) {
383+
emeter.total = emData.aTotal;
384+
}
377385
if (em.aAprtPower != null) {
378386
emeter.totalReturned = em.aAprtPower;
379387
}
@@ -396,6 +404,9 @@ private boolean updateEmStatus(ShellySettingsStatus status, @Nullable Shelly2Dev
396404
if (em.bActPower != null) {
397405
sm.power = emeter.power = em.bActPower;
398406
}
407+
if (emData.bTotal != null) {
408+
emeter.total = emData.bTotal;
409+
}
399410
if (em.bAprtPower != null) {
400411
emeter.totalReturned = em.bAprtPower;
401412
}
@@ -419,6 +430,9 @@ private boolean updateEmStatus(ShellySettingsStatus status, @Nullable Shelly2Dev
419430
if (em.cActPower != null) {
420431
sm.power = emeter.power = em.cActPower;
421432
}
433+
if (emData.cTotal != null) {
434+
emeter.total = emData.cTotal;
435+
}
422436
if (em.cAprtPower != null) {
423437
emeter.totalReturned = em.cAprtPower;
424438
}

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiJsonDTO.java

+10
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,16 @@ public static class Shelly2DeviceStatusEm {
704704

705705
public static class Shelly2DeviceStatusEmData {
706706
public Integer id;
707+
708+
@SerializedName("a_total_act_energy")
709+
public Double aTotal;
710+
@SerializedName("b_total_act_energy")
711+
public Double bTotal;
712+
@SerializedName("c_total_act_energy")
713+
public Double cTotal;
714+
715+
@SerializedName("total_act")
716+
public Double totalKWH;
707717
public String[] errors;
708718
}
709719

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyComponents.java

+3
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ public static boolean updateMeters(ShellyThingInterface thingHandler, ShellySett
352352
thingHandler.updateChannel(CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_ACCURETURNED,
353353
toQuantityType(status.totalReturned != null ? status.totalReturned / 1000 : accumulatedReturned,
354354
DIGITS_KWH, Units.KILOWATT_HOUR));
355+
thingHandler.updateChannel(CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_TOTALKWH, toQuantityType(
356+
status.totalKWH != null ? status.totalKWH / 1000 : 0, DIGITS_KWH, Units.KILOWATT_HOUR));
357+
355358
}
356359
}
357360

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/provider/ShellyChannelDefinitions.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
package org.openhab.binding.shelly.internal.provider;
1414

1515
import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
16-
import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.SHELLY_API_INVTEMP;
16+
import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.*;
1717
import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
1818

1919
import java.util.ArrayList;
@@ -133,6 +133,7 @@ public ShellyChannelDefinitions(@Reference ShellyTranslationProvider translation
133133
.add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_WAKEUP, "sensorWakeup", ITEMT_STRING))
134134
.add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS, "meterAccuWatts", ITEMT_POWER))
135135
.add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL, "meterAccuTotal", ITEMT_ENERGY))
136+
.add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_TOTALKWH, "totalKWH", ITEMT_ENERGY))
136137
.add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED, "meterAccuReturned", ITEMT_ENERGY))
137138
.add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_RESETTOTAL, "meterResetTotals", ITEMT_SWITCH))
138139
.add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_VOLTAGE, "supplyVoltage", ITEMT_VOLT))
@@ -332,6 +333,7 @@ public static Map<String, Channel> createDeviceChannels(final Thing thing, final
332333
|| (profile.hasRelays && profile.numMeters > 1 && !profile.isRoller && !profile.isRGBW2);
333334
addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS);
334335
addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL);
336+
addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_TOTALKWH);
335337
addChannel(thing, add, accuChannel && (status.emeters != null), CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED);
336338
addChannel(thing, add, profile.is3EM, CHGR_DEVST, CHANNEL_DEVST_RESETTOTAL); // 3EM
337339
addChannel(thing, add, status.voltage != null || profile.settings.supplyVoltage != null, CHGR_DEVST,

bundles/org.openhab.binding.shelly/src/main/resources/OH-INF/i18n/shelly.properties

+2
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ channel-type.shelly.meterAccuTotal.label = Accumulated Total Power
300300
channel-type.shelly.meterAccuTotal.description = Accumulated Total Power in kW/h of the device (including all meters)
301301
channel-type.shelly.meterAccuReturned.label = Accumulated Apparent Power
302302
channel-type.shelly.meterAccuReturned.description = Accumulated Apparent Power in kW/h of the device (including all meters)
303+
channel-type.shelly.totalKWH.label = Total Energy Consumption Device
304+
channel-type.shelly.totalKWH.description = Total energy consumption of the device in kW/h since the device powered up (resets on restart)
303305
channel-type.shelly.meterReactive.label = Reactive Energy
304306
channel-type.shelly.meterReactive.description = Instantaneous reactive power in Watts (W)
305307
channel-type.shelly.lastPower1.label = Last Power

bundles/org.openhab.binding.shelly/src/main/resources/OH-INF/thing/shellyGen1_relay.xml

+13
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,19 @@
528528
</state>
529529
</channel-type>
530530

531+
<channel-type id="totalKWH">
532+
<item-type>Number:Energy</item-type>
533+
<label>@text/channel-type.shelly.totalKWH.label</label>
534+
<description>@text/channel-type.shelly.totalKWH.description</description>
535+
<category>Energy</category>
536+
<tags>
537+
<tag>Measurement</tag>
538+
<tag>Energy</tag>
539+
</tags>
540+
<state readOnly="true" pattern="%.3f %unit%">
541+
</state>
542+
</channel-type>
543+
531544
<channel-type id="meterReactive">
532545
<item-type>Number:Power</item-type>
533546
<label>@text/channel-type.shelly.meterReactive.label</label>

0 commit comments

Comments
 (0)