Level II - Timed Deep Sleep for ESP's

Now to the Milliseconds to come close to the perfect Sensor Deep Sleep.

We need a C++ inclusion in the esphome folder

I named it

int64_t include_now() {
        struct timeval tv;
        gettimeofday(&tv, NULL);
        return (tv.tv_sec * 1000LL + (tv.tv_usec / 1000LL));
}`

as /homeassistant/esphome/_include_timestamp.h in the esphome folder.

  includes:
    - _include_timestamp.h

Reason for:

sensor:
  - platform: sht3xd
    temperature:
      name: "Temperatur"
      id: sht3xd_temperature
      filters:
        - round: 1
        - offset: -0.3
    humidity:
      name: "Feuchte"
      id: sht3xd_humidity
      filters:
        - round: 0
    address: 0x44
  - platform: adc
    pin: VCC
    name: "Spannung"

deep_sleep:
  id: sleeper
 
time:
  - platform: sntp
    id: sntp_time
    timezone: Europe/Berlin
    servers:
     - 0.de.pool.ntp.org
     - 1.de.pool.ntp.org
     - 2.de.pool.ntp.org      
    on_time_sync:
      then:
        - lambda: id(sntp_synced) = true;

logger:
  level: debug

globals:
  - id: store_temperature
    type: float
    restore_value: yes
    initial_value: '0.0'
  - id: store_humidity
    type: float
    restore_value: yes
    initial_value: '0.0'
  - id: store_sleep
    type: int
    restore_value: yes
    initial_value: '60000'
  - id: store_timestamp
    type: time_t
    restore_value: yes
    initial_value: '0'
  - id: sntp_synced
    type: boolean
    restore_value: no
    initial_value: 'false'

mqtt:
  broker: 192.168.0.2
  port: 1883
  username: BillyBoy
  password: 4711
  birth_message:
  will_message:
  discovery_unique_id_generator: mac
  discovery_object_id_generator: device_name
  on_message:
    - topic: h-t-debug/sleep
      then:
        - wait_until:
          - lambda: return id(sntp_synced);
        - lambda:  |-
            id(store_sleep) = min(max(id(store_sleep) + (std::stoul(x) - (include_now() - id(store_timestamp))), std::time_t((std::stoul(x) - 30000))), std::time_t((std::stoul(x) + 30000)));
            id(store_timestamp) = include_now();
            id(sleeper).set_sleep_duration(id(store_sleep));
        - deep_sleep.enter: sleeper

script:
  - id: consider_wifi
    then:
      - if:
          condition:
            or:
              - lambda: return id(sht3xd_temperature).state != id(store_temperature);
              - lambda: return id(sht3xd_humidity).state != id(store_humidity);
          then:
            - lambda: |-
                id(store_temperature) = id(sht3xd_temperature).state;
                id(store_humidity) = id(sht3xd_humidity).state;
            - wifi.enable:
          else:
            - lambda: |-
                id(store_temperature) = id(sht3xd_temperature).state;
                id(store_humidity) = id(sht3xd_humidity).state;
                id(store_timestamp) = id(store_timestamp) + id(store_sleep);
                id(sleeper).set_sleep_duration(id(store_sleep));
            - deep_sleep.enter: sleeper

alias: H&T Debug
description: ""
trigger:
  - platform: mqtt
    topic: h-t-debug/sensor/temperatur/state
condition: []
action:
  - if:
      - condition: or
        conditions:
          - condition: template
            value_template: "{{state_attr('climate.trv_arbeitsecke', 'temperature') != 4}}"
    then:
      - service: mqtt.publish
        data:
          topic: h-t-debug/sleep
          qos: "1"
          payload: "60000"
    else:
      - service: mqtt.publish
        data:
          topic: h-t-debug/sleep
          qos: "1"
          payload: "300000"
mode: single
max_exceeded: silent

When the ESP-M2 with sht30’s or sht40’s as sensors do NOT activate WiFi when they know there’s no new value they last on my 3d printed housings with just simply attched 21700 accus for like 867 Days.

Theoretically.

Lol.

ESP-12S are 2. place i measured.

Pretty sure this awesome, at least 3-4 tricks in it, if u can read it, welcome to the club.

esphome:
  friendly_name: H&T Debug
  name: "h-t-debug"
  platformio_options:
    board_build.f_cpu: 80000000L
  on_boot:
    - wait_until:
        sensor.in_range:
          id: sht3xd_temperature
          above: 0.0
          below: 100.0
    - wait_until:
        sensor.in_range:
          id: sht3xd_humidity
          above: 0.0
          below: 100.0
    - script.execute: consider_wifi
  includes:
    - _include_timestamp.h

Sorry, forgot that