Skip to content

Commit

Permalink
Plugin TasmotaSP: add power/energy detail dialog to status plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
dexterbg committed Sep 23, 2023
1 parent d11d357 commit 5500825
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 8 deletions.
8 changes: 6 additions & 2 deletions plugins/tasmotasp/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tasmota Smart Plug Control

**Smart Plug control for hardware running the Tasmota open source firmware**

Version 2.0 by Michael Balzer <dexter@dexters-web.de>
Version 2.1 by Michael Balzer <dexter@dexters-web.de>

- `Tasmota open source firmware <https://tasmota.github.io/>`_
- `List of Tasmota based smart plugs <https://templates.blakadder.com/plug.html>`_
Expand All @@ -20,6 +20,9 @@ switch off power at the charge stop event of the main and/or 12V battery.
Since version 2.0, the plugin can also monitor the plug power & energy sensors, if
available. Energy counters can be reset from the config page.

Version 2.1 adds an energy data dialog to the status page plugin and the ``getdata()``
command.


------------
Installation
Expand Down Expand Up @@ -77,11 +80,12 @@ Usage
script eval tasmotasp.get([true]) -- read power state, true = also read sensor data
script eval tasmotasp.set("on" | "off" | 1 | 0 | true | false)
script eval tasmotasp.getdata([true]) -- read sensor data, true = force
script eval tasmotasp.info() -- output config, state & data
script eval tasmotasp.status() -- only output state & data
script eval tasmotasp.sendcmd(command, [true]) -- send any Tasmota command, true = followed by sensor read
Notes: ``get()``, ``set()`` & ``sendcmd()`` are asynchronous operations, the results are logged.
Notes: ``get()``, ``set()``, ``getdata()`` & ``sendcmd()`` are asynchronous operations, the results are logged.
Use ``info()`` or ``status()`` to show/fetch the current state.
Sensor monitoring (if enabled) is done on request and while the power is switched on,
with a single final update after switching the power off.
Expand Down
51 changes: 49 additions & 2 deletions plugins/tasmotasp/tasmotasp-status.htm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!--
Web UI plugin for page /status hook body.post:
Add Tasmota Smart Plug power control button to Vehicle panel
Tasmota Smart Plug plugin Version 2.0 Michael Balzer <dexter@dexters-web.de>
Tasmota Smart Plug plugin Version 2.1 Michael Balzer <dexter@dexters-web.de>
-->

<style>
Expand All @@ -11,6 +11,11 @@
font-size: 80%;
line-height: 110%;
}
a#action-tasmotasp-info {
font-size: 19px;
line-height: 100%;
padding: 6px 7px 3px;
}
</style>

<script>
Expand All @@ -20,7 +25,18 @@
var $receiver = $('#livestatus');
var $actionres = $('#vehicle-cmdres');
var $actionset;
var $datadisplay;
var $datadisplay, $datadialog, $datarows;

var units = {
"Voltage": "V",
"Current": "A",
"Power": "W",
"ApparentPower": "VA",
"ReactivePower": "VAr",
"Total": "kWh",
"Today": "kWh",
"Yesterday": "kWh",
};

