mirror of
https://github.com/esphome/esphome-docs.git
synced 2024-12-25 17:17:54 +01:00
Create how to share sensor data via http_request (#2893)
* Create how to share sensor data via http_request initial webserver for http_request * Update how to share sensor data via http_request * Rename how to share sensor data via http_request to http_request_sensor.rst * Update http_request_sensor.rst * added client and server images * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst fixed template sensor * Update http_request_sensor.rst fixed template senor indent * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update index.rst added htttp_request to cookbook * Update http_request_sensor.rst updated node ----> nodes * Update index.rst Updated node to nodes in index.rst * Update http_request_sensor.rst fixed headline underline too short * Update http_request_sensor.rst * Update index.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst * Update http_request_sensor.rst --------- Co-authored-by: H. Árkosi Róbert <robreg@zsurob.hu>
This commit is contained in:
parent
f73a7fa829
commit
bcc9ed6bc5
82
cookbook/http_request_sensor.rst
Normal file
82
cookbook/http_request_sensor.rst
Normal file
@ -0,0 +1,82 @@
|
||||
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`.
|
||||
|
||||
Server part
|
||||
-----------
|
||||
|
||||
.. 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.
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
http_request:
|
||||
useragent: esphome/device
|
||||
timeout: 10s
|
||||
id: http_request_data
|
||||
|
||||
sensor:
|
||||
- platform: template
|
||||
name: "Name of the template sensor"
|
||||
id: id_of_the_template_sensor
|
||||
|
||||
|
||||
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.
|
||||
|
||||
.. 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.
|
||||
|
||||
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"]);
|
||||
});
|
||||
|
||||
|
||||
Result
|
||||
------
|
||||
|
||||
.. figure:: images/server.png
|
||||
:align: center
|
||||
:width: 90.0%
|
||||
|
||||
Server side real sensor.
|
||||
|
||||
|
||||
.. figure:: images/clients.png
|
||||
:align: center
|
||||
:width: 90.0%
|
||||
|
||||
Client side template sensor.
|
||||
|
||||
|
||||
See Also
|
||||
--------
|
||||
|
||||
- :doc:`/components/web_server`
|
||||
- :doc:`/components/http_request`
|
||||
- :ref:`api-rest`
|
||||
- :doc:`/components/sensor/template`
|
||||
- :ghedit:`Edit`
|
BIN
cookbook/images/clients.png
Normal file
BIN
cookbook/images/clients.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 36 KiB |
BIN
cookbook/images/server.png
Normal file
BIN
cookbook/images/server.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
@ -838,6 +838,7 @@ Cookbook
|
||||
Sonoff Fishpond Pump, cookbook/sonoff-fishpond-pump, cookbook-sonoff-fishpond-pump.jpg
|
||||
Arduino Port Extender, cookbook/arduino_port_extender, arduino_logo.svg
|
||||
EHMTX a matrix status/text display, cookbook/ehmtx, ehmtx.jpg
|
||||
Share data directly between ESPHome nodes, cookbook/http_request_sensor, connection.svg
|
||||
Automatic Updates with Home Assistant, cookbook/homeassistant_upgrade, home-assistant.svg
|
||||
|
||||
Do you have other awesome automations or cool setups? Please feel free to add them to the
|
||||
|
Loading…
Reference in New Issue
Block a user