Timed Deep Sleep for ESP's

esphome:
friendly_name: H&T Badezimmer
name: “esp32c3-badezimmer”
platformio_options:
board_build.f_cpu: 80000000L

esp32:
board: seeed_xiao_esp32c3
framework:
type: arduino

ota:
password: “X”

wifi:
ssid: “Y”
password: “Z”
fast_connect: on
output_power: “20.5”
power_save_mode: none
manual_ip:
static_ip: 192.168.0.236
gateway: 192.168.0.254
subnet: 255.255.255.0
dns1: 8.8.8.8
dns2: 8.8.4.4
reboot_timeout: 5min

logger:
level: debug

mqtt:
broker: 192.168.0.2
port: 1883
username: A
password: B
birth_message:
will_message:
discovery_unique_id_generator: mac
discovery_object_id_generator: device_name
on_message:
- topic: esp32c3-badezimmer/sleep
then:
- output.turn_on: SHT30
- lambda: |-
id(sleeper).set_sleep_duration(std::stoul(x));
- deep_sleep.enter: sleeper

deep_sleep:
id: sleeper

i2c:
sda: 6
scl: 7
scan: false

sensor:

  • platform: sht3xd
    temperature:
    name: “Temperatur”
    humidity:
    name: “Feuchte”
    address: 0x44
    update_interval: 60s

output:

  • platform: gpio
    pin: GPIO3
    id: SHT30
    inverted: true

alias: H&T Badezimmer
description: “”
trigger:

  • platform: mqtt
    topic: esp32c3-badezimmer/sensor/temperatur/state
    condition:
    action:
  • if:
    • condition: or
      conditions:
      • condition: state
        entity_id: input_boolean.fruhbetrieb
        state: “on”
      • condition: state
        entity_id: input_boolean.tagesbetrieb
        state: “on”
      • condition: state
        entity_id: input_boolean.spatbetrieb
        state: “on”
        then:
    • service: input_number.set_value
      data_template:
      entity_id: input_number.h_t_badezimmer_sleep
      value: >-
      {{[[states(‘input_number.h_t_badezimmer_sleep’)|int + 60000 -
      ‘{:.3f}’.format(now().timestamp() -
      states[‘input_number.h_t_badezimmer_sleep’].last_updated.timestamp())|float
      * 1000, 30000]|max, 90000]|min}}
      else:
    • service: input_number.set_value
      data_template:
      entity_id: input_number.h_t_badezimmer_sleep
      value: >-
      {{[[states(‘input_number.h_t_badezimmer_sleep’)|int + 300000 -
      ‘{:.3f}’.format(now().timestamp() -
      states[‘input_number.h_t_badezimmer_sleep’].last_updated.timestamp())|float
      * 1000, 270000]|max, 330000]|min}}
  • service: mqtt.publish
    data:
    topic: esp32c3-badezimmer/sleep
    qos: “1”
    payload_template: “{{states(‘input_number.h_t_badezimmer_sleep’)}}”
    mode: parallel

Just for fun…

(P.s. a pitty that Deep Sleep for RP2040 is not implemented in ESPHome yet :wink:

https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

Ups. Ok. Let’s try

esphome:
  friendly_name: H&T Badezimmer
  name: "esp32c3-badezimmer"
  platformio_options:
    board_build.f_cpu: 80000000L

esp32:
  board: seeed_xiao_esp32c3
  framework:
    type: arduino

ota:
  password: "X"

wifi:
  ssid: "Y"
  password: "Z"
  fast_connect: on
  output_power: "20.5"
  power_save_mode: none
  manual_ip:
    static_ip: 192.168.0.236
    gateway: 192.168.0.254
    subnet: 255.255.255.0
    dns1: 8.8.8.8
    dns2: 8.8.4.4
  reboot_timeout: 5min

logger:
  level: debug

mqtt:
  broker: 192.168.0.2
  port: 1883
  username: A
  password: B
  birth_message:
  will_message:
  discovery_unique_id_generator: mac
  discovery_object_id_generator: device_name
  on_message:
    - topic: esp32c3-badezimmer/sleep
      then:
        - output.turn_on: SHT30
        - lambda: |-  
            id(sleeper).set_sleep_duration(std::stoul(x));
        - deep_sleep.enter: sleeper

deep_sleep:
  id: sleeper
 
i2c:
  sda: 6
  scl: 7
  scan: false

sensor:
  - platform: sht3xd
    temperature:
      name: "Temperatur"
    humidity:
      name: "Feuchte"
    address: 0x44
    update_interval: 60s

output:
  - platform: gpio
    pin: GPIO3
    id: SHT30
    inverted: true
alias: H&T Badezimmer
description: ""
trigger:
  - platform: mqtt
    topic: esp32c3-badezimmer/sensor/temperatur/state
condition: []
action:
  - if:
      - condition: or
        conditions:
          - condition: state
            entity_id: input_boolean.fruhbetrieb
            state: "on"
          - condition: state
            entity_id: input_boolean.tagesbetrieb
            state: "on"
          - condition: state
            entity_id: input_boolean.spatbetrieb
            state: "on"
    then:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.h_t_badezimmer_sleep
          value: >-
            {{[[states('input_number.h_t_badezimmer_sleep')|int + 60000 -
            '{:.3f}'.format(now().timestamp() -
            states['input_number.h_t_badezimmer_sleep'].last_updated.timestamp())|float
            * 1000, 30000]|max, 90000]|min}}
    else:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.h_t_badezimmer_sleep
          value: >-
            {{[[states('input_number.h_t_badezimmer_sleep')|int + 300000 -
            '{:.3f}'.format(now().timestamp() -
            states['input_number.h_t_badezimmer_sleep'].last_updated.timestamp())|float
            * 1000, 270000]|max, 330000]|min}}
  - service: mqtt.publish
    data:
      topic: esp32c3-badezimmer/sleep
      qos: "1"
      payload_template: "{{states('input_number.h_t_badezimmer_sleep')}}"
mode: parallel

Just for fun…

(P.s. a pitty that Deep Sleep for RP2040 is not implemented in ESPHome yet :wink:

Gives a good hint how close the sensors are to a perfect 1 minute, or 5, for data.