From cab23ae95cf182c9eb49762f1764c34adbe6aeea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=2E=20=C3=81rkosi=20R=C3=B3bert?= Date: Tue, 21 Nov 2023 02:23:19 +0100 Subject: [PATCH] Update Lambda Magic Cookbook with text-to-float conversion example (#3378) --- cookbook/lambda_magic.rst | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/cookbook/lambda_magic.rst b/cookbook/lambda_magic.rst index c3bb42e7e..7f901283b 100644 --- a/cookbook/lambda_magic.rst +++ b/cookbook/lambda_magic.rst @@ -365,6 +365,41 @@ In this example a :doc:`/components/cover/time_based` is used with the GPIO conf - switch.turn_off: open_cover - switch.turn_off: close_cover +Update numeric values from text input +------------------------------------- + +Sometimes it may be more confortable to use a :doc:`/components/text/template` to change some numeric values from the user interface. +ESPHome has some nice `helper functions `__ among which +theres's one to convert text to numbers. + +In the example below we have a text input and a template sensor which can be updated from the text input field. What the lambda +does, is to parse and convert the text string to a number - which only succeedes if the entered string contains characters +represesenting a float number (such as digits, ``-`` and ``.``). If the entered string contains any other characters, the lambda +will return ``NaN``, which corresponds to ``unknown`` sensor state. + +.. code-block:: yaml + + text: + - platform: template + name: "Number type in" + optimistic: true + min_length: 0 + max_length: 16 + mode: text + on_value: + then: + - sensor.template.publish: + id: num_from_text + state: !lambda |- + auto n = parse_number(x); + return n.has_value() ? n.value() : NAN; + + sensor: + - platform: template + id: num_from_text + name: "Number from text" + + See Also --------