Skip to content

Instantly share code, notes, and snippets.

@wildekek
Last active July 21, 2024 08:27
Show Gist options
  • Save wildekek/4e19fb049eb7cea3a35fe801e3ed8f57 to your computer and use it in GitHub Desktop.
Save wildekek/4e19fb049eb7cea3a35fe801e3ed8f57 to your computer and use it in GitHub Desktop.
CT Clamp power consumption for Home Assistant
# Creates Home Assistant Power and Energy sensors for APSystems ECU-C
# Copy this file to /homeassistant/packages/ecu.yaml
# Add the following lines to the /homeassistant/configuration.yaml:
# homeassistant:
# packages:
# system: !include packages/ecu.yaml
# Update the IP (192.168.0.6) to the address your ECU runs at
# Update the ECUID (215000015509) to your ECUID. You can find this on the main webpage of the ECU
rest:
# Fetch power data
# This data is generated by measuring the CT clamps.
- resource: "http://192.168.0.6/index.php/hidden/set_get_app_info_json"
method: POST
scan_interval: 300
payload: '{"Command_Id": "53","APS": "ASK","Command": "1","Version": "1.1","ECUID": "215000015509"}'
sensor:
- name: Solar Power Produced L1
unique_id: solar_power_produced_l1
unit_of_measurement: kW
state_class: measurement
device_class: power
value_template: '{{ value_json["meter_data"]["power1"][-1]["powerA"] / 1000}}'
- name: Solar Power Produced L2
unique_id: solar_power_produced_l2
unit_of_measurement: kW
state_class: measurement
device_class: power
value_template: '{{ value_json["meter_data"]["power1"][-1]["powerB"] / 1000 }}'
- name: Solar Power Produced L3
unique_id: solar_power_produced_l3
unit_of_measurement: kW
state_class: measurement
device_class: power
value_template: '{{ value_json["meter_data"]["power1"][-1]["powerC"] / 1000 }}'
- name: Grid Power Produced L1
unique_id: grid_power_produced_l1
unit_of_measurement: kW
state_class: measurement
device_class: power
value_template: >-
{% set power = value_json["meter_data"]["power2"][-1]["powerA"] / 1000 %}
{% if power < 0 %}
{{ power * -1 }}
{% else %}
0
{% endif %}
- name: Grid Power Produced L2
unique_id: grid_power_produced_l2
unit_of_measurement: kW
state_class: measurement
device_class: power
value_template: >-
{% set power = value_json["meter_data"]["power2"][-1]["powerB"] / 1000 %}
{% if power < 0 %}
{{ power * -1 }}
{% else %}
0
{% endif %}
- name: Grid Power Produced L3
unique_id: grid_power_produced_l3
unit_of_measurement: kW
state_class: measurement
device_class: power
value_template: >-
{% set power = value_json["meter_data"]["power2"][-1]["powerC"] / 1000 %}
{% if power < 0 %}
{{ power * -1 }}
{% else %}
0
{% endif %}
- name: Grid Power Consumed L1
unique_id: grid_power_consumed_l1
unit_of_measurement: kW
state_class: measurement
device_class: power
value_template: >-
{% set power = value_json["meter_data"]["power2"][-1]["powerA"] / 1000 %}
{% if power > 0 %}
{{ power }}
{% else %}
0
{% endif %}
- name: Grid Power Consumed L2
unique_id: grid_power_consumed_l2
unit_of_measurement: kW
state_class: measurement
device_class: power
value_template: >-
{% set power = value_json["meter_data"]["power2"][-1]["powerB"] / 1000 %}
{% if power > 0 %}
{{ power }}
{% else %}
0
{% endif %}
- name: Grid Power Consumed L3
unique_id: grid_power_consumed_l3
unit_of_measurement: kW
state_class: measurement
device_class: power
value_template: >-
{% set power = value_json["meter_data"]["power2"][-1]["powerC"] / 1000 %}
{% if power > 0 %}
{{ power }}
{% else %}
0
{% endif %}
- name: Domestic Power Consumed L1
unique_id: domestic_power_consumed_l1
unit_of_measurement: kW
state_class: measurement
device_class: power
value_template: '{{ (value_json["meter_data"]["power1"][-1]["powerA"] + value_json["meter_data"]["power2"][-1]["powerA"]) / 1000 }}'
- name: Domestic Power Consumed L2
unique_id: domestic_power_consumed_l2
unit_of_measurement: kW
state_class: measurement
device_class: power
value_template: '{{ (value_json["meter_data"]["power1"][-1]["powerB"] + value_json["meter_data"]["power2"][-1]["powerB"]) / 1000 }}'
- name: Domestic Power Consumed L3
unique_id: domestic_power_consumed_l3
unit_of_measurement: kW
state_class: measurement
device_class: power
value_template: '{{ (value_json["meter_data"]["power1"][-1]["powerC"] + value_json["meter_data"]["power2"][-1]["powerC"]) / 1000 }}'
# Fetch energy data
# This data is generated by measuring the CT clamps.
scrape:
- resource: http://192.168.0.6/index.php/display/historical_data/meter_lifetime_energy/
scan_interval: 300
sensor:
- name: Solar Energy
unique_id: solar_energy
unit_of_measurement: kWh
device_class: energy
state_class: total
select: "div.table-responsive > table > tbody > tr:nth-of-type(1) > td:nth-of-type(3)"
value_template: '{{ value| float | round(3) }}'
- name: Grid Energy
unique_id: grid_energy
unit_of_measurement: kWh
device_class: energy
state_class: total
select: "div.table-responsive > table > tbody > tr:nth-of-type(2) > td:nth-of-type(3)"
value_template: '{{ value| float | round(3) }}'
template:
sensor:
# We need the inverted Grid Energy to feed the Grid Production meter
- name: "Grid Energy Inverted"
unique_id: grid_energy_inverted
state_class: total
device_class: energy
unit_of_measurement: "kWh"
state: "{{ states('sensor.grid_energy') | float * -1 }}"
utility_meter:
solar_energy_produced:
name: "Solar Energy Produced"
unique_id: solar_energy_produced
source: sensor.solar_energy
delta_values: false
net_consumption: false
periodically_resetting: false
grid_energy_consumed:
name: "Grid Energy Consumed"
unique_id: grid_energy_consumed
source: sensor.grid_energy
delta_values: false
net_consumption: false
periodically_resetting: false
grid_energy_produced:
name: "Grid Energy Produced"
unique_id: grid_energy_produced
source: sensor.grid_energy_inverted
delta_values: false
net_consumption: false
periodically_resetting: false
sensor:
# Calculate total power values
- platform: min_max
name: Solar Power Produced
unique_id: solar_power_produced
round_digits: 3
type: sum
entity_ids:
- sensor.solar_power_produced_l1
- sensor.solar_power_produced_l2
- sensor.solar_power_produced_l3
- platform: min_max
name: Grid Power Produced
unique_id: grid_power_produced
round_digits: 3
type: sum
entity_ids:
- sensor.grid_power_produced_l1
- sensor.grid_power_produced_l2
- sensor.grid_power_produced_l3
- platform: min_max
name: Grid Power Consumed
unique_id: grid_power_consumed
round_digits: 3
type: sum
entity_ids:
- sensor.grid_power_consumed_l1
- sensor.grid_power_consumed_l2
- sensor.grid_power_consumed_l3
- platform: min_max
name: Domestic Power Consumed
unique_id: domestic_power_consumed
round_digits: 3
type: sum
entity_ids:
- sensor.domestic_power_consumed_l1
- sensor.domestic_power_consumed_l2
- sensor.domestic_power_consumed_l3
@tonejay62
Copy link

