diff --git a/.gitignore b/.gitignore index 9e68c6e..422457b 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,5 @@ .vscode/ # PlatformIo -.pio/ \ No newline at end of file +.pio/ +.DS_Store diff --git a/assets/p1_sensors.yaml b/assets/p1_sensors.yaml index 4c3f2bc..107c23c 100644 --- a/assets/p1_sensors.yaml +++ b/assets/p1_sensors.yaml @@ -144,6 +144,15 @@ state_topic: "sensors/power/p1meter/gas_meter_m3" value_template: "{{ value|float / 1000 }}" +- platform: mqtt + name: P1 Actual Gas Consumption + unique_id: 'sensor.p1_actual_gas_consumption' + device_class: gas + state_class: measurement + unit_of_measurement: 'm³' + state_topic: "sensors/power/p1meter/actual_consumption_gas_m3" + value_template: "{{ value|float / 1000 }}" + - platform: mqtt name: P1 Actual Tariff Group unique_id: 'sensor.p1_actual_tariff_group' diff --git a/esp8266_p1meter/esp8266_p1meter.ino b/esp8266_p1meter/esp8266_p1meter.ino index f3a04dc..53eabde 100644 --- a/esp8266_p1meter/esp8266_p1meter.ino +++ b/esp8266_p1meter/esp8266_p1meter.ino @@ -129,10 +129,24 @@ void send_metric(String name, long metric) void send_data_to_broker() { - send_metric("consumption_low_tarif", CONSUMPTION_LOW_TARIF); - send_metric("consumption_high_tarif", CONSUMPTION_HIGH_TARIF); - send_metric("returndelivery_low_tarif", RETURNDELIVERY_LOW_TARIF); - send_metric("returndelivery_high_tarif", RETURNDELIVERY_HIGH_TARIF); + if (FLIPHIGHLOWTARIF) { + send_metric("consumption_low_tarif", CONSUMPTION_HIGH_TARIF); + send_metric("consumption_high_tarif", CONSUMPTION_LOW_TARIF); + send_metric("returndelivery_low_tarif", RETURNDELIVERY_HIGH_TARIF); + send_metric("returndelivery_high_tarif", RETURNDELIVERY_LOW_TARIF); + if (ACTUAL_TARIF == 1) + { + ACTUAL_TARIF = 2; + } else { + ACTUAL_TARIF = 1; + } + } else { + send_metric("consumption_low_tarif", CONSUMPTION_LOW_TARIF); + send_metric("consumption_high_tarif", CONSUMPTION_HIGH_TARIF); + send_metric("returndelivery_low_tarif", RETURNDELIVERY_LOW_TARIF); + send_metric("returndelivery_high_tarif", RETURNDELIVERY_HIGH_TARIF); + } + send_metric("actual_consumption", ACTUAL_CONSUMPTION); send_metric("actual_returndelivery", ACTUAL_RETURNDELIVERY); @@ -147,6 +161,7 @@ void send_data_to_broker() send_metric("l3_voltage", L3_VOLTAGE); send_metric("gas_meter_m3", GAS_METER_M3); + send_metric("actual_consumption_gas_m3", ACTUAL_CONSUMPTION_GAS_M3); send_metric("actual_tarif_group", ACTUAL_TARIF); send_metric("short_power_outages", SHORT_POWER_OUTAGES); @@ -373,6 +388,14 @@ bool decode_telegram(int len) { GAS_METER_M3 = getValue(telegram, len, '(', '*'); } + + + // 0-1:24.2.3(150531200000S)(00811.923*m3) + // 0-1:24.2.3 = Gas on Belgian meters + if (strncmp(telegram, "0-1:24.2.3", strlen("0-1:24.2.3")) == 0) + { + GAS_METER_M3 = getValue(telegram, len, '(', '*'); + } // 0-0:96.14.0(0001) // 0-0:96.14.0 = Actual Tarif @@ -436,6 +459,14 @@ void processLine(int len) { bool result = decode_telegram(len + 1); if (result) { + if (LAST_GAS_METER_M3 > 0) { + if (GAS_METER_M3 > LAST_GAS_METER_M3) { + ACTUAL_CONSUMPTION_GAS_M3 = GAS_METER_M3 - LAST_GAS_METER_M3; + } + } else { + ACTUAL_CONSUMPTION_GAS_M3 = 0; + } + LAST_GAS_METER_M3 = GAS_METER_M3; send_data_to_broker(); LAST_UPDATE_SENT = millis(); } diff --git a/esp8266_p1meter/settings.h b/esp8266_p1meter/settings.h index 4fb2a62..38aa688 100644 --- a/esp8266_p1meter/settings.h +++ b/esp8266_p1meter/settings.h @@ -32,6 +32,9 @@ // * MQTT root topic #define MQTT_ROOT_TOPIC "sensors/power/p1meter" +// * Belgian meters have flipped high and low tarif codes, this variable allows you to flip +#define FLIPHIGHLOWTARIF true + // * MQTT Last reconnection counter long LAST_RECONNECT_ATTEMPT = 0; @@ -56,6 +59,8 @@ long RETURNDELIVERY_HIGH_TARIF; long ACTUAL_CONSUMPTION; long ACTUAL_RETURNDELIVERY; long GAS_METER_M3; +long ACTUAL_CONSUMPTION_GAS_M3; +long LAST_GAS_METER_M3; long L1_INSTANT_POWER_USAGE; long L2_INSTANT_POWER_USAGE;