Powerpal smart energy monitor

I possibly connected incorrectly. I couldn’t get api token from powerpal today. It was disconnecting on the cert. So possibly they are blocking that mitm cert now. I tried on an Ipad and an android phone

On the other hand… you don’t actually need to generate an influxdb token, for the influxdb token. You would just add a token from powerpal as your host in influx…

So I managed to pull Powerpal data into HomeAssistant today after a bit of tinkering.
Shout out to some of the links already in this thread that helped me in the right direction.

Also other people have mentioned using mitmproxy and an Apple device to get your Auth Key which you will need. I tried on an Android device but couldnt get it to work due to certificate pinning. I tried reversing the APK to remove any SSL security stuff but proved to be just too hard. You’ll need to find an Apple device that’s at least iOS 10.

Anyway without further ado the sensor code,
*** Please alter the values marked with XXXX, or else this will not work ***
The first set of XXXXX is your serial number. Easiest way is look at the bluetooth device you’re connected to. It’ll be “Powerpal XXXXXXXX” that’s your serial number.

The Authorization code can be obtained using mitmproxy as above, this is not the same thing as your pairing code or any password you may have setup. It generated and only available from decrypting the app traffic

# Sensors for PowerPal
- platform: rest
  name: powerpal
  method: GET
  scan_interval: 60
  resource: https://readings.powerpal.net/api/v1/device/XXXXXXXX
  headers:
    authorization: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
    accept: application/json
    accept-encoding: br, gzip, deflate
    accept-language: en-au
    content-type: application/json
    user-agent: Powerpal/1912 CFNetwork/894 Darwin/17.4.0
  value_template: "OK"
  json_attributes:
    - total_watt_hours
    - last_reading_watt_hours

- platform: template
  sensors:
    powerpal_output_actual:
      friendly_name: "Powerpal Current Power Usage"
      value_template: "{{ states.sensor.powerpal.attributes.last_reading_watt_hours | int * 60 }}"
      unit_of_measurement: 'W'
      device_class: power
    powerpal_output_total:
      friendly_name: "Powerpal Total Power Usage"
      value_template: "{{ states.sensor.powerpal.attributes.total_watt_hours | float / 1000 }}"
      unit_of_measurement: 'kWh'
      device_class: energy

And this goes in your customize.yaml or configuration depending where you keep customizations
It is necessary to have the sensors show up in the new Energy part of HomeAssistant.

  customize_glob:
    sensor.powerpal_output_actual:
      state_class: measurement
    sensor.powerpal_output_total:
      state_class: total_increasing

Now for a little explanation. This calls the v1/device Powerpal API endpoint. It returns several JSON formatted values of which the last_reading and total are what we require.
The rest sensor does the work, but as the response is > 255 characters we generate a dummy response “OK” for the main state, and shove the other values we actually require into other entity attributes.

Now using HomeAssistant’s templating we create 2 more sensors that have to be massaged into the right format for Energy module to use.
I divide the usage total by 1000 to get kWh which is more useful, and required in the format.
For the current usage I multiply the last_reading by 60 to get an instantaneous power usage. A reading is capture by Powerpal every 1min, and given it’s in Watt/hours multiplying this by 60 gets us the usage in Watts.
It’s not 100% accurate but it’s good enough. I dabbled with getting results from the v1/meter_readings endpoint to retrieve multiple readings and averaging them but for the extra complexity it didnt provide much value.
This instantaneous value isn’t used by the Energy module that only uses totals, but I grabbed it as it’s handy to display current usage on a widget.

For the HA Energy module I used a fixed value for cost per kWh as mine is a fixed cost per kWh. Powerpal does return a last_reading_cost so in theory you should be able to grab that and use it to create another sensor for cost which can use to calculate cost. If many people need it I can take a look, although given the above template most could work it out I suspect. the JSON attr is “last_reading_cost” and if you * 60 you’ll get AU$ per hour of actual cost used (I think).

Can I also request if you’re using this please don’t hammer their API more than every 60s. As the official app only uploads every 60s there’s no point doing it more often, and you’ll probably just piss Powerpal off and this unofficial method will be shut down.

Happy to answer anything if I can about what I found tinkering with the APIs (yes you can upload your own garbage data if you really want to). The Meter Reading API is quirky and really isn’t of much use for HomeAssistant unless you’d like to know and store more accurate historical data.

Hope this helps someone.

5 Likes

The key bit I had to do (on iOS) after installing the mitmproxy cert that some guides miss is:
Go to Settings then General → About → Certificate Trust Settings → enable full trust mitmproxy root cert

2 Likes

Thanks! That was what I was missing. the full trust bit.
It does actually send if its peak or false, Ill have to dig into that sometime in the future. Peak is 3pm until 9pm, 7 days I think.

1 Like

