Pixometer integration

Update 01/2023: below manual solution became obsolete as I have now created a custom integration that facilitates this, see: GitHub - myTselection/pixometer: Home Assistant custom component HACS for Pixometer app meter reading integration.

Found some time to look into this, if anyone interested, see below. Feel free to suggest improvements, still trying out myself…

configuration.yaml:

sensor:
  - platform: rest
    name: Pixometer Token
    method: POST
    resource: https://pixometer.io/api/v1/access-token/
    payload: !secret pixometer_credentials
    scan_interval: 3600
    value_template: '{{"Bearer "+ value_json.access_token}}'
    headers:
      Content-Type: application/json
  - platform: rest
    name: Pixometer Electricity
    resource: https://pixometer.io/api/v1/readings/?meter_id=XXXXXXXX&o=-created
    headers:
        Authorization: '{{states("sensor.pixometer_token")}}'
    json_attributes:
      - results
    value_template: "{{ value_json.results[0].value }}"
    device_class: energy
    unit_of_measurement: "kWh"


utility_meter:
  energy:
    source: sensor.pixometer_electricity
    cycle: daily
    net_consumption: true

Updated my post to automatically fetch the access token with a separate rest sensor ‘Pixometer Token’, as this access token has limited time to live.