tonejay62 commented Feb 1, 2024

Everything is Ok !!! Thanks !
image

image

@tonejay62
Copy link

Hi ! Everythings seems to be OK but today i've lost one hour in my dashboard...do you know why ?
image

@wildekek
Copy link
Author

wildekek commented Feb 8, 2024

You're not giving me a lot to go on there buddy. That said, I don't think data is 'gone' but was just acquired at a later point in time. Probably Home Assistant was unable to reach the ECU for some reason. I would not worry about it too much tbh.

@tonejay62
Copy link

Indeed, they were grouped together later, after restarting the ECU-C.
image

I was forced to restart it because it stopped responding to ping and seemed unreachable. It happened again 3 days later... :|
Do you know why it crashes sometimes? and how can we ensure that this doesn’t happen?

@trojansc
Copy link

trojansc commented Mar 16, 2024

Capture d’écran 2024-03-16 à 17 46 22 Capture d’écran 2024-03-16 à 17 47 34 Capture d’écran 2024-03-16 à 17 47 13 Capture d’écran 2024-03-16 à 17 47 03

Hello,

I have followed all the steps provided, however, I still see zero production despite another consumption tracking module indicating overproduction. I specifically need the status "sensor.solar_energy_produced". The version of the add-on "APSystems ECU-R" I am using is v1.4.1.

Could you please assist me with this?

Thank you in advance.

Edit : After uninstall / reinstall its work now

@luispb
Copy link

luispb commented Jul 9, 2024

Thank you for this code @wildekek . It works perfectly :-)

@luispb
Copy link

luispb commented Jul 10, 2024

Hey @wildekek, I would like to suggest one additional feature that will add the "Imported/Exported" sensor to Home Assistant replicating the values you get in the APSystems app. That would be the grey line in the screenshot below:

image

We can get these values from this URL: 'http:///index.php/meter/old_meter_power_graph'

To create this new sensor would only require adding the code below to the existing ecu.yaml

# Update the IP (192.168.0.6) to the address your ECU runs at
      - platform: rest
	name: "Import Export Power" # Grid Import is (+) / Export is (-)
	unique_id: import_export_power
	device_class: power
	state_class: measurement
	unit_of_measurement: W
	resource: http://192.168.0.6/index.php/meter/old_meter_power_graph
	scan_interval: 300
	value_template: >
		{% if value_json is defined %}
				{{ value_json.power2[-1].powerA }}
		{% else %}
				{{ "None" }}
		{% endif %}

Please, let me know what you think. And congratulations again for the great work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment