-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnibe.py
646 lines (607 loc) · 24.5 KB
/
nibe.py
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
import logging
import serial
import time
import paho.mqtt.client as mqtt
from struct import unpack, pack
# Setup logger
#logging.basicConfig(level=logging.WARNING)
logging.basicConfig(level=logging.WARNING, filename='nibe_debug.log', filemode='w', format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger('NIBE')
# MQTT setup
mqtt_client = mqtt.Client()
mqtt_client.reconnect_delay_set(min_delay=1, max_delay=60)
mqtt_client.username_pw_set(username="xxx", password="xxx")
mqtt_client.will_set("nibe/status", "offline", retain=True)
mqtt_client.connect("0.0.0.0", 1883, 60)
def publish_mqtt(topic, message):
mqtt_client.publish(topic, message)
logger.info(f"Published {message} to {topic}")
# Publish "online" status to availability topic
def publish_availability(status):
mqtt_client.publish("nibe/status", status, retain=True)
logger.info(f"Published availability status: {status}")
# Serial connection to Nibe heat pump (adjust COM port for Windows)
serial_port = "/dev/ttyUSB0" # Adjust to your correct COM port
ser = serial.Serial(serial_port, 19200, bytesize=serial.EIGHTBITS, stopbits=serial.STOPBITS_ONE, parity=serial.PARITY_NONE, timeout=3)
logger.debug("Serial port opened successfully")
# Register mapping, expanded to match original script
nibe_registers = {
0: "nibe/cpu_id",
1: "nibe/outdoor_temp_c",
4: "nibe/heating_curve",
5: "nibe/flow_setpoint_c",
6: "nibe/flow_actual_c",
7: "nibe/return_temp_c",
8: "nibe/degree_minutes",
12: "nibe/domestic_hot_water_top_temp",
25: "nibe/compressor_starts",
31: "nibe/heating_status",
32: "nibe/additional_heating_allowed",
33: "nibe/max_df_compressor",
34: "nibe/verd_freq_reg_p",
35: "nibe/min_start_time_freq_min",
36: "nibe/min_time_const_freq_min",
38: "nibe/comp_freq_grad_min",
44: "nibe/pump_speed_percent",
45: "nibe/bw_reg_p",
46: "nibe/bw_reg_q",
48: "nibe/bw_reg_value_xp_percent",
100: "nibe/date_year",
101: "nibe/date_month",
102: "nibe/date_day",
103: "nibe/time_hour",
104: "nibe/time_minute",
105: "nibe/time_second",
24: "nibe/run_time_compressor_h",
9: "nibe/comp_freq_desired_hz",
10: "nibe/comp_freq_actual_hz",
19: "nibe/high_pressure_bar",
20: "nibe/low_pressure_bar",
22: "nibe/ams_phase_is_a",
40: "nibe/hysteresis",
47: "nibe/bw_reg_xp",
43: "nibe/stop_temp_heating_c",
49: "nibe/domestic_hot_water_start_temp",
50: "nibe/domestic_hot_water_stop_temp",
28: "nibe/operation_mode_reg_28",
29: "nibe/operation_mode_reg_29",
30: "nibe/operation_mode_reg_30",
23: "nibe/inverter_temp_tho_ip",
11: "nibe/condenser_off_max",
13: "nibe/domestic_hot_water_bottom",
14: "nibe/tho_r1_evap_temp",
15: "nibe/tho_r2_evap_temp",
16: "nibe/suction_gas_temp_tho_s",
17: "nibe/hot_gas_temp_tho_d",
18: "nibe/liquid_temp_ams",
21: "nibe/resp_at_ams_tho_a",
}
# Define unique IDs and MQTT discovery configurations for each sensor
mqtt_discovery_sensors = {
"nibe/pump_speed_percent": {
"name": "Pump Speed Percent",
"unit_of_measurement": "%",
"state_topic": "nibe/pump_speed_percent",
"unique_id": "nibe_pump_speed_percent"
},
# "nibe/cpu_id": {
# "name": "CPU ID",
# "state_topic": "nibe/cpu_id",
# "unique_id": "nibe_cpu_id"
# },
"nibe/inverter_temp_tho_ip": {
"name": "Inverter temp Tho-IP",
"state_class": "measurement",
"unit_of_measurement": "°C",
"device_class": "temperature",
"state_topic": "nibe/inverter_temp_tho_ip",
"unique_id": "nibe_inverter_temp_tho_ip"
},
"nibe/operation_mode": {
"name": "Nibe Operation Mode",
"state_topic": "nibe/operation_mode",
"unique_id": "nibe_operation_mode"
},
"nibe/condenser_off_max": {
"name": "Condenser Off (MAX)",
"state_topic": "nibe/condenser_off_max",
"unit_of_measurement": "°C",
"device_class": "temperature",
"unique_id": "nibe_condenser_off_max"
},
"nibe/domestic_hot_water_bottom": {
"name": "Domestic Hot Water Bottom",
"state_topic": "nibe/domestic_hot_water_bottom",
"unit_of_measurement": "°C",
"device_class": "temperature",
"unique_id": "nibe_domestic_hot_water_bottom"
},
"nibe/tho_r1_evap_temp": {
"name": "Tho-R1 Evaporator Temp",
"state_topic": "nibe/tho_r1_evap_temp",
"unit_of_measurement": "°C",
"device_class": "temperature",
"unique_id": "nibe_tho_r1_evap_temp"
},
"nibe/tho_r2_evap_temp": {
"name": "Tho-R2 Evaporator Temp",
"state_topic": "nibe/tho_r2_evap_temp",
"unit_of_measurement": "°C",
"device_class": "temperature",
"unique_id": "nibe_tho_r2_evap_temp"
},
"nibe/suction_gas_temp_tho_s": {
"name": "Suction Gas Temp Tho-S",
"state_topic": "nibe/suction_gas_temp_tho_s",
"unit_of_measurement": "°C",
"device_class": "temperature",
"unique_id": "nibe_suction_gas_temp_tho_s"
},
"nibe/hot_gas_temp_tho_d": {
"name": "Hot Gas Temp Tho-D",
"state_topic": "nibe/hot_gas_temp_tho_d",
"unit_of_measurement": "°C",
"device_class": "temperature",
"unique_id": "nibe_hot_gas_temp_tho_d"
},
"nibe/liquid_temp_ams": {
"name": "Liquid Temp AMS",
"state_topic": "nibe/liquid_temp_ams",
"unit_of_measurement": "°C",
"device_class": "temperature",
"unique_id": "nibe_liquid_temp_ams"
},
"nibe/resp_at_ams_tho_a": {
"name": "Response at AMS Tho-A",
"state_topic": "nibe/resp_at_ams_tho_a",
"unit_of_measurement": "°C",
"device_class": "temperature",
"unique_id": "nibe_resp_at_ams_tho_a"
},
"nibe/heating_curve": {
"name": "Heating Curve",
"state_topic": "nibe/heating_curve",
"state_class": "measurement",
"unit_of_measurement": "°C",
"device_class": "temperature",
"value_template": "{{ value | float }}",
"unique_id": "nibe_heating_curve"
},
"nibe/return_temp_c": {
"name": "Return Temperature",
"unit_of_measurement": "°C",
"device_class": "temperature",
"state_topic": "nibe/return_temp_c",
"unique_id": "nibe_return_temp_c"
},
"nibe/degree_minutes": {
"name": "Degree Minutes",
"state_topic": "nibe/degree_minutes",
"state_class": "measurement",
"unit_of_measurement": "°C",
"device_class": "temperature",
"value_template": "{{ value | float }}",
"unique_id": "nibe_degree_minutes"
},
"nibe/domestic_hot_water_top_temp": {
"name": "Domestic Hot Water Top Temperature",
"unit_of_measurement": "°C",
"device_class": "temperature",
"state_topic": "nibe/domestic_hot_water_top_temp",
"unique_id": "nibe_domestic_hot_water_top_temp"
},
"nibe/max_df_compressor": {
"name": "Max DF Compressor",
"state_topic": "nibe/max_df_compressor",
"unit_of_measurement": "Hz",
"value_template": "{{ value | float }}",
"unique_id": "nibe_max_df_compressor"
},
"nibe/verd_freq_reg_p": {
"name": "Verd Freq Reg P",
"state_topic": "nibe/verd_freq_reg_p",
"unit_of_measurement": "Hz",
"value_template": "{{ value | float }}",
"unique_id": "nibe_verd_freq_reg_p"
},
"nibe/min_start_time_freq_min": {
"name": "Min Start Time Freq Min",
"unit_of_measurement": "Hz",
"state_topic": "nibe/min_start_time_freq_min",
"unique_id": "nibe_min_start_time_freq_min"
},
"nibe/min_time_const_freq_min": {
"name": "Min Time Const Freq Min",
"state_topic": "nibe/min_time_const_freq_min",
"unit_of_measurement": "Hz",
"unique_id": "nibe_min_time_const_freq_min"
},
"nibe/comp_freq_grad_min": {
"name": "Comp Freq Grad Min",
"state_topic": "nibe/comp_freq_grad_min",
"unit_of_measurement": "Hz",
"value_template": "{{ value | float }}",
"unique_id": "nibe_comp_freq_grad_min"
},
"nibe/bw_reg_p": {
"name": "BW Reg P",
"state_topic": "nibe/bw_reg_p",
"unit_of_measurement": "°C",
"device_class": "temperature",
"value_template": "{{ value | float }}",
"unique_id": "nibe_bw_reg_p"
},
"nibe/bw_reg_q": {
"name": "BW Reg Q",
"state_topic": "nibe/bw_reg_q",
"value_template": "{{ value | float }}",
"unit_of_measurement": "°C",
"device_class": "temperature",
"unique_id": "nibe_bw_reg_q"
},
"nibe/bw_reg_value_xp_percent": {
"name": "BW Reg Value XP Percent",
"unit_of_measurement": "%",
"state_topic": "nibe/bw_reg_value_xp_percent",
"unique_id": "nibe_bw_reg_value_xp_percent"
},
#enable if you want mqtt to publish time and date
# "nibe/date_year": {
# "name": "Date Year",
# "state_topic": "nibe/date_year",
# "unique_id": "nibe_date_year"
# },
# "nibe/date_month": {
# "name": "Date Month",
# "state_topic": "nibe/date_month",
# "unique_id": "nibe_date_month"
# },
# "nibe/date_day": {
# "name": "Date Day",
# "state_topic": "nibe/date_day",
# "unique_id": "nibe_date_day"
# },
# "nibe/time_hour": {
# "name": "Time Hour",
# "state_topic": "nibe/time_hour",
# "unique_id": "nibe_time_hour"
# },
# "nibe/time_minute": {
# "name": "Time Minute",
# "state_topic": "nibe/time_minute",
# "unique_id": "nibe_time_minute"
# },
# "nibe/time_second": {
# "name": "Time Second",
# "state_topic": "nibe/time_second",
# "unique_id": "nibe_time_second"
# },
"nibe/run_time_compressor_h": {
"name": "Run Time Compressor Hours",
"state_topic": "nibe/run_time_compressor_h",
"device_class": "duration",
"unit_of_measurement": "h",
"value_template": "{{ value | float }}",
"unique_id": "nibe_run_time_compressor_h"
},
"nibe/comp_freq_desired_hz": {
"name": "Comp Freq Desired Hz",
"unit_of_measurement": "Hz",
"state_topic": "nibe/comp_freq_desired_hz",
"unique_id": "nibe_comp_freq_desired_hz"
},
"nibe/comp_freq_actual_hz": {
"name": "Comp Freq Actual Hz",
"unit_of_measurement": "Hz",
"state_topic": "nibe/comp_freq_actual_hz",
"unique_id": "nibe_comp_freq_actual_hz"
},
"nibe/high_pressure_bar": {
"name": "High Pressure Bar",
"unit_of_measurement": "bar",
"state_topic": "nibe/high_pressure_bar",
"unique_id": "nibe_high_pressure_bar"
},
"nibe/low_pressure_bar": {
"name": "Low Pressure Bar",
"unit_of_measurement": "bar",
"state_topic": "nibe/low_pressure_bar",
"unique_id": "nibe_low_pressure_bar"
},
"nibe/ams_phase_is_a": {
"name": "AMS Phase Current",
"unit_of_measurement": "A",
"state_topic": "nibe/ams_phase_is_a",
"unique_id": "nibe_ams_phase_is_a"
},
"nibe/hysteresis": {
"name": "Hysteresis",
"state_topic": "nibe/hysteresis",
"state_class": "measurement",
"unit_of_measurement": "°C",
"device_class": "temperature",
"value_template": "{{ value | float }}",
"unique_id": "nibe_hysteresis"
},
"nibe/bw_reg_xp": {
"name": "BW Reg XP",
"state_topic": "nibe/bw_reg_xp",
"unit_of_measurement": "°C",
"device_class": "temperature",
"value_template": "{{ value | float }}",
"unique_id": "nibe_bw_reg_xp"
},
"nibe/stop_temp_heating_c": {
"name": "Stop Temp Heating",
"unit_of_measurement": "°C",
"state_topic": "nibe/stop_temp_heating_c",
"unique_id": "nibe_stop_temp_heating_c"
},
"nibe/domestic_hot_water_start_temp": {
"name": "Domestic Hot Water Start Temp",
"unit_of_measurement": "°C",
"state_topic": "nibe/domestic_hot_water_start_temp",
"unique_id": "nibe_domestic_hot_water_start_temp"
},
"nibe/domestic_hot_water_stop_temp": {
"name": "Domestic Hot Water Stop Temp",
"unit_of_measurement": "°C",
"state_topic": "nibe/domestic_hot_water_stop_temp",
"unique_id": "nibe_domestic_hot_water_stop_temp"
},
"nibe/outdoor_temp_c": {
"name": "Outdoor Temperature",
"unit_of_measurement": "°C",
"device_class": "temperature",
"state_topic": "nibe/outdoor_temp_c",
"unique_id": "nibe_outdoor_temp_c"
},
"nibe/flow_setpoint_c": {
"name": "Flow Setpoint Temperature",
"unit_of_measurement": "°C",
"device_class": "temperature",
"state_topic": "nibe/flow_setpoint_c",
"unique_id": "nibe_flow_setpoint_c"
},
"nibe/flow_actual_c": {
"name": "Flow Actual Temperature",
"unit_of_measurement": "°C",
"device_class": "temperature",
"state_topic": "nibe/flow_actual_c",
"unique_id": "nibe_flow_actual_c"
},
"nibe/compressor_starts": {
"name": "Compressor starts",
"state_topic": "nibe/compressor_starts",
"unit_of_measurement": "x",
"state_class": "total_increasing",
"value_template": "{{ value | float }}",
"unique_id": "nibe_compressor_starts"
},
"nibe/heating_status": {
"name": "Heating Status",
"state_topic": "nibe/heating_status",
"unique_id": "nibe_heating_status"
},
"nibe/additional_heating": {
"name": "Additional Heating Allowed",
"state_topic": "nibe/additional_heating_allowed",
"unique_id": "nibe_additional_heating_allowed",
"value_template": "{{ value | int }}", # Ensure Home Assistant interprets 0 and 1 as integers
"availability_topic": "nibe/status",
"payload_available": "online",
"payload_not_available": "offline",
"device": {
"identifiers": ["nibe_heat_pump"],
"name": "Nibe Heat Pump",
"manufacturer": "Nibe",
"model": "EasySolar",
"sw_version": "1.0"
}
},
}
# Publish MQTT discovery payload for each sensor
def publish_discovery_payloads():
for sensor, config in mqtt_discovery_sensors.items():
discovery_topic = f"homeassistant/sensor/{config['unique_id']}/config"
# Build the base payload
payload = {
"name": config["name"],
"state_topic": config["state_topic"],
"unique_id": config["unique_id"],
"availability_topic": "nibe/status",
"payload_available": "online",
"payload_not_available": "offline",
"device": {
"identifiers": ["nibe_heat_pump"],
"name": "Mitsubishi heavy industries Heat Pump",
"manufacturer": "MHI",
"model": "HMA100v",
"sw_version": "1.0"
}
}
# Add optional fields only if they exist
if config.get("unit_of_measurement"):
payload["unit_of_measurement"] = config["unit_of_measurement"]
if config.get("device_class"):
payload["device_class"] = config["device_class"]
if config.get("value_template"):
payload["value_template"] = config["value_template"]
# Publish the discovery payload as JSON
mqtt_client.publish(discovery_topic, str(payload).replace("'", '"'), qos=0, retain=True)
logger.info(f"Published MQTT discovery payload for {config['name']}")
# Initialize global variables to store register 28, 29, and 30 values
reg28_value = None
reg29_value = None
reg30_value = None
def _decode(reg, raw):
global reg28_value, reg29_value, reg30_value # Ensure these variables are accessible
if len(raw) == 2:
value = unpack('>H', raw)[0]
else:
value = unpack('B', raw)[0]
# Handle registers 28, 29, and 30
if reg == 28:
reg28_value = value
logger.debug(f"Register 28 (operation mode) value: {reg28_value}")
return None # Avoid falling through to unhandled case
elif reg == 29:
reg29_value = value
logger.debug(f"Register 29 (operation mode) value: {reg29_value}")
return None # Avoid falling through to unhandled case
elif reg == 30:
reg30_value = value
logger.debug(f"Register 30 (operation mode) value: {reg30_value}")
# Now that we have all three registers, interpret the state
if reg28_value == 0x0000 and reg29_value == 0x8222 and reg30_value == 0x0032:
publish_mqtt("nibe/operation_mode", "Pois päältä") #pwer on, heatpump off
elif reg28_value == 0x4409 and reg29_value == 0xA22A and reg30_value == 0x01FE:
publish_mqtt("nibe/operation_mode", "Käyttövesi") #domestic water
elif reg28_value == 0x0008 and reg29_value == 0xC22A and reg30_value == 0x000A:
publish_mqtt("nibe/operation_mode", "Pois päältä") #power on, heatpump off
elif reg28_value == 26634 and reg29_value == 49706 and reg30_value == 170:
publish_mqtt("nibe/operation_mode", "Öljy paluu") #oil return
elif reg28_value == 16394 and reg29_value == 49706 and reg30_value == 610:
publish_mqtt ("nibe/operation_mode", "Lämmitys") #heating
elif reg28_value == 16394 and reg29_value == 49706 and reg30_value == 10:
publish_mqtt ("nibe/operation_mode", "Lämmitys") #heating
elif reg28_value == 16650 and reg29_value == 49706 and reg30_value == 610:
publish_mqtt ("nibe/operation_mode", "Lämmitys") #heating
elif reg28_value == 0x0000 and reg29_value == 0xC22A and reg30_value == 0x003C:
publish_mqtt("nibe/operation_mode", "Vain sähkövastukset") #additional heating only
elif reg28_value == 16385 and reg29_value == 41514 and reg30_value == 50:
publish_mqtt("nibe/operation_mode", "Käyttövesi") #domestic water
elif reg28_value == 16393 and reg29_value == 49706 and reg30_value == 10:
publish_mqtt("nibe/operation_mode", "Lämmitys") # heating
elif reg28_value == 10 and reg29_value == 49706 and reg30_value == 10:
publish_mqtt("nibe/operation_mode", "Pois päältä") #power on, heatpump off
elif reg28_value == 28 and reg29_value == 49706 and reg30_value == 10:
publish_mqtt("nibe/operation_mode", "lämmitys") #heating
elif reg28_value == 10 and reg29_value == 49706 and reg30_value == 610:
publish_mqtt("nibe/operation_mode", "Pois päältä") #power on, heatpump off
elif reg28_value == 32776 and reg29_value == 49706 and reg30_value == 10:
publish_mqtt("nibe/operation_mode", "Jäätymisensuoja") #freeze protection
elif reg28_value == 32778 and reg29_value == 49706 and reg30_value == 10:
publish_mqtt("nibe/operation_mode", "Jäätymisensuoja") #freeze protection
elif reg28_value == 17419 and reg29_value == 41514 and reg30_value == 510:
publish_mqtt("nibe/operation_mode", "Käyttövesi") #domestic water
elif reg28_value == 17425 and reg29_value == 41514 and reg30_value == 450:
publish_mqtt("nibe/operation_mode", "LisäLV") #extra domestic water
elif reg28_value == 24586 and reg29_value == 49706 and reg30_value == 270:
publish_mqtt("nibe/operation_mode", "Sulatus") #defrost
elif reg28_value == 24842 and reg29_value == 49706 and reg30_value == 270:
publish_mqtt("nibe/operation_mode", "Sulatus") #defrost
elif reg28_value == 17409 and reg29_value == 41514 and reg30_value == 30:
publish_mqtt("nibe/operation_mode", "Käyttövesi") #domestic water
else:
logger.warning(f"Unknown combination of register values: reg28={reg28_value}, reg29={reg29_value}, reg30={reg30_value}")
publish_mqtt("nibe/operation_mode", f"Unknown mode: reg28={reg28_value}, reg29={reg29_value}, reg30={reg30_value}")
return None # Avoid falling through to unhandled case
# Handle additional heating using nibe/additional_heating_allowed
if reg == 32:
logger.debug(f"Register 32 (additional heating allowed) value: {value}")
publish_mqtt("nibe/additional_heating_allowed", str(value))
return None
# Handle heating status using register 31
if reg == 31:
logger.debug(f"Register 31 (heating status) value: {value}")
if value == 1:
publish_mqtt("nibe/heating_status", "auto")
elif value == 3:
publish_mqtt("nibe/heating_status", "lämmitys") #heating
elif value == 5:
publish_mqtt("nibe/heating_status", "lämminvesi") #domestic water
elif value == 6:
publish_mqtt("nibe/heating_status", "lisäys (sähkö)") #additional heating
return None
# Handle temperature and flow registers
if reg in [1, 5, 6, 7, 12, 23, 11, 13, 14, 15, 16, 17, 18, 21]:
logger.debug(f"Register {reg} (temperature/flow) value: {value}")
return float(unpack('h', pack('H', value))[0] / 10)
# Handle general integer registers
if reg in [0, 33, 34, 35, 36, 38, 44, 45, 46, 48, 100, 101, 102, 103, 104, 105]:
logger.debug(f"Register {reg} (general integer) value: {value}")
return int(value)
# Handle signed values for heating curve and degree minutes
if reg in [4, 8]:
logger.debug(f"Register {reg} (signed value) value: {value}")
return int(unpack('h', pack('H', value))[0] / 10)
# Handle compressor starts
if reg == 25:
logger.debug(f"Register 25 (compressor starts) value: {value}")
return int(value / 10)
# Handle frequency, pressure, phase, and runtime registers
if reg in [9, 10, 19, 20, 22, 24]:
logger.debug(f"Register {reg} (frequency/pressure) value: {value}")
return float(value / 10)
# Handle hysteresis and BW registers
if reg in [40, 47]:
logger.debug(f"Register {reg} (hysteresis/BW reg) value: {value}")
return float(value / 2)
# Handle stop temperature and domestic water temperatures
if reg in [43, 49, 50]:
logger.debug(f"Register {reg} (temperature) value: {value}")
return float(value)
# Log unknown registers
logger.warning(f"Register {reg} is not handled")
return None
def run():
logger.info("Starting the main loop...")
# Publish availability as "online" at the start of the script
publish_availability("online")
# Publish discovery payloads for all sensors
publish_discovery_payloads()
mqtt_client.loop_start()
try:
while True:
try:
if ser.read(1)[0] != 0x03:
logger.debug("No start byte found")
continue
logger.debug("Start byte found, reading data...")
ret = ser.read(2)
if ret[0] != 0x00 or ret[1] != 0x14:
logger.debug("Invalid start frame")
continue
ser.write(b"\x06")
frm = ser.read(4)
if frm[0] == 0x03:
continue
l = int(frm[3])
frm += ser.read(l + 1)
ser.write(b"\x06")
crc = 0
for i in frm[:-1]:
crc ^= i
if crc != frm[-1]:
logger.warning("Frame CRC error")
continue
msg = frm[4:-1]
l = len(msg)
i = 4
while i <= l:
reg = msg[i - 3]
if i != l and (msg[i] == 0x00 or i == (l - 1)):
raw = bytes([msg[i - 2], msg[i - 1]])
i += 4
else:
raw = bytes([msg[i - 2]])
i += 3
if reg in nibe_registers:
value = _decode(reg, raw)
if value is not None:
mqtt_topic = nibe_registers[reg]
publish_mqtt(mqtt_topic, value)
except Exception as e:
logger.warning(f"Error in Nibe data processing: {e}")
time.sleep(1)
except KeyboardInterrupt:
logger.info("Script interrupted, shutting down...")
finally:
# Publish availability as "offline" when the script is stopped
publish_availability("offline")
mqtt_client.loop_stop()
mqtt_client.disconnect()
if __name__ == "__main__":
logger.info("Starting Nibe heat pump MQTT bridge")
run()