Update http_request_sensor.rst (#2961)

fix typos and add a bit of explanation
This commit is contained in:
H. Árkosi Róbert 2023-06-01 08:49:16 +02:00 committed by GitHub
parent 5b16e023b5
commit 59f791e6c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 24 deletions

View File

@ -1,58 +1,61 @@
Share data directly between ESPHome nodes
=========================================
You need two ESPHome nodes. One will be the server, and the others will be the clients (can be multiple). You must set up a webserver on your primary esphome to make available the required sensor data using the :doc:`/components/web_server`.
In certain special cases you might want 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 possibvle using HTTP, but beware that the involved components are resource hungry and may be less stable on long term than the usual methods. The webserver embedded in the node is not designed to constantly serve a large amount of requests.
The node holding the data we need to retrieve will be the server, and the others polling for it will be the clients (can be multiple).
Server part
-----------
You must set up a webserver on your primary node to make available the required sensor data using the :doc:`/components/web_server`.
.. code-block:: yaml
# Webserver configration
web_server:
port: 80
Client part
-----------
On the client nodes we need a :doc:`/components/http_request` with an ``http_request_data`` id set, and a :doc:`/components/sensor/template` to make it accessible locally.
On the client nodes we need a :doc:`/components/http_request` with an ``id`` set, and a :doc:`/components/sensor/template` to make it accessible locally.
.. code-block:: yaml
http_request:
useragent: esphome/device
timeout: 10s
id: http_request_data
id: http_request_id
sensor:
- platform: template
name: "Name of the template sensor"
id: id_of_the_template_sensor
name: "Template sensor on client"
id: template_sensor_id
Pulling the data
****************
To automate the request for data, we will add an interval component requesting the URL pointing to the sensor id for which the state is needed. See :ref:`api-rest` on how to build up the URL for your sensors.
To automate the request for data, we use an :ref:`interval` component requesting the URL pointing to the sensor id for which the state is needed. See :ref:`api-rest` on how to build up the URL for your sensors.
.. note::
The domain is the type of the component, for example ``sensor`` or ``light``. ``id`` refers to the ID of the component - which is created from the name of the component, stripping out all non-alphanumeric characters, making everything lowercase and replacing all spaces by underscores. To confirm the corrrct ID to use, you can set the log level to VERY_VERBOSE on your server node and look for ``object_id:`` in the logs.
The domain is the type of the component, for example ``sensor`` or ``light``. ``id`` refers to the internal ID of the component - which is created from the name of the component, stripping out all non-alphanumeric characters, making everything lowercase and replacing all spaces by underscores. To confirm the corrrct ID to use, you can set the log level to VERY_VERBOSE on your server node and look for ``object_id:`` in the logs.
In the example below we pull the value of a sensor, and after parsing the resulted JSON string we publish it to the template sensor:
.. code-block:: yaml
interval:
- interval: 60s
then:
- http_request.get:
url: http://address.of.server.node/sensor/ID.of.the.sensor
on_response:
then:
- lambda: |-
json::parse_json(id(http_request_data).get_string(), [](JsonObject root) {
id(id_of_the_template_sensor).publish_state(root["value"]);
});
- interval: 60s
then:
- http_request.get:
url: http://address.of.server.node/sensor/ID.of.the.sensor
on_response:
then:
- lambda: |-
json::parse_json(id(http_request_id).get_string(), [](JsonObject root) {
id(template_sensor_id).publish_state(root["value"]);
});
Result
@ -60,16 +63,16 @@ Result
.. figure:: images/server.png
:align: center
:width: 90.0%
:width: 95.0%
Server side real sensor.
Server side real sensor.
.. figure:: images/clients.png
:align: center
:width: 90.0%
:width: 95.0%
Client side template sensor.
Client side template sensor.
See Also
@ -77,6 +80,7 @@ See Also
- :doc:`/components/web_server`
- :doc:`/components/http_request`
- :ref:`api-rest`
- :doc:`/components/sensor/template`
- :ref:`interval`
- :ref:`api-rest`
- :ghedit:`Edit`