TasmotaTimer

User friendly timer app using node.js to control Tasmota powered smartplugs


Project maintained by tonyjurg Hosted on GitHub Pages — Theme by mattgraham

Technical Description of the Code

The provided codebase consists of two primary files: index.js (running on the server side) and index.html (interpreted on the client side). Additionaly there are two configuration files:

Together, these files create a web-based interface for using Tasmota-powered devices as a timer. The following description outlines the functionality and interactions between these files.

File: index.html

The index.html file is a web page designed to control the timerfunction on a Tasmota devices via a simple user interface. It contains several HTML elements, JavaScript functions, and CSS references to facilitate control and status monitoring of the Tasmota timer. This part is rendered and executed at the client side, to create a GUI and define actions for its control elements. The static file index.html together with the ascociated stylesheet (index.css) are served by a call to index.js.

Details on HTML Structure

Head Section:

Body Section:

Embedded within the HTML file, the JavaScript handles the interaction using the following logic:

Initialization and WebSocket Setup:

Device Control Functions:

Utility Functions:

Event Listeners:

File: index.js

The index.js file implements the server-side logic using Node.js, handling HTTP requests to interact with Tasmota devices. It reads configuration settings, manages user authentication, and serves static files and API endpoints. This part is executed at the server side. The following functionality is provided:

Server Setup and Configuration

Dependencies and Initialization:*

Configuration File Loading:

Reads and parses config.json and settings.json to load user credentials and device accounts.

HTTP Server

Server Creation:

Creates an HTTP server to listen for incoming requests.

Request Handling:

Handles different routes based on the request URL:

 <ul><li><code>POST /login</code>: Authenticates users using credentials from the request body.
 </li><li>Serves static files (<code>index.html</code>, <code>styles.css</code>, <code>favicon.ico</code>, and <code>header.png</code>).
 </li><li><code>GET /devices</code>: Returns a list of available Tasmota devices defined in <code>settings.json</code>.
 </li><li>The following device-specific routes are defined (<code>/setPower</code>, <code>/getTime</code>, <code>/setTimer</code>, <code>/clearTimer</code>, <code>/getTimerStatus</code>, <code>/getPowerStatus</code>, <code>/enableTimers</code>, <code>/disableTimers</code>).
 </li></ul>

Command Execution:

Defines functions to handle device commands:

 <ul><li><code>handleSetPower()</code>: Sets the power state of a device.
 </li><li><code>handleSetTimer()</code>: Sets a timer on a device.
 </li><li><code>handleClearTimer()</code>: Clears a timer on a device.
 </li><li><code>handleGetTimerStatus()</code>, <code>handleGetPowerStatus()</code> <code>handleGetTime()</code>: Fetches current status information from the device for timer, switchstatus and time.
 </li><li><code>handleEnableTimers()</code>, <code>handleDisableTimers()</code> Enables or disables timers on a device.
 </li></ul>

Utility Functions:

Logging and Debugging

The server logs client actions and errors to a log file if debug mode is enabled (by setting ‘debug’ to ‘true’ in config.json, providing traceability and aiding in debugging issues. Please be aware that there is no mechanism implemented to manage the logfiles. Logging is only to be switched on to allow for investigation or for development purposes (in order to prevent filesystem overflow).