Tibber - Sensor for power consumption for electric car charging

With the new Energy feature in HA, it would be great to see how much power my electric car has consumed during charging. The Tibber app shows this information accumulated for the month. It would be great to have a sensor that displays the same information in HA. Or maybe it needs to be on daily basis to make sense in the Energy display.

Any comments @Danielhiversen ?

The data are not exposed by the Tibber api today.
You should send a feature request to Tibber.

Thank you. I will send a request today.

You get the data with the app API: http://app.tibber.com/

@JannePanne

Run


curl 'https://app.tibber.com/v1/login.credentials' \
  -H 'content-type: application/json' \
  --data-binary '{"email":"[email protected]","password":"XXXX"}'

Keep TOKEN. (This might expire)

Login to app.tibber.com:


{
  me {
		myVehicles{
      vehicles{
        id
        title
      }
    }
  }
}

Find the ID.

Replace ID and TOKEN:


sensor:
  - platform: rest
    name: "consumption"
    resource: https://app.tibber.com/v4/gql
    unit_of_measurement: "kWh"
    method: POST
    payload: '{"operationName":null,"variables":{},"query":"{\n  me {\n    electricVehicleConsumptionMonths(id: \"ID\") {\n   consumption\n      energyCost\n  }\n  }\n}\n"}'
    value_template: "{{ value_json.data.me.electricVehicleConsumptionMonths[-1].consumption}}"
    headers:
      User-Agent: 'Tibber/8.0.3 (com.tibber.tibber-swift-ios; build:2; iOS 14.7.1) Alamofire/5.4.2'
      Content-Type: application/json
      cookie: 'token=TOKEN'


  - platform: rest
    name: "cost"
    resource: https://app.tibber.com/v4/gql
    unit_of_measurement: "EUR"
    method: POST
    payload: '{"operationName":null,"variables":{},"query":"{\n  me {\n    electricVehicleConsumptionMonths(id: \"ID\") {\n   consumption\n      energyCost\n  }\n  }\n}\n"}'
    value_template: "{{ value_json.data.me.electricVehicleConsumptionMonths[-1].energyCost}}"
    headers:
      User-Agent: 'Tibber/8.0.3 (com.tibber.tibber-swift-ios; build:2; iOS 14.7.1) Alamofire/5.4.2'
      Content-Type: application/json
      cookie: 'token=TOKEN'
1 Like

is it possible to modify the sensors to show the daily/ weekly/ monthly consumption for the whole house? not just the car?

Hi Daniel
And thank you for the immense work you have done with the Tibber integration.

Edit; Because my kids can’t leave my phone alone, they posted an unfinished post. So here I try to finish it…

Can the integration as it is today show information related to Easee EV chargers, such as schedules or cost?

1 Like

This is a good integration for the Easee EV charger.

How did you find the available data? I’d rather be interested in the SoC (as this is the only somehow accessible API for Polestar other than their proprietary app…)

Hey John,
I do not have access to the API at the moment but it should be possible to also access the SoC, which @habitoti and many others, including myself would be interested in.

In this case the guys over at iobroker seem to be faster than us :slight_smile: Would you be so kind and check whether it’s possible to access the car SoC?

https://forum.iobroker.net/topic/67947/bastellösung-polestar-ladezustand-via-tibber-app-api/2

Thank you!

I implemented a simple SoC sensor for HA now (that’s all I need – probably can be easily extended for other data Tibber delivers). No Node Red required.
You just need a free Tibber account and configure your Polestar there. No contract required. Sensor refreshes every 5 mins, but you can choose any other scan_interval.

First, create a shell script getSoC.sh in HA config folder with this content and replace your Tibber credentials (email and password):

#! /bin/bash

AUTH=`(curl https://app.tibber.com/login.credentials -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"secret_pw"}')`   

TOKEN=`(echo $AUTH | grep -o '"token":"[^"]*' | grep -o '[^"]*$')`

SOCJSON=`curl https://app.tibber.com/v4/gql -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d '{ "query": "{ me { homes { electricVehicles { lastSeen battery { percent } } } }}"}'` 

SOC=`(echo $SOCJSON | grep -o '"percent":[[:digit:]]*' | grep -o '[[:digit:]]*')`

echo $SOC

Then add the following sensor to the configuration.yaml:

command_line:
  - sensor:
      name: BEV SoC
      command: "sh /config/getSoC.sh"
      unit_of_measurement: "%"
      scan_interval: 300
      device_class: battery
      value_template: "{{ value | int }}"

