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