moustic999
(Moustic999)
January 10, 2026, 7:58am
1
Hello all,
Is there a way to have a variable declared in the RTC_memroy while using esphome ?
I would like to use an esp32-c3 as a pulsecounter, my idea is the following
have a variable counter holding the number of pulse in RTC_DATA_ATTR
each time the gpio go to low, esp wake up and increment the counter then go to deep sleep
every 15min , esp wake up, power up wifi, send the counter value, reset to zero, stop wifi, go to deep sleep
for this I need to access RTC_DATA_ATTR, but I don’t know if I can do it with epshome
No. Can’t be done. What’s incrementing the pulse counter during deep sleep?
koying
(Chris B)
January 10, 2026, 10:13am
3
If you know your way in ESPHome, this might help you
dev ← brisk0:pulse-counter-ulp
opened 06:39AM - 06 Oct 24 UTC
# What does this implement/fix?
This introduces a new type of pulse counter… that uses the ESP32's Ultra-Low Power (ULP) coprocessor to keep counting through deep sleep with the goal of enabling simple battery operated sensors in esphome (this is specifically to drive my own electricity meter reader, which is ironically difficult to power otherwise).
The sensor tracks how long the ULP program takes to run while it is awake, so that it can estimate the time passed on wake-up by maintaining a count of times the ULP program ran. All variables that need to persist are stored by the ULP program (which I believe uses RTC_SLOW_MEM).
The Pulse Counter ULP sensor is (perhaps obviously) based on the Pulse Counter sensor. I was hoping it would be possible to maintain it as a configuration of Pulse Counter, but there were a few difficulties that pushed away from that idea:
- There would have to be breaking changes to the configuration, as Pulse Counter assumes only a single variation (`has_pcnt`) and assumes all configuration is valid for all storage types.
- The relationship between the "storage" part of the sensor and the main part of the sensor has to be much tighter in the ULP version, because anything that persists must be known to the ULP program, including time information. This would have meant substantially rewriting the component at the risk of introducing new bugs, and a lot of conditional execution only relevant to the ULP variant.
- A few hardware abstractions had to be broken through to make this work, meaning that the ULP sensor cannot compile on e.g. an ESP8266, which would lead to a lot of conditional compilation.
## Types of changes
- [ ] Bugfix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Other
**Related issue or feature (if applicable):** fixes https://github.com/esphome/feature-requests/issues/826
**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#4307
## Test Environment
- [ ] ESP32
- [X] ESP32 IDF
- [ ] ESP8266
- [ ] RP2040
- [ ] BK72xx
- [ ] RTL87xx
## Example entry for `config.yaml`:
```yaml
# Example config.yaml
substitutions:
device_name: deepsleep-test
friendly_name: Deep Sleep
project_version: "1.0"
device_description: "Test deep sleep pulse counter component"
# Define the GPIO pins
pulse_pin: GPIO13
esphome:
name: '${device_name}'
comment: '${device_description}'
project:
name: "brisk.deepsleep-test"
version: "${project_version}"
esp32:
board: esp32dev
framework:
type: esp-idf
version: recommended
# WiFi credentials #
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_key
fast_connect: true
# Enable logging
logger:
level: VERBOSE
deep_sleep:
run_duration: 25s
sleep_duration: 20s
sensor:
- platform: pulse_counter_ulp
name: ${friendly_name} - Number of Pulses
pin:
number: ${pulse_pin}
inverted: true
mode:
pullup: true
input: true
update_interval: 5s
```
## Checklist:
- [x] The code change is tested and works locally.
- [x] Tests have been added to verify that the new code works (under `tests/` folder).
If user exposed functionality or configuration variables are added/changed:
- [x] Documentation added/updated in [esphome-docs](https://github.com/esphome/esphome-docs).
1 Like
moustic999
(Moustic999)
January 12, 2026, 12:57pm
4
I’m already using it for my esp32-S3, but I have plenty of esp32-c3 which doesn’t have ulp. I’m looking a solution to use them. I can do it in platformio directly, and was trying to figure out if there is the possibility to achieve this also with esphome.
Karosm
(Karosm)
January 12, 2026, 2:35pm
5
Aren’t global variables saved in RTC before going to sleep by default??