// State & UI update:
function update(data) {
Expand Down Expand Up @@ -50,8 +66,16 @@
function updateEnergy() {
if (tasmotasp.data["StatusSNS"] && tasmotasp.data["StatusSNS"]["ENERGY"]) {
let td = tasmotasp.data["StatusSNS"]["ENERGY"];
// update inline displays:
$('#data-tasmotasp-power').text(td["Power"]);
$('#data-tasmotasp-total').text(td["Total"]);
// update info dialog:
$datarows.empty();
for (let [key, value] of Object.entries(td)) {
if (typeof value == "number") {
$datarows.append(`<tr><th>${key}</th><td>${value}</td><td>${units[key]||""}</td></tr>`);
}
}
}
}

Expand Down Expand Up @@ -101,6 +125,8 @@
'<div class="metric number"><span class="value" id="data-tasmotasp-power">?</span><span class="unit">W</span></div>'+
'<div class="metric number"><span class="value" id="data-tasmotasp-total">?</span><span class="unit">kWh</span></div>'+
'</div>'+
'<a class="btn btn-default btn-sm data-tasmotasp" id="action-tasmotasp-info">ⓘ</a>'+
'<div id="dialog-tasmotasp-info"></div>'+
'</li>');
$actionset = $('.action-tasmotasp-set > label');
$datadisplay = $('.data-tasmotasp');
Expand All @@ -110,6 +136,27 @@
tasmotasp.state.power = $(this).val();
loadjs('tasmotasp.set("'+tasmotasp.state.power+'")', '#action-tasmotasp-output');
});
// init data dialog:
$datadialog = $('#dialog-tasmotasp-info');
$datadialog.dialog({
title: 'Plug Energy Data',
body:
'<table class="table table-striped table-condensed"><tbody /></table>'+
'<samp class="samp-inline" id="action-tasmotasp-output2"></samp>',
buttons: [
{ label: "Close" },
{ label: "Update", btnClass: "default pull-left", autoHide: false, action: function () {
loadjs("tasmotasp.getdata(true)", "#action-tasmotasp-output2"); } },
{ label: "Reset Today", btnClass: "default pull-left", autoHide: false, action: function () {
loadjs("tasmotasp.sendcmd('EnergyToday 0', true)", "#action-tasmotasp-output2"); } },
{ label: "Reset Total", btnClass: "default pull-left", autoHide: false, action: function () {
loadjs("tasmotasp.sendcmd('EnergyTotal 0', true)", "#action-tasmotasp-output2"); } },
],
});
$datarows = $('#dialog-tasmotasp-info tbody');
$('#action-tasmotasp-info').on('click', function(){
$datadialog.dialog('show');
});
// get status:
getInfo();
// subscribe to stream updates:
Expand Down
2 changes: 1 addition & 1 deletion plugins/tasmotasp/tasmotasp.htm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!--
Web UI page plugin:
Control & configuration frontend for Tasmota Smart Plug module plugin (tasmotasp.js)
Tasmota Smart Plug Plugin Version 2.0 Michael Balzer <dexter@dexters-web.de>
Tasmota Smart Plug Plugin Version 2.1 Michael Balzer <dexter@dexters-web.de>
Recommended installation:
- Page: /usr/tasmotasp
Expand Down
14 changes: 12 additions & 2 deletions plugins/tasmotasp/tasmotasp.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/**
* Module plugin:
* Tasmota Smart Plug control -- see https://tasmota.github.io/
* Version 2.0 by Michael Balzer <dexter@dexters-web.de>
* Version 2.1 by Michael Balzer <dexter@dexters-web.de>
*
* History:
* - v2.1: energy data dialog in status plugin, suppress repeated on/off events, add getdata command
* - v2.0: power/energy monitoring
* - v1.0: initial release
*
Expand All @@ -18,6 +19,7 @@
* Usage:
* - script eval tasmotasp.get([true]) -- read power state, true = also read sensor data
* - script eval tasmotasp.set("on" | "off" | 1 | 0 | true | false)
* - script eval tasmotasp.getdata([true]) -- read sensor data, true = force
* - script eval tasmotasp.info() -- output config, state & data
* - script eval tasmotasp.status() -- only output state & data
* - script eval tasmotasp.sendcmd(command, [true]) -- send any Tasmota command, true = followed by sensor read
Expand Down Expand Up @@ -56,6 +58,10 @@ var state = {
monitoring: false,
};

var oldstate = {
power: "",
};

var data = {};

// Process call result:
Expand All @@ -65,7 +71,10 @@ function processResult(tag) {
OvmsEvents.Raise("usr.tasmotasp.error");
} else {
print("TasmotaSP " + tag + ": power=" + state.power);
OvmsEvents.Raise("usr.tasmotasp." + state.power);
if (state.power != oldstate.power) {
oldstate.power = state.power;
OvmsEvents.Raise("usr.tasmotasp." + state.power);
}
}
}

Expand Down Expand Up @@ -299,6 +308,7 @@ PubSub.subscribe("usr.tasmotasp.getdata", handleEvent);
// API exports:
exports.get = getPowerState;
exports.set = setPowerState;
exports.getdata = getSensorData;
exports.info = printInfo;
exports.status = printStatus;
exports.sendcmd = sendCommand;
2 changes: 1 addition & 1 deletion plugins/tasmotasp/tasmotasp.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"group": "Home Automation",
"info": "https://www.openvehicles.com/",
"maintainer": "Michael Balzer <dexter@dexters-web.de>",
"version": "2.0",
"version": "2.1",
"prerequisites": ["ovms>=3.3.001"],
"elements": [
{
Expand Down

0 comments on commit 5500825

Please sign in to comment.