From a429137485bc567192725c057ed8783e2c937fd9 Mon Sep 17 00:00:00 2001 From: Lukas Schuller Date: Tue, 11 Nov 2025 15:08:56 +0100 Subject: [PATCH] Add command templates for publishing complex messages --- mqtt-dash | 2 +- mqtt_dash/static/main.js | 7 ++++++- mqtt_dash/widgets.py | 5 ++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/mqtt-dash b/mqtt-dash index f04043a..f9b41c3 100755 --- a/mqtt-dash +++ b/mqtt-dash @@ -25,4 +25,4 @@ if missing_config_fields: sys.exit(1) -app.run(debug=False, host='0.0.0.0', port=4000) +app.run(debug=True, host='0.0.0.0', port=4000) diff --git a/mqtt_dash/static/main.js b/mqtt_dash/static/main.js index ff81bf2..0f1c690 100644 --- a/mqtt_dash/static/main.js +++ b/mqtt_dash/static/main.js @@ -166,7 +166,12 @@ function init(brokerUri) { this.onchange() var topic = this.getAttribute('data-pub-topic'); var retain = Boolean(this.getAttribute('data-retain')); - publish(topic, this.value, retain); + commandTemplate = this.getAttribute('data-command-template'); + var value = this.value; + if (commandTemplate) { + value = commandTemplate.replace("%VALUE%", value); + } + publish(topic, value, retain); }; } diff --git a/mqtt_dash/widgets.py b/mqtt_dash/widgets.py index a9f79b0..dea1b32 100644 --- a/mqtt_dash/widgets.py +++ b/mqtt_dash/widgets.py @@ -92,7 +92,8 @@ def slider_widget(label, min_val=0, max_val=255, step=1, - retain=False): + retain=False, + command_template=None): id_ = make_id() sub_topic = sub_topic or topic template = ''' @@ -103,6 +104,7 @@ def slider_widget(label, data-pub-topic="{{topic}}" data-retain="{{retain | string | lower}}" {% if value_path %} data-value-path="{{value_path}}" {% endif %} + {% if command_template %} data-command-template="{{command_template}}" {% endif %} min="{{min_val}}" type="range" max="{{max_val}}" step="{{step}}" value="{{min_val}}"/> - @@ -121,6 +123,7 @@ def slider_widget(label, topic=topic, unit=unit, retain=retain, + command_template=command_template, id_=id_)