Tibber - Sensor for power consumption for electric car charging

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?

Hi Christian,
did you find out what was your issue?

best regards.

Screenshot your config files and blur the password, so i can help.

here you go, and thx in advance.

I’ve tested the scriped version and it works, but would prefer to get the sensors.yaml version to run.

Its a config package and not something for the sensors.yaml…

1 Like

I see a lot of confusion around sensors.yaml. May I just state something bold? Every user should implement packages in their configuration. I think it is a mistake that a packages setup is not part of the standard configuration provided with HA.

homeassistant:
  packages: !include_dir_named packages

Some could see this as personal preference but it really isn’t. Packages can be used to have one sensors heavy file, one automations centered file… if the user wanted to.

@lwde thanks for your snippet! This makes everything so much easier. I’m hopefully soon in a position to test and give feedback.

2 Likes

thank you…
took a while till the “sensors” updated themselves… but it works…
thank you also to bringing up the “packages” thats totally new to me… something new to figure out in HA.

BR and greetings

Thanks for sharing, works like a charm. I changed device_class to distance for polestar2_range.

But that value isn’t really helpful, anyways. Where does the number 487 for the calculation SoC%/100*487 come from?

I really hope Polestar is listening and finally opens up their API, at least a little bit for the most basic information…