From 9208561275d6f57177278369b0146be3e1663590 Mon Sep 17 00:00:00 2001 From: Fredrik Erlandsson Date: Mon, 11 Jan 2021 15:50:29 +0100 Subject: [PATCH] Add example for human readable uptime sensor (#923) * Add example for human readable uptime sensor * Fix lint * simpler return --- components/sensor/uptime.rst | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/components/sensor/uptime.rst b/components/sensor/uptime.rst index ac8f388a3..afbc1e822 100644 --- a/components/sensor/uptime.rst +++ b/components/sensor/uptime.rst @@ -23,6 +23,45 @@ Configuration variables: - **id** (*Optional*, :ref:`config-id`): Set the ID of this sensor for use in lambdas. - All other options from :ref:`Sensor `. +Human readable sensor +--------------------- + +The sensor reports uptime in seconds which is good for automations +but is hard to read for humans, this example creates a text sensor +with human readable output. + +.. code-block:: yaml + + # Example configuration entry + text_sensor: + - platform: template + name: Uptime Human Readable + id: uptime_human + icon: mdi:clock-start + sensor: + - platform: uptime + name: Uptime Sensor + id: uptime + update_interval: 60s + on_raw_value: + then: + - text_sensor.template.publish: + id: uptime_human + state: !lambda |- + int seconds = round(id(uptime).raw_state); + int days = seconds / (24 * 3600); + seconds = seconds % (24 * 3600); + int hours = seconds / 3600; + seconds = seconds % 3600; + int minutes = seconds / 60; + seconds = seconds % 60; + return ( + (days ? String(days) + "d " : "") + + (hours ? String(hours) + "h " : "") + + (minutes ? String(minutes + "m " : "") + + (String(seconds) + "s") + ).c_str(); + See Also --------