2018-08-22 22:05:28 +02:00
|
|
|
Uptime Sensor
|
|
|
|
=============
|
|
|
|
|
2018-11-14 22:12:27 +01:00
|
|
|
.. seo::
|
|
|
|
:description: Instructions for setting up a sensor that tracks the uptime of the ESP.
|
2021-11-16 03:19:33 +01:00
|
|
|
:image: timer.svg
|
2018-11-14 22:12:27 +01:00
|
|
|
|
2018-08-22 22:05:28 +02:00
|
|
|
The ``uptime`` sensor allows you to track the time the ESP has stayed up for in seconds.
|
|
|
|
Time rollovers are automatically handled.
|
|
|
|
|
2018-11-19 18:32:16 +01:00
|
|
|
.. code-block:: yaml
|
2018-08-22 22:05:28 +02:00
|
|
|
|
|
|
|
# Example configuration entry
|
|
|
|
sensor:
|
|
|
|
- platform: uptime
|
|
|
|
name: Uptime Sensor
|
|
|
|
|
|
|
|
Configuration variables:
|
|
|
|
------------------------
|
|
|
|
|
2019-01-06 18:56:14 +01:00
|
|
|
- **update_interval** (*Optional*, :ref:`config-time`): The interval to check the sensor. Defaults to ``60s``.
|
|
|
|
|
2018-08-22 22:05:28 +02:00
|
|
|
- **id** (*Optional*, :ref:`config-id`): Set the ID of this sensor for use in lambdas.
|
2019-02-17 12:28:17 +01:00
|
|
|
- All other options from :ref:`Sensor <config-sensor>`.
|
2018-08-24 22:44:01 +02:00
|
|
|
|
2021-01-11 15:50:29 +01:00
|
|
|
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
|
2021-01-27 08:09:41 +01:00
|
|
|
id: uptime_sensor
|
2021-01-11 15:50:29 +01:00
|
|
|
update_interval: 60s
|
|
|
|
on_raw_value:
|
|
|
|
then:
|
|
|
|
- text_sensor.template.publish:
|
|
|
|
id: uptime_human
|
|
|
|
state: !lambda |-
|
2021-01-27 08:09:41 +01:00
|
|
|
int seconds = round(id(uptime_sensor).raw_state);
|
2021-01-11 15:50:29 +01:00
|
|
|
int days = seconds / (24 * 3600);
|
|
|
|
seconds = seconds % (24 * 3600);
|
|
|
|
int hours = seconds / 3600;
|
|
|
|
seconds = seconds % 3600;
|
|
|
|
int minutes = seconds / 60;
|
|
|
|
seconds = seconds % 60;
|
|
|
|
return (
|
2021-10-19 22:00:25 +02:00
|
|
|
(days ? to_string(days) + "d " : "") +
|
|
|
|
(hours ? to_string(hours) + "h " : "") +
|
|
|
|
(minutes ? to_string(minutes) + "m " : "") +
|
|
|
|
(to_string(seconds) + "s")
|
2021-01-11 15:50:29 +01:00
|
|
|
).c_str();
|
|
|
|
|
2018-08-24 22:44:01 +02:00
|
|
|
See Also
|
|
|
|
--------
|
|
|
|
|
|
|
|
- :ref:`sensor-filters`
|
2019-05-12 22:44:59 +02:00
|
|
|
- :apiref:`uptime/uptime_sensor.h`
|
2019-02-07 13:54:45 +01:00
|
|
|
- :ghedit:`Edit`
|