OwnTracks-to-ntfy setup variants#
About#
This section informs you about additional configuration and operation variants of the Forward OwnTracks low-battery warnings to ntfy recipe. For example, you may want to use Docker or Podman to run both mqttwarn and ntfy, or you may want to use another language than Python to implement your filtering function.
Docker and Podman#
Running mqttwarn as container#
This command will run mqttwarn in a container, using the docker
command to launch it.
Alternatively, podman
can be used. It expects an MQTT broker to be running on localhost
,
so it uses the --network=host
option. The command will mount the configuration file and
the user-defined functions file correctly, and will invoke mqttwarn with the corresponding
--config-file
option.
docker run --rm -it --network=host --volume=$PWD:/etc/mqttwarn \
ghcr.io/jpmens/mqttwarn-standard \
mqttwarn --config-file=mqttwarn-owntracks.ini
Running ntfy as container#
While this tutorial uses the ntfy service at ntfy.sh, it is possible to run your own instance. For example, use Docker or Podman.
docker run --name=ntfy --rm -it --publish=5555:80 \
binwiederhier/ntfy serve --base-url="http://localhost:5555"
In this case, please adjust the ntfy configuration section [config:ntfy]
to use
a different URL, and make sure to restart mqttwarn afterwards.
[config:ntfy]
targets = {'testdrive': 'http://localhost:5555/testdrive'}
Alternative languages for user-defined functions#
JavaScript#
In order to explore JavaScript user-defined functions using the OwnTracks-to-ntfy recipe,
use the alternative mqttwarn-owntracks.js
implementation by adjusting the functions
setting within the [defaults]
section of your configuration file, and restart mqttwarn.
[defaults]
functions = mqttwarn-owntracks.js
The JavaScript function owntracks_batteryfilter()
implements the same rule as the
previous one, which was written in Python.
/**
*
* Forward OwnTracks low-battery warnings to ntfy.
* https://mqttwarn.readthedocs.io/en/latest/examples/owntracks-battery/readme.html
*
*/
// mqttwarn filter function, returning true if the message should be ignored.
// In this case, ignore all battery level telemetry values above a certain threshold.
function owntracks_batteryfilter(topic, message) {
let ignore = true;
let data;
// Decode inbound message.
try {
data = JSON.parse(message);
} catch {
data = null;
}
// Evaluate filtering rule.
if (data && "batt" in data && data.batt !== null) {
ignore = Number.parseFloat(data.batt) > 20;
}
return ignore;
}
// Status message.
console.log("Loaded JavaScript module.");
// Export symbols.
module.exports = {
"owntracks_batteryfilter": owntracks_batteryfilter,
};
Attention
The feature to run JavaScript code is currently considered to be experimental. Please use it responsibly.
Lua#
In order to explore Lua user-defined functions using the OwnTracks-to-ntfy recipe,
use the alternative mqttwarn-owntracks.lua
implementation by adjusting the functions
setting within the [defaults]
section of your configuration file, and restart mqttwarn.
[defaults]
functions = mqttwarn-owntracks.lua
The Lua function owntracks_batteryfilter()
implements the same rule as the
previous ones, which was written in Python and JavaScript.
--[[
Forward OwnTracks low-battery warnings to ntfy.
https://mqttwarn.readthedocs.io/en/latest/examples/owntracks-battery/readme.html
--]]
-- mqttwarn filter function, returning true if the message should be ignored.
-- In this case, ignore all battery level telemetry values above a certain threshold.
function owntracks_batteryfilter(topic, message)
local ignore = true
-- Decode inbound message.
local data = json.decode(message)
-- Evaluate filtering rule.
if data ~= nil and data.batt ~= nil then
ignore = tonumber(data.batt) > 20
end
return ignore
end
-- Status message.
print("Loaded Lua module.")
-- Export symbols.
return {
owntracks_batteryfilter = owntracks_batteryfilter,
}
Attention
The feature to run Lua code is currently considered to be experimental. Please use it responsibly.