--- /dev/null
+# 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