Custom Component: IoTaWatt Energy Monitor integration

My Iotawatt is in the mail. I was thinking of setting up an influxdb or emoncms server to send the data to. Could that be a solution?

You can.

But you still need to perform the processing of the data you’re going to store somewhere.

OK so where you say “I calculate” where do you do that. In HA?

I personally push the data in influxdb, then using a rest sensor pull what I need from that data into HA. This has worked for me as I used influx to view long term data. Now HA is starting to dip it toe into long term data I might change that at some point an instead pull the data straight into HA which drops my need for influx.

Thanks. I am about to set up an influxdb server to trial it out. Can you post your rest sensor code?

As I was typing this reply I worked out we are probably having this conversation in the wrong place, this thread is dedicated to the custom component which I am not using. If you need follow up questions on doing integration through rest might be better moving elsewhere. That said I had already written my reply so here goes:

Below is my code for pulling the data from Influx, this pulls in data for all the circuts, so I then create individual sensors for each circut and finally convert from W to kwh.

############################################################################
## Pull IotaWatt details from Influx
############################################################################
  - platform: rest
    name: iotawatt
    resource: 'http://192.168.1.166:8086/query?db=IoTaWatt&q=SELECT%20last(%22Watts%22)%20FROM%20%22IotaWatt%22.%22autogen%22.%22iotawatt%22%20WHERE%20time%20%3E%20NOW()-10m%20GROUP%20BY%20entity_id'
    username: xxxx
    password: xxxx
    authentication: basic
    value_template: 'OK'
    json_attributes:
      - results

############################################################################
## Create Sensors for each IoTaWatt Circut
############################################################################
  - platform: template
    sensors:
      iotawatt_mains_realtime_power:
        value_template: '{% for row in state_attr("sensor.iotawatt", "results")[0]["series"] -%}{% if row["tags"]["entity_id"] == "mains" -%}{{ row["values"][0][1] | float }}{% endif -%}{% endfor -%}'
        unit_of_measurement: W
        device_class: power
        friendly_name_template: 'IoTaWatt mains'
      iotawatt_solar_realtime_power:
        value_template: '{% for row in state_attr("sensor.iotawatt", "results")[0]["series"] -%}{% if row["tags"]["entity_id"] == "solar" -%}{{ row["values"][0][1] | float }}{% endif -%}{% endfor -%}'
        unit_of_measurement: W
        device_class: power
        friendly_name_template: 'IoTaWatt solar'

############################################################################
## Convert W to kwh
############################################################################
  - platform: integration
    source: sensor.iotawatt_mains_realtime_power
    name: power_mains_kwh
    method: left
    unit_prefix: k
    round: 2

  - platform: integration
    source: sensor.iotawatt_solar_realtime_power
    name: power_solar_kwh
    method: left
    unit_prefix: k
    round: 2
1 Like

I’m thinking that we could probably use just the iotawatt and the “integration” platform

So rather than using the blah_wh sensor generated by the IoTaWatt integration, you use the sensor directly and create a new sensor based on the integration platform.
This will estimate the Wh, with probably sufficient accuracy for drawing nice graph.

I’m going to try that, and if I find that I have too much discrepancy I’ll write some code that calculates the energy value from the collected measurement (performed every 6s).

So using @Eoin template above, use the Convert W to kWh part bit.

1 Like

Looking forward to hearing how it goes

So I created two outputs in the IotaWatt:
Import : Watt = (Phase1 + Phase2 + Phase3 + Tesla1 + Tesla2 + Tesla3 + Pool) + Solar1 + Solar2 + AC1 + AC2 + AC3 + HotWater max 0
and
Export: Watts = Phase1 + Phase2 + Phase3 + Tesla1 + Tesla2 + Tesla3 + Pool + Solar1 + Solar2 + AC1 + AC2 + AC3 + HotWater min 0

This gives negative value for imports, same for solar production sensors; and I want to keep it that way
So in HA I have template sensors that make them always positive:

- platform: template
  sensors:
    energy_solar1_wh:
      friendly_name: "Solar1 Energy"
      unit_of_measurement: Wh
      device_class: energy
      value_template: "{{ states('sensor.iotawatt_input_solar1_wh')|float|abs }}"
      attribute_templates:
        last_reset: "{{ state_attr('sensor.iotawatt_input_solar1_wh', 'last_reset') }}"
        state_class: "{{ state_attr('sensor.iotawatt_input_solar1_wh', 'state_class') }}"
    energy_solar2_wh:
      friendly_name: "Solar2 Energy"
      unit_of_measurement: Wh
      device_class: energy
      value_template: "{{ states('sensor.iotawatt_input_solar2_wh')|float|abs }}"
      attribute_templates:
        last_reset: "{{ state_attr('sensor.iotawatt_input_solar2_wh', 'last_reset') }}"
        state_class: "{{ state_attr('sensor.iotawatt_input_solar2_wh', 'state_class') }}"
    energy_solar_total_wh:
      friendly_name: "Solar Total Energy Output"
      unit_of_measurement: Wh
      device_class: energy
      value_template: "{{ states('sensor.iotawatt_output_total_solar_wh')|float|abs }}"
      attribute_templates:
        last_reset: "{{ state_attr('sensor.iotawatt_output_total_solar_wh', 'last_reset') }}"
        state_class: "{{ state_attr('sensor.iotawatt_output_total_solar_wh', 'state_class') }}"
    power_grid_import:
      friendly_name: "Grid Power Import"
      unit_of_measurement: W
      device_class: power
      value_template: "{{ states('sensor.iotawatt_output_import')|float|abs }}"
      attribute_templates:
        last_reset: "{{ state_attr('sensor.iotawatt_output_import', 'last_reset') }}"
        state_class: "{{ state_attr('sensor.iotawatt_output_import', 'state_class') }}"
    power_grid_export:
      friendly_name: "Grid Power Export"
      unit_of_measurement: W
      device_class: power
      value_template: "{{ states('sensor.iotawatt_output_export')|float|abs }}"
      attribute_templates:
        last_reset: "{{ state_attr('sensor.iotawatt_output_export', 'last_reset') }}"
        state_class: "{{ state_attr('sensor.iotawatt_output_export', 'state_class') }}"

