Skip to content

Commit 762874e

Browse files
authored
[factory_reset] Document power cycle reset (#5148)
1 parent 661f5cf commit 762874e

File tree

5 files changed

+82
-68
lines changed

5 files changed

+82
-68
lines changed

components/button/factory_reset.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Configuration variables:
4040
See Also
4141
--------
4242

43+
- :doc:`/components/factory_reset`
4344
- :doc:`shutdown`
4445
- :doc:`restart`
4546
- :doc:`safe_mode`

components/factory_reset.rst

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Factory Reset
2+
=============
3+
4+
.. seo::
5+
:description: Instructions for setting up conditions that can remotely invalidate all ESPHome preferences stored in flash and reboot ESP.
6+
7+
The ``factory_reset`` component allows you to invalidate (reset) all ESPHome :ref:`preferences <preferences-flash_write_interval>` stored in flash memory and reboot your node.
8+
After reboot all states, parameters and variables will be reinitialized with their default values. This is useful:
9+
10+
- for devices preflashed with ESPHome to reset behavior back to factory state
11+
- in case of moving a device to a new environment or starting a new use-case (e.g. reset counters or state)
12+
- for privacy concerns when giving away a device
13+
14+
.. note::
15+
16+
**USE WITH GREAT CAUTION!** All credentials, global variables, counters and saved states stored in non-volatile memory will be lost with no chance of recovering them.
17+
Even raw reading of flash memory with ``esptool`` will not help, since data is physically erased from flash memory.
18+
19+
For devices configured using :doc:`captive portal </components/captive_portal>`, this will reset WiFi settings as well, thus making such devices offline.
20+
You'll need to be in close proximity to your device to configure it again using a built-in WiFi access point and captive portal.
21+
22+
Reset by Fast Power Cycling
23+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
The ``factory_reset`` component can be configured to clear stored preferences by repeatedly pressing the reset button or power cycling,
26+
which can be useful to clear the data stored in non-volatile memory on devices that can't be
27+
connected with a serial cable. The required number of power cycles and the maximum delay between them can be configured in the
28+
``factory_reset`` component configuration. Points to note:
29+
30+
- The maximum delay affects only the time when the device is powered on,
31+
not the time when it is powered off (this can't be measured).
32+
- The reset count will be cleared to zero when any other kind of reset occurs,
33+
or if the device remains powered on and running for longer than the maximum delay.
34+
- Not available on RP2040 and RP2350 as the reset cause is not able to be determined.
35+
- On ESP8266 this feature requires the ``restore_from_flash`` feature to be enabled in the :doc:`ESP8266 platform </components/esp8266>`.
36+
37+
.. code-block:: yaml
38+
39+
factory_reset:
40+
resets_required: 5
41+
max_delay: 10s
42+
43+
Configuration variables:
44+
------------------------
45+
46+
- **resets_required** (*Optional*, integer): The number of power cycles after which the device will be reset.
47+
No default, if not configured the power cycle reset feature will be disabled
48+
- **max_delay** (*Optional*, :ref:`Time <config-time>`): The maximum delay between power cycles. Default: 10s
49+
50+
``on_increment`` Trigger
51+
^^^^^^^^^^^^^^^^^^^^^^^^
52+
53+
A trigger is available that will be triggered whenever the current reset cycle count changes. This happens when the
54+
``factory_reset`` component detects a power cycle, or when the
55+
cycle count is cleared to zero by timeout or a different type of reset. Arguments passed to the trigger are:
56+
57+
- ``x``: The current cycle count
58+
- ``target``: The target cycle count
59+
60+
.. code-block:: yaml
61+
62+
factory_reset:
63+
resets_required: 5
64+
max_delay: 10s
65+
on_increment:
66+
- logger.log:
67+
format: "Fast power cycle count now %u, target %u"
68+
args: [x, target]
69+
70+
See Also
71+
--------
72+
73+
- :doc:`/components/switch/factory_reset`
74+
- :doc:`/components/button/factory_reset`
75+
- :doc:`/components/button/shutdown`
76+
- :doc:`/components/button/restart`
77+
- :doc:`/components/button/safe_mode`
78+
- :apiref:`factory_reset/factory_reset.h`
79+
- :ghedit:`Edit`

components/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ ESPHome-specific components or components supporting ESPHome device provisioning
8383
Mapping, components/mapping, mapping.svg, dark-invert
8484
XXTEA, components/xxtea, xxtea.svg
8585
Script, components/script, description.svg, dark-invert
86+
Factory Reset, components/factory_reset, restart-alert.svg, dark-invert
8687

8788
Network Hardware
8889
----------------

components/switch/factory_reset.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Configuration variables:
4040
See Also
4141
--------
4242

43+
- :doc:`/components/factory_reset`
4344
- :doc:`shutdown`
4445
- :doc:`restart`
4546
- :doc:`safe_mode`

cookbook/lambda_magic.rst

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -278,74 +278,6 @@ will return ``NaN``, which corresponds to ``unknown`` sensor state.
278278
name: "Number from text"
279279
280280
281-
Factory reset after 5 quick reboots
282-
-----------------------------------
283-
284-
One may want to restore factory settings (like Wi-Fi credentials set at runtime, or clear restore states) without having to
285-
disassemble or dismount the devices from their deployed location, whilst there's no network access either. The example below
286-
shows how to achieve that using lambdas in a script by triggering the factory reset switch after the system rebooted 5 times
287-
with 10-second timeframes.
288-
289-
.. code-block:: yaml
290-
291-
# Example config.yaml
292-
esphome:
293-
name: "esphome_ld2410"
294-
on_boot:
295-
priority: 600.0
296-
then:
297-
- script.execute: fast_boot_factory_reset_script
298-
esp32:
299-
board: esp32-c3-devkitm-1
300-
301-
substitutions:
302-
factory_reset_boot_count_trigger: 5
303-
304-
globals:
305-
- id: fast_boot
306-
type: int
307-
restore_value: yes
308-
initial_value: '0'
309-
310-
script:
311-
- id: fast_boot_factory_reset_script
312-
then:
313-
- if:
314-
condition:
315-
lambda: return ( id(fast_boot) >= ${factory_reset_boot_count_trigger});
316-
then:
317-
- lambda: |-
318-
ESP_LOGD("Fast Boot Factory Reset", "Performing factotry reset");
319-
id(fast_boot) = 0;
320-
fast_boot->loop();
321-
global_preferences->sync();
322-
- button.press: factory_reset_button
323-
- lambda: |-
324-
if(id(fast_boot) > 0)
325-
ESP_LOGD("Fast Boot Factory Reset", "Quick reboot %d/%d, do it %d more times to factory reset", id(fast_boot), ${factory_reset_boot_count_trigger}, ${factory_reset_boot_count_trigger} - id(fast_boot));
326-
id(fast_boot) += 1;
327-
fast_boot->loop();
328-
global_preferences->sync();
329-
- delay: 10s
330-
- lambda: |-
331-
id(fast_boot) = 0;
332-
fast_boot->loop();
333-
global_preferences->sync();
334-
335-
wifi:
336-
id: wifi_component
337-
ap:
338-
ap_timeout: 0s
339-
reboot_timeout: 0s
340-
341-
captive_portal:
342-
343-
button:
344-
- platform: factory_reset
345-
id: factory_reset_button
346-
name: "ESPHome: Factory reset"
347-
348-
349281
See Also
350282
--------
351283

0 commit comments

Comments
 (0)