Anniversary Sale · up to 80% off all librariesEnds Nov 9Shop now
SOFTTDocs
Cards

Energy Card

Display an energy or power consumption value with a configurable unit using dash::EnergyCard.

ESP-DASH Pro feature. This functionality is available only in the commercial edition of ESP-DASH.

The energy card displays an energy or power consumption value with a unit, such as readings from an energy meter or a current sensor.

Energy card

Constructor

dash::EnergyCard<T, Precision> card(dashboard, name, unit);

Prop

Type

Template parameters:

Prop

Type

dash::EnergyCard energy(dashboard, "Energy consumption", "kWh");        // float, 2 decimals
dash::EnergyCard<float, 3> energyJ(dashboard, "Energy consumption", "J");
dash::EnergyCard<int> energyWh(dashboard, "Energy consumption", "Wh");

An overload without the dashboard argument creates the card without attaching it to a dashboard; attach it later with dashboard.add(card).

Methods

setValue()

bool setValue(T value);

Sets the energy value. Returns true if the value changed and an update is pending.

value()

const T& value() const;

Returns the current value. Call it only when hasValue() returns true.

hasValue()

bool hasValue() const;

Returns true if a value has been set.

removeValue()

bool removeValue();

Clears the value. The dashboard then shows 0. Returns true if a value was removed.

setUnit()

bool setUnit(const char* unit);

Sets the unit displayed next to the value. Returns true if the unit changed.

unit()

const char* unit() const;

Returns the current unit.

Example

main.cpp
#include <Arduino.h>
#include <WiFi.h>              // ESP32; use the networking headers for your board
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ESPDashPro.h>

AsyncWebServer server(80);
ESPDash dashboard(server);

dash::EnergyCard<float> energy(dashboard, "Energy consumption", "kWh");

void setup() {
  // ... connect to Wi-Fi
  server.begin();
}

void loop() {
  float consumption = 123.4f; // replace with a meter reading
  energy.setValue(consumption);
  dashboard.sendUpdates();
  delay(3000);
}

Notes

  • The default unit is an empty string. Pass a unit to the constructor or call setUnit().
  • The card stores the unit pointer and does not copy the string. Pass a string literal or a buffer that outlives the card.

On this page