Update `json::parse_json` examples (#3949)

This commit is contained in:
Jesse Hills 2024-06-13 16:06:06 +12:00
parent 5652c9c7da
commit 38f8eb2c6e
No known key found for this signature in database
GPG Key ID: BEAAE804EFD8E83A
2 changed files with 12 additions and 9 deletions

View File

@ -55,9 +55,9 @@ Configuration variables:
Without the root certificate bundle, certificates used by the remote HTTPS server cannot be verified, opening the
HTTPS connection up to person-in-the-middle attacks.
To maximize security, do not set ``verify_ssl`` to ``false`` *unless:*
- a custom CA/self-signed certificate is used,
- the Arduino framework is used, or
- the device does not have sufficient memory to store the certificate bundle
@ -257,8 +257,9 @@ whose ``id`` is set to ``player_volume``:
on_response:
then:
- lambda: |-
json::parse_json(body, [](JsonObject root) {
json::parse_json(body, [](JsonObject root) -> bool {
id(player_volume).publish_state(root["vol"]);
return true;
});

View File

@ -3,7 +3,7 @@ Share data directly between ESPHome nodes
In certain special cases it might be desired to avoid placing any middleware like an MQTT or a home automation server just to transfer small bits of data from one node to another. Direct data polling is possible using HTTP, but beware that the involved components are resource hungry and may be less stable on long term. The webserver embedded in the node is not designed to constantly serve a large amount of requests.
The primary node holding the data we need to retrieve from will be the server, and the others polling for it will be the clients (can be multiple).
The primary node holding the data we need to retrieve from will be the server, and the others polling for it will be the clients (can be multiple).
Server part
-----------
@ -44,16 +44,17 @@ In the example below we request the value of a sensor from the server node, and
interval:
- interval: 60s
then:
- http_request.get:
- http_request.get:
url: http://ip or nodename.local/sensor/ID_of_the_sensor
on_response:
then:
- lambda: |-
json::parse_json(id(http_request_id).get_string(), [](JsonObject root) {
json::parse_json(id(http_request_id).get_string(), [](JsonObject root) -> bool {
id(template_sensor_id).publish_state(root["value"]);
return true;
});
Result
------
@ -99,15 +100,16 @@ Add an ``Authorization`` header to your ``http_request.get`` action. The simples
interval:
- interval: 60s
then:
- http_request.get:
- http_request.get:
url: http://ip or nodename.local/sensor/ID_of_the_sensor
headers:
Authorization: 'Digest username="admin", realm="asyncesp", nonce="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", uri="/sensor/ID_of_the_sensor", response="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", opaque="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", qop=auth, nc=xxxxxxxx, cnonce="xxxxxxxxxxxxxxxx"'
on_response:
then:
- lambda: |-
json::parse_json(id(http_request_id).get_string(), [](JsonObject root) {
json::parse_json(id(http_request_id).get_string(), [](JsonObject root) -> bool {
id(template_sensor_id).publish_state(root["value"]);
return true;
});
See Also