Skip to content

Instantly share code, notes, and snippets.

@zubir2k
Last active September 20, 2021 23:22
Show Gist options
  • Save zubir2k/41afa21f40f7d53a22abacc60c8632b7 to your computer and use it in GitHub Desktop.
Save zubir2k/41afa21f40f7d53a22abacc60c8632b7 to your computer and use it in GitHub Desktop.
Calculate TNB Bill based on Energy Sensor
########################
## configuration.yaml ##
########################
## Change sensor name accordingly (sensor.sonoff_xxxxxxxxx)
## Original code from bro Ishak (https://github.com/ishakmuhamad/bill_tnb_ha)
## AlexxIT/SonoffLAN ##
sonoff:
username: your@email.here
password: password
force_update: [temperature, power]
scan_interval: '00:01:00'
sensors: [temperature, humidity, power, current, voltage, rssi]
reload: always
## Energy Meter ##
utility_meter:
daily_energy:
source: sensor.energy_consumed
cycle: daily
tariffs:
- normal
monthly_energy:
source: sensor.energy_consumed
cycle: yearly
tariffs:
- normal
## Sensors ##
sensor:
## TNB energy reading ##
- platform: integration
name: Energy Consumed
source: sensor.sonoff_xxxxxxxxx_power #sensor device dalam watt
- platform: template
sensors:
# New Energy Sensors #
daily_power_kw:
friendly_name: "Daily kWh"
unit_of_measurement: "kWh"
device_class: energy
value_template: '{{ (states("sensor.daily_energy_normal") | round) / 1000 | float }}'
icon_template: mdi:counter
monthly_power_kw:
friendly_name: "Monthly kWh"
unit_of_measurement: "kWh"
device_class: energy
value_template: '{{ (states("sensor.monthly_energy_normal") | round) / 1000 | float }}'
icon_template: mdi:counter
# Bacaan Blok TNB
- platform: template
sensors:
1stbill_tnb_rm:
friendly_name: "1st Jumlah Bill TNB"
unit_of_measurement: "MYR"
icon_template: mdi:currency-usd
device_class: monetary
value_template: >-
{% if states('sensor.monthly_power_kw') | float < 201 %}
{% set G = states('sensor.monthly_power_kw') | float %}
{{ (G * 0.218) | round(2) }}
{% elif states('sensor.monthly_power_kw') | float >= 201 %}
43.60
{%- else %}
Unknown
{%- endif %}
2ndbill_tnb_rm:
friendly_name: "2nd Jumlah Bill TNB"
unit_of_measurement: "MYR"
icon_template: mdi:currency-usd
device_class: monetary
value_template: >-
{% if states('sensor.monthly_power_kw') | float <= 200 %}
0
{% elif states('sensor.monthly_power_kw') | float >= 301 %}
33.40
{% elif states('sensor.monthly_power_kw') | float >= 201 < 300 %}
{% set H = states('sensor.monthly_power_kw') | float - 200 %}
{{ (H * 0.334) | round(2) }}
{%- else %}
Unknown
{%- endif %}
- platform: template
sensors:
3rdbill_tnb_rm:
friendly_name: "3rd Jumlah Bill TNB"
unit_of_measurement: "MYR"
icon_template: mdi:currency-usd
device_class: monetary
value_template: >-
{% if states('sensor.monthly_power_kw') | float <= 300 %}
0
{% elif states('sensor.monthly_power_kw') | float >= 601 %}
154.80
{% elif states('sensor.monthly_power_kw') | float >= 301 < 600 %}
{% set I = states('sensor.monthly_power_kw') | float - 300 %}
{{ (I * 0.516) | round(2) }}
{%- else %}
Unknown
{%- endif %}
- platform: template
sensors:
4thbill_tnb_rm:
friendly_name: "4th Jumlah Bill TNB"
unit_of_measurement: "MYR"
icon_template: mdi:currency-usd
device_class: monetary
value_template: >-
{% if states('sensor.monthly_power_kw') | float <= 600 %}
0
{% elif states('sensor.monthly_power_kw') | float >= 901 %}
163.80
{% elif states('sensor.monthly_power_kw') | float >= 601 < 900 %}
{% set J = states('sensor.monthly_power_kw') | float - 600 %}
{{ (J * 0.546) | round(2) }}
{%- else %}
Unknown
{%- endif %}
- platform: template
sensors:
5thbill_tnb_rm:
friendly_name: "5th Jumlah Bill TNB"
unit_of_measurement: "MYR"
icon_template: mdi:currency-usd
device_class: monetary
value_template: >-
{% if states('sensor.monthly_power_kw') | float <= 900 %}
0
{% elif states('sensor.monthly_power_kw') | float >= 901 %}
{% set K = states('sensor.monthly_power_kw') | float - 900 %}
{{ (K * 0.571) | round(2) }}
{%- else %}
Unknown
{%- endif %}
# Jumlah Bill TNB
- platform: template
sensors:
to_bill_tnb_rm:
friendly_name: "Total Bill TNB"
unit_of_measurement: "MYR"
icon_template: mdi:currency-usd
device_class: monetary
value_template: >-
{% set G = states('sensor.1stbill_tnb_rm') | float %}
{% set H = states('sensor.2ndbill_tnb_rm') | float %}
{% set I = states('sensor.3rdbill_tnb_rm') | float %}
{% set J = states('sensor.4thbill_tnb_rm') | float %}
{% set K = states('sensor.5thbill_tnb_rm') | float %}
{{ (G + H + I + J + K) | round(2) }}
# Average Daily Bill TNB
- platform: template
sensors:
daily_tnb_rm:
friendly_name: "Daily Bill TNB"
unit_of_measurement: "MYR"
icon_template: mdi:currency-usd
device_class: monetary
value_template: >-
{% set Y = states('sensor.daily_power_kw') | float %}
{{ (Y * 0.218) | round(2) }}
actual_tnb_kw:
friendly_name: "TNB Meter kWh"
unit_of_measurement: "kWh"
value_template: >-
{{ states.input_number.tnb_current_month.state | float(2) - states.input_number.tnb_previous_month.state | float(2) }}
icon_template: mdi:counter
##############
## Lovelace ##
##############
- title: Energy
path: energy
icon: mdi:gauge
type: sidebar
badges: []
cards:
- type: gauge
min: 0
entity: sensor.sonoff_xxxxxxxxx_current
max: 62
severity:
green: 0
yellow: 40
red: 55
name: Current Load
needle: true
view_layout:
position: sidebar
- type: custom:mini-graph-card
entities:
- entity: sensor.daily_power_kw
name: TODAY ENERGY CONSUMPTION
show:
graph: bar
view_layout:
position: sidebar
- type: vertical-stack
cards:
- type: entities
icon: mdi:gauge
entities:
- entity: sensor.daily_power_kw
- entity: sensor.monthly_power_kw
- entity: sensor.daily_tnb_rm
title: Energy Consumptions
- type: entities
entities:
- entity: sensor.actual_tnb_kw
- entity: input_number.tnb_previous_month
- entity: input_number.tnb_current_month
title: TNB Meter Reading
icon: mdi:gauge
view_layout:
position: sidebar
- type: entities
icon: mdi:power-plug
entities:
- entity: sensor.sonoff_xxxxxxxxx_power
- entity: sensor.sonoff_xxxxxxxxx_voltage
title: Main Switch Status
view_layout:
position: sidebar
- type: history-graph
entities:
- entity: sensor.sonoff_xxxxxxxxx_voltage
- entity: sensor.sonoff_xxxxxxxxx_power
- entity: sensor.sonoff_xxxxxxxxx_current
hours_to_show: 24
refresh_interval: 0
title: Energy Readings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment