{"id":23592599,"url":"https://github.com/filipnet/ups-json-publisher","last_synced_at":"2026-04-09T12:50:32.891Z","repository":{"id":190705675,"uuid":"683183650","full_name":"filipnet/ups-json-publisher","owner":"filipnet","description":"UPS Data Publisher for MQTT: Capture UPS details using upsc, format as JSON, and send to MQTT broker.","archived":false,"fork":false,"pushed_at":"2023-12-02T16:53:21.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-27T08:16:06.068Z","etag":null,"topics":["grafana","grafana-dashboard","grafana-influxdb","json","mosquitto-pub","mqtt","mqtt-client","nodered","parser","parsing","shell-script","ups","upsc"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/filipnet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-25T19:49:34.000Z","updated_at":"2023-08-28T05:23:03.000Z","dependencies_parsed_at":"2023-12-02T17:42:24.988Z","dependency_job_id":null,"html_url":"https://github.com/filipnet/ups-json-publisher","commit_stats":null,"previous_names":["filipnet/ups-json-publisher"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/filipnet%2Fups-json-publisher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/filipnet%2Fups-json-publisher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/filipnet%2Fups-json-publisher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/filipnet%2Fups-json-publisher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/filipnet","download_url":"https://codeload.github.com/filipnet/ups-json-publisher/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239432734,"owners_count":19637798,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["grafana","grafana-dashboard","grafana-influxdb","json","mosquitto-pub","mqtt","mqtt-client","nodered","parser","parsing","shell-script","ups","upsc"],"created_at":"2024-12-27T08:14:53.045Z","updated_at":"2025-11-04T10:30:29.577Z","avatar_url":"https://github.com/filipnet.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ups-json-publisher\nUPS Data Publisher for MQTT: Capture UPS details using upsc, format as JSON, and send to MQTT broker.\n\n\u003c!-- TOC --\u003e\n\n- [ups-json-publisher](#ups-json-publisher)\n    - [Usage](#usage)\n        - [Clone the Repository](#clone-the-repository)\n        - [Configure MQTT Broker Settings](#configure-mqtt-broker-settings)\n        - [Run the Script](#run-the-script)\n        - [Run the Script Periodically with Cron](#run-the-script-periodically-with-cron)\n    - [Node-RED transformation and InfluxDB import](#node-red-transformation-and-influxdb-import)\n    - [License](#license)\n\n\u003c!-- /TOC --\u003e\n\n## Usage\n\n### Clone the Repository\n```\ngit clone https://github.com/filipnet/ups-json-publisher.git\ncd ups-json-publisher\n```\n\n### Configure MQTT Broker Settings\nCopy the example configuration file:\n```\ncp config.sh.example config.sh\n```\nOpen config.sh in a text editor (e.g., nano, vim) and replace placeholders with your actual MQTT broker information.\n\n### Run the Script\nMake sure the required dependencies are installed: upsc (part of Network UPS Tools) and mosquitto_pub (part of Mosquitto MQTT client).\n\nRun the script with the device name as an argument:\n```\n./mqtt_upsc_script.sh \u003cdevice_name\u003e\n```\nImportant: Protect Your Configuration:\n\nThe config.sh file contains sensitive information. Make sure not to expose your credentials by adding config.sh to the .gitignore file. Never commit the actual config.sh to the repository.\n\n### Run the Script Periodically with Cron\nTo run the script periodically, you can add a line to the system-wide crontab file. Open the crontab configuration:\n```\nsudo vim /etc/crontab\n```\nAdd the following line to run the script every 5 seconds, suppressing the script output:\n```\n*/5 * * * * /bin/timeout -s 2 1m /root/ups-json-publisher/mqtt_upsc_script.sh \u003cdevice name\u003e \u003e/dev/null 2\u003e\u00261\n```\nFor shorter time intervals you can also use the helper-script \"run_upsc_script.sh\". \n\nReplace \u003cdevice_name\u003e with the actual name of your UPS device.\n\nIf the execution intervals are too short, the script may be restarted before the previous process has finished - which can lead to problems with system performance or the MQTT server. Therefore, always adjust the timeout in the cronjob to a value below the set execution interval.\n\n- Mosquitto MQTT Client\n\n## Node-RED transformation and InfluxDB import\n\nIf the scheduler and an MQTT input node have been configured, all data from the output of nutc should now arrive in the node-RED. Forwarding this data to InfluxDB is simple due to the JSON format. Since InfluxDB is a time series database it makes sense to pass the fields that have a numeric value to InfluxDB as a number and not as a string. For this I wrote a separate function.\n\n\u003cimg src=\"images/nodered-influx-flow.png\" alt=\"Screenshot Node-RED MQTT to InfluxDB\" width=\"600px\"/\u003e\n\nDie Funktionion sieht dabei wie folgt aus:\n\n```\n// Node-RED Function Node\n// Processes JSON input and returns a JSON differentiating between Strings and Numbers\n\nvar inputJSON = msg.payload;\n\nfunction differentiateJSON(inputJSON) {\n    var outputJSON = {};\n\n    for (var key in inputJSON) {\n        if (/^-?\\d*\\.?\\d+$/.test(inputJSON[key])) {\n            outputJSON[key] = parseFloat(inputJSON[key]); // Convert value to Number\n        } else {\n            outputJSON[key] = inputJSON[key];\n        }\n    }\n\n    return outputJSON;\n}\n\nvar outputJSON = differentiateJSON(inputJSON);\nmsg.payload = outputJSON;\n\nreturn msg;\n```\n\nTo pass only certain values to InfluxDB, a downstream function node can be used that might look like this:\n\n```\nvar inputJson = msg.payload; // Input JSON\n\n// Select desired keys/values\n/*\n    battery.charge = Battery charge (percent)\n    battery.runtime = Battery runtime (seconds)\n    ups.realpower = Current value of real power (Watts)\n*/\nvar desiredKeys = [\"battery.charge\", \"battery.runtime\", \"ups.realpower\"]; // Specify the desired keys here\n\nvar outputJson = {};\n\nfor (var i = 0; i \u003c desiredKeys.length; i++) {\n    var key = desiredKeys[i];\n    if (inputJson.hasOwnProperty(key)) {\n        outputJson[key] = inputJson[key];\n    }\n}\n\nmsg.payload = outputJson; // Set the output\nreturn msg;\n```\n\n## License\nThis project is licensed under the BSD 3-Clause License.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffilipnet%2Fups-json-publisher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffilipnet%2Fups-json-publisher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffilipnet%2Fups-json-publisher/lists"}