]> www.infradead.org Git - users/dwmw2/esp32-pool.git/commitdiff
add support for feeding to Emoncms
authorDavid Woodhouse <dwmw@amazon.co.uk>
Mon, 14 Oct 2024 18:24:12 +0000 (19:24 +0100)
committerDavid Woodhouse <dwmw@amazon.co.uk>
Mon, 14 Oct 2024 18:24:12 +0000 (19:24 +0100)
emoncms.yaml [new file with mode: 0644]

diff --git a/emoncms.yaml b/emoncms.yaml
new file mode 100644 (file)
index 0000000..e6e8174
--- /dev/null
@@ -0,0 +1,79 @@
+# Submit data to EmonCMS using the input API as documented at
+# https://emoncms.org/input/api
+#
+# Usage: Invoke the 'queue_emoncms' script to append data to be sent, for
+# example in an 'on_value' automation:
+#
+#    on_value:
+#      then:
+#        - script.execute:
+#            id: queue_emoncms
+#            node: "ashp1"
+#            input: "elec_usage"
+#            value: !lambda "return x;"
+#
+# Every 10 seconds, the data will be flushed to Emoncms.
+#
+# The API key needs to be set as a secret named 'emoncms_apikey', *including*
+# the prefix "Bearer ". For example your secrets.yaml might contain:
+#
+# emoncms_apikey: "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+
+
+globals:
+  - id: emoncms_data
+    type: std::string
+    restore_value: no
+
+http_request:
+  useragent: esphome/device
+  timeout: 5s
+  id: http_request_data
+
+script:
+  - id: queue_emoncms
+    mode: queued
+    parameters:
+      node: string
+      input: string
+      value: float
+    then:
+      - lambda: |-
+          time_t now = ::time(NULL);
+          if (!id(emoncms_data).empty())
+             id(emoncms_data) += ",";
+          id(emoncms_data) += "[" + std::to_string(now) + ",\"" + node + "\",{\"" + input + "\":" + std::to_string(value) + "}]";
+          ESP_LOGD("Emoncms", "Queued data now '%s'", id(emoncms_data).c_str());
+
+  - id: send_emoncms
+    then:
+      - http_request.post:
+          capture_response: true
+          url: https://emoncms.org/input/bulk
+          headers:
+            Authorization: !secret emoncms_apikey
+            Content-Type: application/x-www-form-urlencoded
+          body: !lambda |-
+             std::string body = "data=[" + id(emoncms_data) + "]&time=0";
+             id(emoncms_data) = "";
+             ESP_LOGD("Emoncms", "Request body: '%s'", body.c_str());
+             return body;
+          on_response:
+           then:
+            - logger.log:
+                format: "Response status: %d, Duration: %u ms"
+                args:
+                  - response->status_code
+                  - response->duration_ms
+            - lambda: |-
+                ESP_LOGD("Emoncms", "Response body: '%s'", body.c_str());
+
+interval:
+  - interval: 10s
+    then:
+      - if:
+         condition:
+           lambda: |-
+             return !id(emoncms_data).empty();
+         then:
+           - script.execute: send_emoncms