and two integrations sensors:

- platform: integration
  source: sensor.power_grid_export
  name: energy_export_wh
  method: right

- platform: integration
  source: sensor.power_grid_import
  name: energy_import_wh
  method: right

And I would configure HA energy with the relevant sensor:
sensor.energy_import_wh for import
sensor.energy_export_wh for export
energy_solar1_wh for my first solar array
energy_solar2_wh for the second.
(haven’t decided if I like have separate solar data rather than the total, will probably change as I’m not a fan of the current graph HA is drawing)

Not sure why @Eoin would want to use the left method over the default trapezoidal.

Edit: you need to use the right method for the integration component as we are adding values that have already been averaged.

Looking at your top two equations they are exactly the same except for the max and min at the end. Is that right? If so how?

Edit: I have thought about it some more and I get it. I was thrown by the brackets you had in the top equation and thought that something different should be going on.

Is this an issue because you’re using solar? I have a simple system without solar, and I find that Home Assistant matches exactly what my IoTaWatt reports for daily energy usage. :thinking:

Yes.
Because how you calculate Export vs Import has to be done whenever a read is done by the IoTaWatt.

When you query it via the REST API it will do:
SUM(energy used)-SUM(energy_produced)

consider the following:
you have 2 inputs defined: main and solar.
It’s now August, you’ve consumed 3MWh since the beginning of the year, and your solar panels have generated 1MWh of energy
if you were to query the iota how much energy you’ve imported it will do:
3MHz - 1MHh = 2MWh
and you’ve exported 0,because your consumption is greater than your production under this scenario.

But that is not how export are calculated. You need to measure for each sample (e.g. about 30 times per second for the iota) if your usage is greater than solar and if so you’re exporting.

So say you’re appliances are running flat at 2kW, after a day you’ve used 48kWh.
But during that time, your solar produce energy between 8AM and 5PM, and during that time the power they produced was 3kW.
Between 8AM, you’ve exported (3kW-2kW)*1h = 1kWh
so you’ve exported that day 1kWh * (17:00 - 8:00) = 9kWh

In the example earlier you had produced 1MWh on a single hour, your import would have been 3MWh and your export about 1MWh!

As the author of iota watt explained Wrong values returned by REST API? - explained - #7 by overeasy - Support - IoTaWatt User Community
to calculate it properly, the iotawatt for each queries by HA would need to run thousands of calculations for every samples recorded for that year.
It just can’t, it doesn’t have the storage nor the processing power to do so.

If you don’t have solar, and never ever export, then it doesn’t matter how you calculate it, the sum of the averages is identical to the average of the sums

I hope this post explains properly on what is going on here.

1 Like

Thank you for the detailed explanation. Makes sense to me now.

So if I understand it correctly the integration is retrieving the kWh values. Why doesn’t/can’t it just retrieve the current power value, say every second, and then HA could do the running calculation of energy used/exported?

This might be what your template is doing just want to check. If it is what you are doing why isn’t this accurate (enough)?

This is what the above with the “integration” integration is doing.

It’s not particularly accurate from what I’ve seen though, about 10% off. I have an idea on how we could retrieve the data from the iota that would better calculate export/import.

This won’t be using this iota watt integration however, will have to do a REST one manually

Ok that is quite an error. Do you have a feel for where the error creeps in?
A) Poll rate of Iotawatt? (Is it once a second?)
B) Accuracy of Iotawatt measurement since the last poll (a second ago?)?
C) Calculation of the Integration integration?

I read the accuracy of the Iotawatt is about 1%?

every 30s it polls the average of the last 30s

It should be more than enough for accuracy.
When I plot a graph

and over my mouse on the graph, the value you read there is spot-on. This graph was made at 11h so it uses minutes interval.

So the problem is obviously not a polling interval being too long.
I’m told that PVOutput uses 5 minutes interval and get very accurate result (I’ve always used one minute interval myself over the past 10 years and it’s been very accurate, not much different to what I’m billing for.

with a properly calibrated voltage sensor, the accuracy of the iota watt is extremely good, way better than 1% in my experience.

I used the default, I’ve just changed to “left” method will see if that’s any better.

Because right now it’s not very good, and the integration method being wrong is the only thing that makes sense here. All other values are correct.

Thanks that all makes sense. If we get a reading every minute then why can’t we just take that reading, lets say it is 600W, and divide that by 60 and then by 1000 to get that minutes worth of kWhs (0.01kWh in my example) and then add that to a running total? Wouldn’t that work instead of doing integration?

Reading again the Riesmann integration, with the left method this is exactly what it will do
We are adding averages already, so there’s no point to use the default integration method. It would over read.

good to know. Look forward to hearing your results. I have a few of these I have been playing with to keep track of total power of some lights. I will go and change these to the left method…