Hi, giving your customisation a try. In Energy I see the options for tracking costs. I f I pick ‘Use a static price’ the value is enterd as EUR/kWH. How to make AUS or does it not matter?

You set currency under general settings.
Configuration > general > currency

So I found the first flaw, for some reason I got a 0 response which has screwed my Energy graphs
image

(you might be able to see the tiny blue bars that are the real usage before the giant bar)
I’m not 100% sure if it was because the request to the API failed or it really did return a 0 result, either way it has thrown the graphs off.

I’ll have to have a think about hot to cater for if it gets a 0 result back from the API either an actual 0 or a failed req.

Hmm mine has been working so far with Zero I think. I have zero output a lot because of solar

also can’t

  customize_glob:
    sensor.powerpal_output_actual:
      state_class: measurement
    sensor.powerpal_output_total:
      state_class: total_increasing

Just go here

powerpal_output_actual:
      friendly_name: "Powerpal Current Power Usage"
      value_template: "{{ state_attr('sensor.powerpal', 'last_reading_watt_hours') | int * 60 }}"
      unit_of_measurement: 'W'
      device_class: power
      state_class: measurement  //<<<<<<< HERE
    powerpal_output_total:
      friendly_name: "Powerpal Total Power Usage"
      value_template: "{{ state_attr('sensor.powerpal', 'total_watt_hours') | float / 1000 }}"
      unit_of_measurement: 'kWh'
      device_class: energy
      state_class: total_increasing //<<<<<<< HERE

thus avoiding the customize glob

I have been having a look at pybluez.
powerpal shows up in scanning. I haven’t figured out beyond that yet.

The problem is you can’t add a state_class to a template sensor, it throws an error.
Hence having to do it via a customize_glob. You can also do the device_class and unit_of_measurement in customize too if really desired but I find it cleaner to keep it together as much as possible.

I also have Solar so actual grid usage is zero quite a lot and that’s fine, becasue it’s tracking the Total Power Used from Powerpal. This was a case of Powerpal API returning 0 instead of 319.5 that is should’ve been

1 Like

I have edited my previous post with an update to the template I think should address the dropping to zero reading issue.

It’s worth noting using “states.sensor.powerpal.attributes” is not recommended practice you are supposed to use state_attr() as discussed here Templating - Home Assistant
But in this case it works in our favour to have unavailable returned rather than a zero.

1 Like

I updated my template with your changes and re-started HA. Seems al the previous data gets deleted on re-start - is that just for this integration? Not ideal.

After updating the template yesterday everything seemed to be working fine - new data was showing in the Energy graphs. But then this morning I see no data after 6pm yesterday. Looking in the HA logs I see the following:

2021-09-16 10:55:25 ERROR (MainThread) [homeassistant.components.rest.data] Error fetching data: https://readings.powerpal.net/api/v1/device/XXXXXXXX failed with
2021-09-16 10:55:25 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'mappingproxy object' has no attribute 'last_reading_watt_hours'') while processing template 'Template("{{ states.sensor.powerpal.attributes.last_reading_watt_hours | int * 60 }}")' for attribute '_attr_native_value' in entity 'sensor.powerpal_output_actual'
2021-09-16 10:55:25 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'mappingproxy object' has no attribute 'total_watt_hours'') while processing template 'Template("{{ states.sensor.powerpal.attributes.total_watt_hours | float / 1000 }}")' for attribute '_attr_native_value' in entity 'sensor.powerpal_output_total'

is there a way to do the mitmproxy bit on android? i do not have an apple phone. also any links to guides on doing this?
was about to build my own sensor using esphome but if i can get the data from my powerpal i will not bother.

I don’t think so or not simply is the answer I believe.
perhaps borrow a friends apple device? you only need it for a few minutes to get the api keys. Although I probably wouldn’t lend you mine lol.

yeah llooks like a real pain.
can it be installed on a windows pc? if so could you in theory run it on a windows pc, share the internet from the windows pc and have the phone connect to the internet via the pc and get the code from powerpal app that way?

Just a quick note - I have created an unofficial Home Assistant integration for Powerpal - which supports integration with energy dashboard - I have verified that its within a few cents daily of my energy provider (Amber) so pretty accurate. You do need a stable device (like an iPad) to be connected to the Powerpal in order to upload readings consistently.

@Know1 - just saw your post - and pretty much follows the same logic in your template

Feel free to contribute to the repository as needed.

4 Likes

This looks amazing.

Regarding the API auhorisation key - is this the one you have to get through mitmproxy?

Affirmative!

Thanks a lot mindmelting! I have up to date power consumption and I don’t have to muck around with pulse counters etc…

I used Charles proxy (trial is fine) for the token and id and the integration ran flawlessly.

installed this via HACS but it will not come up in HA integrations even after restarts and reinstalls.