Skip to content

Commit 4599edc

Browse files
committed
Improve data formatting and display energy in kWh
1 parent 8240bef commit 4599edc

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

src/web/html_home.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@ const char HTML_HOME[] PROGMEM = R"=====(
6161
</div>
6262

6363
<script>
64-
function formatValue(value, unit, decimals = 1) {
65-
if (value === null || value === undefined) return 'N/A';
66-
return parseFloat(value).toFixed(decimals) + ' ' + unit;
64+
function formatValue(value, unit = '', decimals = 1, scale = 1) {
65+
if (value === null || value === undefined || isNaN(value)) return 'N/A';
66+
67+
const num = Number(value) * scale;
68+
const formatted = num.toFixed(decimals);
69+
70+
return unit ? `${formatted} ${unit}` : formatted;
6771
}
6872

6973
function updatePowerData() {
@@ -117,15 +121,15 @@ function updateEnergyData() {
117121
phases.forEach(phase => {
118122
html += `<div class="phase-card ${phase.class}">
119123
<h3>${phase.name}</h3>
120-
<div class="data-row"><span class="data-label">Consumption:</span><span class="data-value">${formatValue(data[phase.prefix + '_total_act_energy'], 'Wh')}</span></div>
121-
<div class="data-row"><span class="data-label">Grid Feed-in:</span><span class="data-value">${formatValue(data[phase.prefix + '_total_act_ret_energy'], 'Wh')}</span></div>
124+
<div class="data-row"><span class="data-label">Consumption:</span><span class="data-value">${formatValue(data[phase.prefix + '_total_act_energy'], 'kWh', 2, 0.001)}</span></div>
125+
<div class="data-row"><span class="data-label">Grid Feed-in:</span><span class="data-value">${formatValue(data[phase.prefix + '_total_act_ret_energy'], 'kWh', 2, 0.001)}</span></div>
122126
</div>`;
123127
});
124128
html += '</div>';
125129

126130
html += '<div class="totals"><h3 style="margin-top:0;">Totals</h3>';
127-
html += `<div class="data-row"><span class="data-label">Total Consumption:</span><span class="data-value">${formatValue(data.total_act, 'Wh')}</span></div>`;
128-
html += `<div class="data-row"><span class="data-label">Total Grid Feed-in:</span><span class="data-value">${formatValue(data.total_act_ret, 'Wh')}</span></div>`;
131+
html += `<div class="data-row"><span class="data-label">Total Consumption:</span><span class="data-value">${formatValue(data.total_act, 'kWh', 2, 0.001)}</span></div>`;
132+
html += `<div class="data-row"><span class="data-label">Total Grid Feed-in:</span><span class="data-value">${formatValue(data.total_act_ret, 'kWh', 2, 0.001)}</span></div>`;
129133
html += '</div>';
130134

131135
document.getElementById('energy-data').innerHTML = html;

0 commit comments

Comments
 (0)