Skip to content

Commit

Permalink
[shelly] Add totalKWH channels for Shelly Pro 3EM (openhab#17602)
Browse files Browse the repository at this point in the history
* added totalkwh channel

Signed-off-by: Jonathan van de Giessen <jonathan.vandegiessen@student.hu.nl>
  • Loading branch information
JonathanvdGHU authored and KaaNee committed Nov 8, 2024
1 parent ee89931 commit b6acd44
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ public class ShellyBindingConstants {
public static final String CHANNEL_DEVST_ACCUWATTS = "accumulatedWatts";
public static final String CHANNEL_DEVST_ACCUTOTAL = "accumulatedWTotal";
public static final String CHANNEL_DEVST_ACCURETURNED = "accumulatedReturned";
public static final String CHANNEL_DEVST_TOTALKWH = "totalKWH";
public static final String CHANNEL_DEVST_RESETTOTAL = CHANNEL_EMETER_RESETTOTAL;

public static final String CHANNEL_DEVST_CHARGER = "charger";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ public static class ShellySettingsStatus {

public Double totalCurrent;
public Double totalPower;
public Double totalKWH;
public Double totalReturned;

@SerializedName("ext_temperature")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package org.openhab.binding.shelly.internal.api2;

import static org.openhab.binding.shelly.internal.ShellyBindingConstants.CHANNEL_INPUT;
import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.*;
import static org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.*;
import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
Expand Down Expand Up @@ -61,6 +61,7 @@
import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult;
import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult.Shelly2CoverStatus;
import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult.Shelly2DeviceStatusEm;
import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult.Shelly2DeviceStatusEmData;
import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult.Shelly2DeviceStatusHumidity;
import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult.Shelly2DeviceStatusIlluminance;
import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult.Shelly2DeviceStatusPower;
Expand Down Expand Up @@ -190,7 +191,7 @@ protected boolean fillDeviceStatus(ShellySettingsStatus status, Shelly2DeviceSta
updated |= updateRelayStatus(status, result.switch3, channelUpdate);
updated |= updateRelayStatus(status, result.switch100, channelUpdate);
updated |= updateRelayStatus(status, result.pm10, channelUpdate);
updated |= updateEmStatus(status, result.em0, channelUpdate);
updated |= updateEmStatus(status, result.em0, result.emdata0, channelUpdate);
updated |= updateEmStatus(status, result.em10, channelUpdate);
updated |= updateEmStatus(status, result.em11, channelUpdate);
updated |= updateRollerStatus(status, result.cover0, channelUpdate);
Expand Down Expand Up @@ -354,8 +355,8 @@ private boolean updateEmStatus(ShellySettingsStatus status, @Nullable Shelly2Sta
}

private boolean updateEmStatus(ShellySettingsStatus status, @Nullable Shelly2DeviceStatusEm em,
boolean channelUpdate) throws ShellyApiException {
if (em == null) {
@Nullable Shelly2DeviceStatusEmData emData, boolean channelUpdate) throws ShellyApiException {
if (em == null || emData == null) {
return false;
}

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

if (emData.totalKWH != null) {
status.totalKWH = emData.totalKWH;
}

ShellySettingsMeter sm = new ShellySettingsMeter();
ShellySettingsEMeter emeter = status.emeters.get(0);
if (em.aActPower != null) {
sm.power = emeter.power = em.aActPower;
}
if (emData.aTotal != null) {
emeter.total = emData.aTotal;
}
if (em.aAprtPower != null) {
emeter.totalReturned = em.aAprtPower;
}
Expand All @@ -396,6 +404,9 @@ private boolean updateEmStatus(ShellySettingsStatus status, @Nullable Shelly2Dev
if (em.bActPower != null) {
sm.power = emeter.power = em.bActPower;
}
if (emData.bTotal != null) {
emeter.total = emData.bTotal;
}
if (em.bAprtPower != null) {
emeter.totalReturned = em.bAprtPower;
}
Expand All @@ -419,6 +430,9 @@ private boolean updateEmStatus(ShellySettingsStatus status, @Nullable Shelly2Dev
if (em.cActPower != null) {
sm.power = emeter.power = em.cActPower;
}
if (emData.cTotal != null) {
emeter.total = emData.cTotal;
}
if (em.cAprtPower != null) {
emeter.totalReturned = em.cAprtPower;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,16 @@ public static class Shelly2DeviceStatusEm {

public static class Shelly2DeviceStatusEmData {
public Integer id;

@SerializedName("a_total_act_energy")
public Double aTotal;
@SerializedName("b_total_act_energy")
public Double bTotal;
@SerializedName("c_total_act_energy")
public Double cTotal;

@SerializedName("total_act")
public Double totalKWH;
public String[] errors;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ public static boolean updateMeters(ShellyThingInterface thingHandler, ShellySett
thingHandler.updateChannel(CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_ACCURETURNED,
toQuantityType(status.totalReturned != null ? status.totalReturned / 1000 : accumulatedReturned,
DIGITS_KWH, Units.KILOWATT_HOUR));
thingHandler.updateChannel(CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_TOTALKWH, toQuantityType(
status.totalKWH != null ? status.totalKWH / 1000 : 0, DIGITS_KWH, Units.KILOWATT_HOUR));

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
package org.openhab.binding.shelly.internal.provider;

import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.SHELLY_API_INVTEMP;
import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.*;
import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;

import java.util.ArrayList;
Expand Down Expand Up @@ -133,6 +133,7 @@ public ShellyChannelDefinitions(@Reference ShellyTranslationProvider translation
.add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_WAKEUP, "sensorWakeup", ITEMT_STRING))
.add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS, "meterAccuWatts", ITEMT_POWER))
.add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL, "meterAccuTotal", ITEMT_ENERGY))
.add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_TOTALKWH, "totalKWH", ITEMT_ENERGY))
.add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED, "meterAccuReturned", ITEMT_ENERGY))
.add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_RESETTOTAL, "meterResetTotals", ITEMT_SWITCH))
.add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_VOLTAGE, "supplyVoltage", ITEMT_VOLT))
Expand Down Expand Up @@ -332,6 +333,7 @@ public static Map<String, Channel> createDeviceChannels(final Thing thing, final
|| (profile.hasRelays && profile.numMeters > 1 && !profile.isRoller && !profile.isRGBW2);
addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS);
addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL);
addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_TOTALKWH);
addChannel(thing, add, accuChannel && (status.emeters != null), CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED);
addChannel(thing, add, profile.is3EM, CHGR_DEVST, CHANNEL_DEVST_RESETTOTAL); // 3EM
addChannel(thing, add, status.voltage != null || profile.settings.supplyVoltage != null, CHGR_DEVST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ channel-type.shelly.meterAccuTotal.label = Accumulated Total Power
channel-type.shelly.meterAccuTotal.description = Accumulated Total Power in kW/h of the device (including all meters)
channel-type.shelly.meterAccuReturned.label = Accumulated Apparent Power
channel-type.shelly.meterAccuReturned.description = Accumulated Apparent Power in kW/h of the device (including all meters)
channel-type.shelly.totalKWH.label = Total Energy Consumption Device
channel-type.shelly.totalKWH.description = Total energy consumption of the device in kW/h since the device powered up (resets on restart)
channel-type.shelly.meterReactive.label = Reactive Energy
channel-type.shelly.meterReactive.description = Instantaneous reactive power in Watts (W)
channel-type.shelly.lastPower1.label = Last Power
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,19 @@
</state>
</channel-type>

<channel-type id="totalKWH">
<item-type>Number:Energy</item-type>
<label>@text/channel-type.shelly.totalKWH.label</label>
<description>@text/channel-type.shelly.totalKWH.description</description>
<category>Energy</category>
<tags>
<tag>Measurement</tag>
<tag>Energy</tag>
</tags>
<state readOnly="true" pattern="%.3f %unit%">
</state>
</channel-type>

<channel-type id="meterReactive">
<item-type>Number:Power</item-type>
<label>@text/channel-type.shelly.meterReactive.label</label>
Expand Down

0 comments on commit b6acd44

Please sign in to comment.