forked from jumakki/esphome-ensto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesphome-ensto.yaml
811 lines (775 loc) · 34.7 KB
/
esphome-ensto.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
esphome:
name: esphome-ensto
platform: ESP32
#board: esp32dev
board: esp32doit-devkit-v1
# Enable logging. Note that too much logging will cause connections to break.
logger:
logs:
esp32_ble_tracker: INFO # DEBUG to show discovered devices
sensor: INFO # DEBUG to show sensor value updates to HA
climate: INFO # DEBUG to show climate status updates to HA
template.sensor: NONE # CRITICAL to list sensors after WiFi connect
template.binary_sensor: NONE # CRITICAL to list sensors after WiFi connect
esp32_ble_client: INFO # WARN to not show service UUIDs
esp32_ble_client.service: NONE # INFO to list ensto characteristics
ble_client.automation: INFO # DEBUG to show connections
thermostat.climate: NONE # CRITICAL to show default values
ble_sensor: WARN # INFO to show connected characteristics
homeassistant.sensor: DEBUG # INFO to not show updates from external temperature sensor
ensto: INFO # DEBUG to show extra logging from this YAML
ensto.adv: INFO # DEBUG to show extra logging from this YAML about advertisements
ensto.receive: INFO # DEBUG to show extra logging from this YAML about received data from characteristics
ota:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
#captive_portal:
# Add webserver on port 80
#web_server:
# port: 80
esp32_ble_tracker:
id: ble_tracker
scan_parameters:
# Ensto sends one advertisement package every 200ms. With 320ms interval and 60ms window
# we will have a change to receive advertisement about once per second. That should be enough.
# With defaults (320ms & 30ms) it would be once every 3 seconds.
# update: 90ms works much-much better with some cheap esp32 dev boards.
interval: 320ms
window: 90ms
active: false
on_ble_advertise:
- mac_address: !secret ensto1_mac
then:
- lambda: |-
ESP_LOGI("ensto.adv", "New BLE device");
ESP_LOGD("ensto.adv", " address: %s", x.address_str().c_str());
ESP_LOGI("ensto.adv", " Advertised manufacturer data:");
auto man_datas = x.get_manufacturer_datas();
for (auto man_data : man_datas) {
ESP_LOGD("ensto.adv", " - %s (length %i)", man_data.uuid.to_string().c_str(), man_data.data.size());
if (man_data.uuid.get_uuid().uuid.uuid16 != 0x2806) {
continue;
}
auto data = man_data.data;
ESP_LOGI("ensto.adv", " - %s", &data[0]);
if (data.size() < 3) {
continue;
}
std::string data_str = reinterpret_cast<const char *>(data.data() + 2);
auto semi_pos = data_str.find_first_of(';');
if (semi_pos == std::string::npos || semi_pos > data_str.length() - 1) {
continue;
}
id(ensto1_is_pairing) = data_str[semi_pos + 1] == '1';
ESP_LOGI("ensto.adv", " - is pairing = %s", id(ensto1_is_pairing) ? "true" : "false");
}
globals:
- id: ensto1_factory_reset_id
type: uint32_t
restore_value: yes
initial_value: '0'
- id: ensto1_is_pairing
type: bool
restore_value: no
# internal state machine states:
# 0 = disconnected, don't try to read any characteristics
# 1 = read factory reset ID while device is in pairing mode
# 2 = write factory reset ID back to device. Write just received ID back during pairing or old one when not pairing.
# 3 = connected, free to read characteristics
# 4 = all characteristics have been read and sensor values are valid
- id: ensto1_state
type: uint32_t
initial_value: '0'
# interval count to filter what characteristics to read on each interval
- id: ensto1_interval_count
type: uint32_t
initial_value: '0'
- id: ensto1_previous_target_temperature
type: float
initial_value: "0.0"
- id: ensto1_characterics_written_not_read
type: uint32_t
initial_value: "0"
ble_client:
- mac_address: !secret ensto1_mac
id: ensto1
on_connect:
then:
- lambda: |-
esphome::ble_client::BLEClient* client = id(ensto1);
esp_ble_set_encryption(client->get_remote_bda(), ESP_BLE_SEC_ENCRYPT_MITM);
if (id(ensto1_is_pairing)) {
ESP_LOGI("ensto", "Connected to Ensto1 (pairing) -> read factory reset ID");
id(ensto1_state) = 1; // read new factory reset ID
return;
}
uint32_t key = id(ensto1_factory_reset_id);
if (key == 0) {
ESP_LOGE("ensto", "Factory reset ID is not set. Must be paired first.");
return;
}
ESP_LOGI("ensto", "Connected to Ensto1 -> write factory reset ID");
id(ensto1_state) = 2; // write old factory reset id
on_disconnect:
then:
- lambda: |-
ESP_LOGI("ensto", "Disconnected from Ensto1");
id(ensto1_connection_state).publish_state(false);
id(ensto1_state) = 0; // disconnected
binary_sensor:
- platform: template
id: ensto1_connection_state
name: "Ensto1 Connection status"
- platform: template
id: ensto1_relay_state
name: "Ensto1 Relay state"
- platform: template
id: ensto1_boost_status
name: "Ensto1 Boost status"
# - platform: template
# id: ensto1_adaptive_temperature_control_status
# name: "Ensto1 Adaptive temperature control status"
sensor:
# Real-time information (temperatures, is heating relay active)
- platform: ble_client
type: characteristic
ble_client_id: ensto1
id: ensto1_real_time_indication_characteristic
internal: true
service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
characteristic_uuid: 66AD3E6B-3135-4ADA-BB2B-8B22916B21D4
notify: false
update_interval: never
lambda: |-
ESP_LOGD("ensto.receive", "Real-time values received with %d bytes", x.size());
if (x.size() < 20) return NAN;
auto target_temp = (x[1] + 256 * x[2]) / 10.0f;
auto room_temp = (int16_t)((x[4] + 256 * x[5])) / 10.0f;
auto floor_temp = (int16_t)((x[6] + 256 * x[7])) / 10.0f;
if (target_temp == 0 && room_temp == 0 && floor_temp == 0) {
// security key was not written correctly -> don't update
ESP_LOGW("ensto.receive", "Real-time values are zero");
return NAN;
}
ESP_LOGI("ensto.receive", "Room: %.1f°C, Floor: %.1f°C, Target: %.1f°C", room_temp, floor_temp, target_temp);
static bool is_first = true;
// statics to update values to home-assistant only when they change
static auto last_target_temp = target_temp;
if (is_first || last_target_temp != target_temp) {
last_target_temp = target_temp;
id(ensto1_target_temperature).publish_state(target_temp);
}
static auto last_room_temp = room_temp;
if (is_first || last_room_temp != room_temp) {
last_room_temp = room_temp;
id(ensto1_room_temperature).publish_state(room_temp);
}
static auto last_floor_temp = floor_temp;
if (is_first || last_floor_temp != floor_temp) {
last_floor_temp = floor_temp;
id(ensto1_floor_temperature).publish_state(floor_temp);
}
auto relay_state = x[8] != 0;
static auto last_relay_state = relay_state;
if (is_first || last_relay_state != relay_state) {
last_relay_state = relay_state;
id(ensto1_relay_state).publish_state(relay_state);
}
/*
uint8_t heating_mode = x[14];
static uint8_t last_heating_mode = heating_mode;
if (is_first || last_heating_mode != heating_mode) {
last_heating_mode = heating_mode;
id(ensto1_heating_mode).publish_state(heating_mode);
}
*/
is_first = false;
return NAN;
- platform: template
name: "Ensto1 Target temperature"
id: ensto1_target_temperature
unit_of_measurement: "°C"
accuracy_decimals: 1
state_class: measurement
device_class: temperature
- platform: template
name: "Ensto1 Room temperature"
id: ensto1_room_temperature
unit_of_measurement: "°C"
accuracy_decimals: 1
state_class: measurement
device_class: temperature
- platform: template
name: "Ensto1 Floor temperature"
id: ensto1_floor_temperature
unit_of_measurement: "°C"
accuracy_decimals: 1
state_class: measurement
device_class: temperature
# - platform: template
# name: "Ensto1 Heating mode"
# id: ensto1_heating_mode
# unit_of_measurement: "mode"
# # Adaptive temperature control status
# - platform: ble_client
# type: characteristic
# ble_client_id: ensto1
# id: ensto1_adaptive_temp_control_characteristic
# internal: true
# service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
# characteristic_uuid: c2dc85e9-47bf-4968-9562-d2e1980ed4e4
# notify: false
# update_interval: never
# lambda: |-
# if (x.size() < 1) return NAN;
# static bool is_first = true;
# bool is_adaptive = x[0];
# ESP_LOGI("ensto.receive", "Adaptive temp control mode: %d", x[0]);
# static bool last_is_adaptive = is_adaptive;
# if (is_first || last_is_adaptive != is_adaptive) {
# is_first = false;
# last_is_adaptive = is_adaptive;
# id(ensto1_adaptive_temperature_control_status).publish_state(is_adaptive);
# }
# return NAN;
# Temperature calibration
- platform: ble_client
type: characteristic
ble_client_id: ensto1
id: ensto1_temp_calibration_characteristic
internal: true
service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
characteristic_uuid: 1eca4351-b264-4db6-9c59-af4341d6ce69
notify: false
update_interval: never
lambda: |-
ESP_LOGD("ensto.receive", "Temperature calibration characteristic received with %d bytes", x.size());
if (x.size() < 2) return NAN;
auto temp_cal = (int16_t)(256 * x[1] + x[0]);
ESP_LOGI("ensto.receive", "Temperature calibration value: %.1f°C", temp_cal / 10.0f);
static auto last_temp_cal = temp_cal;
static bool is_first = true;
if (is_first || last_temp_cal != temp_cal) {
last_temp_cal = temp_cal;
id(ensto1_temp_calibration).publish_state(temp_cal / 10.0f);
}
is_first = false;
return NAN;
- platform: template
name: "Ensto1 Temperature calibration value"
id: ensto1_temp_calibration
unit_of_measurement: "°C"
accuracy_decimals: 1
state_class: measurement
device_class: temperature
# Temperature boost
- platform: ble_client
type: characteristic
ble_client_id: ensto1
id: ensto1_boost_characteristic
internal: true
service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
characteristic_uuid: ca3c0685-b708-4cd4-a049-5badd10469e7
notify: false
update_interval: never
lambda: |-
ESP_LOGD("ensto.receive", "Boost characteristic received with %d bytes", x.size());
if (x.size() < 8) return NAN;
auto is_boost_on = x[0] != 0;
static bool is_first = true;
static auto last_is_boost_on = is_boost_on;
if (is_first || last_is_boost_on != is_boost_on) {
last_is_boost_on = is_boost_on;
id(ensto1_boost_status).publish_state(is_boost_on);
}
auto boost_offset = (int16_t)(256 * x[2] + x[1]);
static auto last_boost_offset = boost_offset;
if (is_first || last_boost_offset != boost_offset) {
last_boost_offset = boost_offset;
id(ensto1_temp_boost_offset).publish_state(boost_offset / 100.0f);
}
auto boost_minutes_left = (uint16_t)(256 * x[7] + x[6]);
ESP_LOGI("ensto.receive", "Boost: %s, offset: %.2f°C, minutes left: %d", (is_boost_on ? "ON" : "OFF"), boost_offset/100.0f, boost_minutes_left);
static auto last_boost_minutes_left = boost_minutes_left;
if (is_first || last_boost_minutes_left != boost_minutes_left) {
last_boost_minutes_left = boost_minutes_left;
id(ensto1_temp_boost_minutes_left).publish_state(boost_minutes_left);
}
is_first = false;
return NAN;
- platform: template
name: "Ensto1 Temperature boost offset"
id: ensto1_temp_boost_offset
unit_of_measurement: "°C"
accuracy_decimals: 2
state_class: measurement
device_class: temperature
- platform: template
name: "Ensto1 Temperature boost minutes left"
id: ensto1_temp_boost_minutes_left
unit_of_measurement: "minutes"
# Power consumption
- platform: ble_client
type: characteristic
ble_client_id: ensto1
id: ensto1_power_consumption_characteristic
internal: true
service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
characteristic_uuid: c1686f28-fa1b-4791-9eca-35523fb3597e
notify: false
update_interval: never
lambda: |-
// format (uint8_t): <header><current hour><current date><current month><current year> following 25x(<hour delta from current><consumption ratio>)
// ESP_LOGD("ensto.receive", "Power consumption characteristic received with %d bytes: %s", x.size(), format_hex(x.data(), x.size()).c_str());
if (x.size() < 9) return NAN;
if (x[5] != 0) return NAN; // current hour offset should be 0
if (x[7] != 1) return NAN; // previous hour offset should be 1
ESP_LOGI("ensto.receive", "Power consumption ratio: %d%%, last hour: %d%%", x[6], x[8]);
id(ensto1_power_consumption_ratio_current_hour).publish_state(x[6]);
id(ensto1_power_consumption_ratio_previous_hour).publish_state(x[8]);
if (!id(ensto1_max_heating_power).has_state()) return NAN; // not updated yet
uint32_t power = id(ensto1_max_heating_power).get_state();
id(ensto1_power_consumption_current_hour).publish_state(x[6] * power / 1000.0 / 100.0);
id(ensto1_power_consumption_previous_hour).publish_state(x[8] * power / 1000.0 / 100.0);
if (!id(ensto1_price_per_kwh).has_state()) return NAN; // not updated yet
auto price = id(ensto1_price_per_kwh).get_state();
id(ensto1_price_current_hour).publish_state(x[6] * power / 1000.0 / 100.0 * price);
id(ensto1_price_previous_hour).publish_state(x[8] * power / 1000.0 / 100.0 * price);
return NAN;
- platform: template
name: "Ensto1 Power consumption ratio current hour"
id: ensto1_power_consumption_ratio_current_hour
accuracy_decimals: 0
unit_of_measurement: "%"
device_class: power_factor
state_class: total_increasing
- platform: template
name: "Ensto1 Power consumption ratio previous hour"
id: ensto1_power_consumption_ratio_previous_hour
accuracy_decimals: 0
unit_of_measurement: "%"
device_class: power_factor
state_class: total
- platform: template
name: "Ensto1 Power consumption current hour (kWh)"
id: ensto1_power_consumption_current_hour
accuracy_decimals: 3
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
- platform: template
name: "Ensto1 Power consumption previous hour (kWh)"
id: ensto1_power_consumption_previous_hour
accuracy_decimals: 3
unit_of_measurement: "kWh"
device_class: energy
state_class: total
- platform: template
name: "Ensto1 Electricity price current hour"
id: ensto1_price_current_hour
accuracy_decimals: 5
unit_of_measurement: "€" # hard-coded unit
state_class: total_increasing
- platform: template
name: "Ensto1 Electricity price previous hour"
id: ensto1_price_previous_hour
accuracy_decimals: 5
unit_of_measurement: "€" # hard-coded unit
state_class: total
# Max heating power (set manually from App)
- platform: ble_client
type: characteristic
ble_client_id: ensto1
id: ensto1_heating_power_characteristic
internal: true
service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
characteristic_uuid: 53b7bf87-6cf0-4790-839a-e72d3afbec44
notify: false
update_interval: never
lambda: |-
// format (all single byte): <header><current hour><current date><current month><current year> following 25x(<hour delta from current><consumption ratio>)
ESP_LOGD("ensto.receive", "Heating Power characteristic received with %d bytes: %s", x.size(), format_hex(x.data(), x.size()).c_str());
if (x.size() < 2) return NAN;
ESP_LOGI("ensto.receive", "Heating Power: %dW", (int16_t)(256 * x[1] + x[0]));
id(ensto1_max_heating_power).publish_state((int16_t)(256 * x[1] + x[0]));
return NAN;
- platform: template
name: "Ensto1 Max Heating Power"
id: ensto1_max_heating_power
accuracy_decimals: 0
device_class: power
unit_of_measurement: "W"
# # LED brightness
# - platform: ble_client
# type: characteristic
# ble_client_id: ensto1
# id: ensto1_led_brightness_characteristic
# internal: true
# service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
# characteristic_uuid: 0bee30ff-ed95-4747-bf1b-01a60f5ff4fc
# notify: false
# update_interval: never
# lambda: |-
# if (x.size() < 3) return NAN;
# auto red = x[0];
# auto green = x[1];
# auto blue = x[2];
# ESP_LOGI("ensto.receive", "LED brightness: red=%d%%, green=%d%%, blue=%d%%", red, green, blue);
# id(ensto1_red_led_brightness).publish_state(red);
# id(ensto1_green_led_brightness).publish_state(green);
# id(ensto1_blue_led_brightness).publish_state(blue);
# return NAN;
# - platform: template
# name: "Ensto1 Red LED brightness"
# id: ensto1_red_led_brightness
# accuracy_decimals: 0
# unit_of_measurement: "%"
# - platform: template
# name: "Ensto1 Green LED brightness"
# id: ensto1_green_led_brightness
# accuracy_decimals: 0
# unit_of_measurement: "%"
# - platform: template
# name: "Ensto1 Blue LED brightness"
# id: ensto1_blue_led_brightness
# accuracy_decimals: 0
# unit_of_measurement: "%"
# Energy unit price (set manually from App)
- platform: ble_client
type: characteristic
ble_client_id: ensto1
id: ensto1_energy_unit_characteristic
internal: true
service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
characteristic_uuid: ccf1fe7b-d928-45b1-abba-7a915f2f0c64
notify: false
update_interval: never
lambda: |-
if (x.size() < 4) return NAN;
auto currency = x[0];
auto price = (int16_t)(256 * x[3] + x[2]) / 100.0f;
std::string currency_str;
if (currency == 1) currency_str = "€";
if (currency == 2) currency_str = "SEK";
if (currency == 3) currency_str = "NOK";
if (currency == 4) currency_str = "RUB";
if (currency == 5) currency_str = "$";
ESP_LOGI("ensto.receive", "Energy unit: currency=%d (%s), price=%.2f", currency, currency_str.c_str(), price);
id(ensto1_currency_id).publish_state(currency);
id(ensto1_price_per_kwh).publish_state(price);
return NAN;
- platform: template
name: "Ensto1 Currency ID"
id: ensto1_currency_id
accuracy_decimals: 0
- platform: template
name: "Ensto1 Price per kWh"
id: ensto1_price_per_kwh
accuracy_decimals: 2
# Factory reset ID
- platform: ble_client
type: characteristic
ble_client_id: ensto1
id: ensto1_security_key_characteristic
internal: true
service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
characteristic_uuid: f366dddb-ebe2-43ee-83c0-472ded74c8fa
notify: false
update_interval: never
lambda: |-
if (x.size() < 4) return NAN;
uint32_t key = x[0] | x[1] << 8 | x[2] << 16 | x[3] << 24;
if (key == 0) {
ESP_LOGW("ensto", "Reading Ensto1 factory reset ID failed");
return NAN; // couldn't get pairing key
}
ESP_LOGD("ensto", "key read = %02x%02x%02x%02x", x[0], x[1], x[2], x[3]);
id(ensto1_factory_reset_id) = key;
ESP_LOGI("ensto", "Factory reset ID read -> write factory reset ID");
id(ensto1_state) = 2; // write key back
return NAN;
# External temperature sensor from Home Assistant for Climate Control
- platform: homeassistant
id: external_temperature_sensor
entity_id: sensor.bedroom_temperature
internal: true
accuracy_decimals: 2
# Optional filter in case original sensor requires filtering. Note that median filter will make reacting to temperature changes slower.
# filters:
# - median:
# window_size: 7
# send_every: 1
# send_first_at: 1
# state machine loop. Do actions every 5 second
interval:
- interval: 5s
then:
- lambda: !lambda 'id(ensto1_interval_count) = id(ensto1_interval_count) + 1;'
# 1 = pairing, read factory reset ID
- if:
condition:
lambda: !lambda 'return id(ensto1_state) == 1;'
then:
- lambda: !lambda 'id(ensto1_security_key_characteristic).update();'
# 2 = write factory reset ID (if it is not zero)
- if:
condition:
lambda: |-
uint32_t key = id(ensto1_factory_reset_id);
return id(ensto1_state) == 2 && key != 0;
then:
- ble_client.ble_write:
id: ensto1
service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
characteristic_uuid: f366dddb-ebe2-43ee-83c0-472ded74c8fa
value: !lambda |-
uint32_t key = id(ensto1_factory_reset_id);
uint8_t newVal[4] = {
(uint8_t)(key & 0xff),
(uint8_t)((key >> 8) & 0xff),
(uint8_t)((key >> 16) & 0xff),
(uint8_t)((key >> 24) & 0xff),
};
ESP_LOGI("ensto", "Factory reset ID written -> Connected");
id(ensto1_connection_state).publish_state(true);
id(ensto1_state) = 3; // connected
return {newVal[0], newVal[1], newVal[2], newVal[3]};
# 3+ = connected, read characteristics once in a while
- if:
condition:
lambda: !lambda 'return id(ensto1_state) >= 3;'
then:
- lambda: |-
// Ensto disconnects if some characteristic is not read for 30 seconds
// every 10 seconds OR when needed
if (id(ensto1_interval_count) % 2 == 0 || id(ensto1_characterics_written_not_read) == 1) {
id(ensto1_boost_characteristic).update();
id(ensto1_real_time_indication_characteristic).update();
id(ensto1_characterics_written_not_read) = 0;
id(ensto1_state) = 4;
}
if (id(ensto1_interval_count) % 3 == 0) {
// update if sensor values are not valid after reconnect
/*
if (!id(ensto1_adaptive_temperature_control_status).has_state()) {
id(ensto1_adaptive_temp_control_characteristic).update();
}
*/
if (!id(ensto1_temp_calibration).has_state()) {
id(ensto1_temp_calibration_characteristic).update();
}
if (!id(ensto1_max_heating_power).has_state()) {
id(ensto1_heating_power_characteristic).update();
}
if (!id(ensto1_price_current_hour).has_state()) {
id(ensto1_power_consumption_characteristic).update();
}
if (!id(ensto1_price_per_kwh).has_state()) {
id(ensto1_energy_unit_characteristic).update();
}
/*
if (!id(ensto1_red_led_brightness).has_state()) {
id(ensto1_led_brightness_characteristic).update();
}
*/
}
if (id(ensto1_interval_count) % 12 == 0) { // every 60 seconds
id(ensto1_power_consumption_characteristic).update();
id(ensto1_energy_unit_characteristic).update();
}
if (id(ensto1_interval_count) % 13 == 0) { // every 65 seconds
// id(ensto1_adaptive_temp_control_characteristic).update();
id(ensto1_temp_calibration_characteristic).update();
// id(ensto1_led_brightness_characteristic).update();
}
if (id(ensto1_interval_count) % 61 == 0) { // every ~5 minutes
id(ensto1_heating_power_characteristic).update();
}
# 4 = Climate Controller boost
- if:
condition:
lambda: |-
return id(ensto1_state) >= 4 &&
(
// 2 minute minimum delay between updates, if the target temperature has not changed.
id(ensto1_thermostat).target_temperature != id(ensto1_previous_target_temperature) ||
id(ensto1_interval_count) % 24 == 0
)
&& (
id(ensto1_thermostat).mode != ClimateMode::CLIMATE_MODE_OFF &&
id(ensto1_temp_boost_minutes_left).has_state() &&
id(ensto1_temp_boost_offset).has_state() &&
id(external_temperature_sensor).has_state() &&
id(ensto1_target_temperature).has_state() &&
id(ensto1_temp_calibration).has_state()
);
then:
- ble_client.ble_write:
id: ensto1
service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
characteristic_uuid: ca3c0685-b708-4cd4-a049-5badd10469e7
value: !lambda |-
auto target = id(ensto1_thermostat).target_temperature;
auto thermos_diff = target - id(external_temperature_sensor).get_state();
auto boost = id(ensto1_temp_boost_offset).get_state();
auto device_target = id(ensto1_target_temperature).get_state();
auto device_base = device_target - boost;
// safe for next iteration
id(ensto1_previous_target_temperature) = target;
id(ensto1_characterics_written_not_read) = 1;
// first add boost to compensate difference between ensto's internal and our external temperature sensor
auto needed_boost = (target - device_base);
// then add difference of thermostat's set and current temperatures
needed_boost += thermos_diff;
// add also calibration value
needed_boost += id(ensto1_temp_calibration).get_state();
// finally overcompensate needed boost so that target temperature is actually reached
// These formulas could be improved
if (thermos_diff < -1 * id(ensto1_thermostat).heat_overrun()) {
// above and not close to target (e.g. >0.5 from target) -> big negative boost to turn heating completely off
needed_boost += -5;
} else if (thermos_diff > id(ensto1_thermostat).heat_deadband()) {
// below and not close to target (e.g. <0.5 from target) -> boost more than needed
needed_boost += 2;
} else {
// close to target -> try to keep it here
needed_boost += thermos_diff * 1.5;
}
int16_t value = round(needed_boost * 100);
value = std::max<int16_t>(-2000, std::min<int16_t>(value, 2000));
uint16_t minutes = 15; // TODO: hardcoded
// uint16_t minutes = std::max(0, std::min(length_minutes, 255));
ESP_LOGI("ensto", "New climate boost offset set to %.2f°C for %u minutes", value/100.0f, minutes);
return {
1, // enable
(uint8_t)(value & 0xff), (uint8_t)((value >> 8) & 0xff), // int16_t offset
0, // boost offset percentage (not used)
(uint8_t)(minutes & 0xff), (uint8_t)((minutes >> 8) & 0xff) // uint16_t boost time set point in minutes
};
# Enable Home Assistant API
api:
services:
# Temperature calibration value has range of -5.0..5.0C written as -50..50 to Ensto.
- service: set_ensto1_temperature_calibration
variables:
temperature_offset: float
then:
- if:
condition:
lambda: !lambda 'return id(ensto1_connection_state).has_state() && id(ensto1_connection_state).state;'
then:
- ble_client.ble_write:
id: ensto1
service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
characteristic_uuid: 1eca4351-b264-4db6-9c59-af4341d6ce69
value: !lambda |-
int16_t value = round(temperature_offset * 10);
// ensure it is within limits
value = std::max<int16_t>(-50, std::min<int16_t>(value, 50));
ESP_LOGI("ensto", "New temperature calibration value set to %d", value);
return {(uint8_t)(value & 0xff), (uint8_t)((value >> 8) & 0xff)};
# Temperature boost offset has valid range of -20.00..20.00C written as -2000..2000 to Ensto.
# Boost length has valid range of 0..300 minutes.
- service: set_ensto1_temperature_boost_offset
variables:
boost_offset: float
length_minutes: int
then:
- if:
condition:
lambda: !lambda 'return id(ensto1_connection_state).has_state() && id(ensto1_connection_state).state;'
then:
- ble_client.ble_write:
id: ensto1
service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
characteristic_uuid: ca3c0685-b708-4cd4-a049-5badd10469e7
value: !lambda |-
int16_t value = round(boost_offset * 100);
value = std::max<int16_t>(-2000, std::min<int16_t>(value, 2000));
uint16_t minutes = std::max<int>(0, std::min<int>(length_minutes, 300));
ESP_LOGI("ensto", "New temperature boost offset set to %d for %u minutes", value, minutes);
return {
1, // enable
(uint8_t)(value & 0xff), (uint8_t)((value >> 8) & 0xff), // int16_t offset
0, // boost offset percentage (not used)
(uint8_t)(minutes & 0xff), (uint8_t)((minutes >> 8) & 0xff) // uint16_t boost time set point in minutes
};
# Max heating power of the device (e.g. 1000W).
- service: set_ensto1_max_power
variables:
max_power: int
then:
- if:
condition:
lambda: !lambda 'return id(ensto1_connection_state).has_state() && id(ensto1_connection_state).state;'
then:
- ble_client.ble_write:
id: ensto1
service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
characteristic_uuid: 53b7bf87-6cf0-4790-839a-e72d3afbec44
value: !lambda |-
// ensure it is within limits
uint16_t value = std::max<int32_t>(0, std::min<int32_t>(max_power, std::numeric_limits<uint16_t>::max()));
ESP_LOGI("ensto", "New max heating power value set to %dW", value);
return {(uint8_t)(value & 0xff), (uint8_t)((value >> 8) & 0xff)};
# Price of electricity per kWh (e.g. 0.09€). Energy unit IDs: use_previous=0, EUR=1, SEK=2, NOK=3, RUB=4, USD=5
- service: set_ensto1_kwh_price
variables:
energy_unit_id: int
price_per_kwh: float
then:
- if:
condition:
lambda: !lambda 'return id(ensto1_connection_state).has_state() && id(ensto1_connection_state).state && (energy_unit_id >= 0 && energy_unit_id <= 5);'
then:
- ble_client.ble_write:
id: ensto1
service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
characteristic_uuid: ccf1fe7b-d928-45b1-abba-7a915f2f0c64
value: !lambda |-
auto unit_id = energy_unit_id;
if (unit_id == 0 && id(ensto1_currency_id).has_state()) unit_id = id(ensto1_currency_id).get_state();
uint16_t value = std::max<uint16_t>(0, std::min<uint16_t>(price_per_kwh * 100, std::numeric_limits<uint16_t>::max()));
ESP_LOGI("ensto", "New price of electricity per kWh set to %.2f (unit ID=%d)", price_per_kwh, unit_id);
return {(uint8_t)(unit_id & 0xff), 0, (uint8_t)(value & 0xff), (uint8_t)((value >> 8) & 0xff)};
# # Led brightness percentage (0-100)
# - service: set_ensto1_led_brightness_percentage
# variables:
# red: int
# green: int
# blue: int
# then:
# - if:
# condition:
# lambda: !lambda 'return id(ensto1_connection_state).has_state() && id(ensto1_connection_state).state;'
# then:
# - ble_client.ble_write:
# id: ensto1
# service_uuid: F49CEFD5-209B-4531-99BD-89FE2909931A
# characteristic_uuid: 0bee30ff-ed95-4747-bf1b-01a60f5ff4fc
# value: !lambda |-
# ESP_LOGI("ensto", "New LED brightness values set to: red=%d, green=%d, blue=%d", red, green, blue);
# return {(uint8_t)(std::max(0, std::min(red, 100)) & 0xff),
# (uint8_t)(std::max(0, std::min(green, 100)) & 0xff),
# (uint8_t)(std::max(0, std::min(blue, 100)) & 0xff)};
climate:
- platform: thermostat
name: "Ensto1 Thermostat"
id: ensto1_thermostat
sensor: external_temperature_sensor
preset:
- name: home # default
default_target_temperature_low: 20.0°C
mode: heat
# default_target_temperature_low: 20°C # deprecated
# these 2 are used in state machine above
heat_deadband: 0.5°C # The minimum temperature differential (temperature below the set point) before engaging extra heating
heat_overrun: 0.5°C # The minimum temperature differential (temperature above the set point) before disengaging heat
min_idle_time: 5s # not really used, required field
min_heating_off_time: 5s # not really used, required field
min_heating_run_time: 5s # not really used, required field
heat_action:
- lambda: !lambda ';' # heating control is handled in state machine
idle_action:
- lambda: !lambda ';' # idle control is handled in state machine