From: David Woodhouse Date: Mon, 14 Oct 2024 18:24:12 +0000 (+0100) Subject: add support for feeding to Emoncms X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=a431af8ccb6711ac0c32b888cb8c828a19a0a3d3;p=users%2Fdwmw2%2Fesp32-pool.git add support for feeding to Emoncms --- diff --git a/emoncms.yaml b/emoncms.yaml new file mode 100644 index 0000000..e6e8174 --- /dev/null +++ b/emoncms.yaml @@ -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