That’s all it takes…

11 Likes

I love it! The best present in a long time! Thank you!

@habitoti, thanks!
SoC is all I really need.

I had to remove the spaces at the end of some of the lines for the bash script to work. Took me some time to figure it out…

@ThomasMidgley Did you use the copy function of the code field? Should normally not be required to change anything…

Or even simpler:

secrets.yaml:

tibber: '{"email":"YourEMAIL","password":"YourPASSWORD"}'

config (as config package)

rest:
  - resource: https://app.tibber.com/login.credentials
    scan_interval: 21600 #6h
    timeout: 60
    method: POST
    payload: !secret tibber
    headers:
      Content-Type: "application/json"
    sensor:
      - name: "Tibber API Access Token"
        value_template: "Bearer {{ value_json.token }}"

  - resource: https://app.tibber.com/v4/gql
    method: POST
    payload: '{ "query": "{ me { homes { electricVehicles { lastSeen battery { percent }}}}}"}'
    headers:
      Authorization: "{{ states.sensor.tibber_api_access_token.state }}"
      Content-Type: "application/json"
    scan_interval: 600 #10m
    sensor:
      - name: "Polestar 2 SOC"
        unique_id: polestar2_soc
        value_template: "{{ value_json.data.me.homes[0].electricVehicles[0].battery.percent | int }}"
        unit_of_measurement: "%"
        icon: mdi:car-electric
        device_class: battery
      - name: "Polestar 2 Range"
        unique_id: polestar2_range
        value_template: "{{ (value_json.data.me.homes[0].electricVehicles[0].battery.percent / 100 * 487) | int }}"
        unit_of_measurement: "km"
        icon: mdi:car-electric
        device_class: distance
      - name: "Polestar 2 SOC Last Update"
        unique_id: polestar2_soc_lastupdate
        device_class: timestamp
        value_template: "{{ value_json.data.me.homes[0].electricVehicles[0].lastSeen | as_datetime | as_local }}"
2 Likes

I copied the ‘rest’ template code above into my sensors.yaml and get numerous “duplicate key” errors

What formatting should I be using given this is not in configuration.yaml but a separate file linked to by

sensor: !include sensors.yaml

I assume this is a damn YAML thing where the use of spaces or the - symbol is all important

Right I changed it too:

###############################
#                             #
#          SENSORS            #
#                             #
###############################
- platform: rest
  name: Polestar
  resource: https://app.tibber.com/login.credentials
  scan_interval: 21600 #6h
  timeout: 60
  method: POST
  payload: !secret tibber
  headers:
    Content-Type: "application/json"
- platform: template
  sensors:
    tibber_access_token:
      friendly_name: "Tibber API Access Token"
      value_template: "Bearer {{ value_json.token }}"
- platform: rest
  resource: https://app.tibber.com/v4/gql
  method: POST
  payload: '{ "query": "{ me { homes { electricVehicles { lastSeen battery { percent }}}}}"}'
  headers:
    Authorization: "{{ states.sensor.tibber_access_token.state }}"
    Content-Type: "application/json"
  scan_interval: 600 #10m
- platform: template
  sensors:
    polestar_soc:
      friendly_name: "Polestar 2 SOC"
      unique_id: polestar2_soc
      value_template: "{{ value_json.data.me.homes[0].electricVehicles[0].battery.percent | int }}"
      unit_of_measurement: "%"
      device_class: battery
    polestar_range:
      friendly_name: "Polestar 2 Range"
      unique_id: polestar2_range
      value_template: "{{ (value_json.data.me.homes[0].electricVehicles[0].battery.percent / 100 * 487) | int }}"
      unit_of_measurement: "km"
      device_class: battery
    polestar_last_update: 
      friendly_name: "Polestar 2 SOC Last Update"
      unique_id: polestar2_soc_lastupdate
      value_template: "{{ value_json.data.me.homes[0].electricVehicles[0].lastSeen }}"

Does this seem OK

The payload also looks incorrect, shouldn;t it be:

tibber: '{"email":"EMAIL","password":"PASSWORD"}'

noting the extra quote after the email address, and I am assuming that the uppercase values are replaced with actual account values?

OMG! This just worked out of the box. Now I have Polestar data. Thanks so much.

1 Like

I used the changed version and copied it to sensors.yaml.
Copied the tibber credentials to secrets.yaml.
But it does not work for me. The sensors are the and but they say unavailable.

Any suggestiosn to what I may have done wrong?

both combinations, when even actually the same, are not working for me,
any other tip?