Skip to content

Commit 06e49d5

Browse files
committed
Add BTHome energy monitor
1 parent e731d7b commit 06e49d5

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

apps.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,18 @@
278278
{"name":".bootcde","url":"app.js"}
279279
]
280280
},
281+
{ "id": "bthome_energy",
282+
"name": "BTHome Energy Monitor",
283+
"icon": "icon.png",
284+
"version":"0.01",
285+
"description": "Turn your Espruino device into a BTHome/Home Assistant compatible energy monitor by sensing flashes from your electricty meter (needs an LDR wired between D1 and D2)",
286+
"tags": "bluetooth,homeassistant,energy,power,electricity",
287+
"readme": "README.md",
288+
"needsFeatures":["BLE"],
289+
"storage": [
290+
{"name":".bootcde","url":"app.js"}
291+
]
292+
},
281293
{ "id": "knobbutton",
282294
"name": "Knob Button",
283295
"icon": "icon.png",

apps/bthome_energy/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.01: New App!

apps/bthome_energy/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# BTHome Energy Monitor
2+
3+
This advertises energy usage and battery in a [BTHome (https://bthome.io/)](https://bthome.io/) compatible format, which can then be used in https://www.home-assistant.io/
4+
5+
This is based on https://www.espruino.com/BTHome and https://www.espruino.com/Smart+Meter
6+
7+
## Usage
8+
9+
With the device as on https://www.espruino.com/Smart+Meter, so an LDR between `D1` and `D2`, and position it over the flashing LED of an electricity meter.
10+
11+
Install the app and disconnect, and a device called `Electricity` should appear in Home Assistant.

apps/bthome_energy/app.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var counter = 0; // number of impulses
2+
var power = 0; // power in watts (assuming 1000imp/kwh)
3+
var lastPulse = getTime();
4+
5+
// Update BLE advertising
6+
function update() {
7+
NRF.setAdvertising(require("BTHome").getAdvertisement([
8+
{
9+
type : "battery",
10+
v : E.getBattery()
11+
}, {
12+
type : "energy",
13+
v : counter/1000 // kWh, and usualy 1000 imp/kWh
14+
}, {
15+
type : "power",
16+
v : power // kWh, and usualy 1000 imp/kWh
17+
}
18+
]), {
19+
name : "Electricity",
20+
// not being connectable/scannable saves power (but you'll need to reboot to connect again with the IDE!)
21+
//connectable : false, scannable : false,
22+
});
23+
}
24+
25+
// Set up pin states
26+
D1.write(0);
27+
pinMode(D2,"input_pullup");
28+
// Watch for pin changes
29+
setWatch(function(e) {
30+
let timeDiff = e.time - lastPulse;
31+
power = 3600 / timeDiff; // 1000imp/kwh -> 1000 pulses in 3600sec = 1kW
32+
counter++;
33+
update();
34+
lastPulse = e.time;
35+
digitalPulse(LED1,1,1); // show activity
36+
}, D2, { repeat:true, edge:"falling" });
37+
update();

apps/bthome_energy/icon.png

10 KB
Loading

0 commit comments

Comments
 (0)