Make Your Car Charger Smart

I recently changed electric company from an old fashioned one to the "smart supplier" Tibber. The cool thing with Tibber -- except from that they deliver green electricity -- is that they charge by the hour and they have an API. This means that with some smart equipment, it is possible to plan your electric consumption. In my example, I make an automation to charge my electric car when the price is below average.

As I already had a "stupid" car charger, I converted it to a smart one. I already have Home Assistant running on a Raspberry Pi, so I only had to invest in a Sonoff smart switch. But even if you need to buy a RPi, this will still be a cheap solution -- and fun to build!
What you need:
  • A Tibber subscription
    If you like this guide, please use my invite discount code ab26409e and we both get SEK 500 to shop for in the Tibber Store
  • A Raspberry Pi
  • A Sonoff TH16
    Please note that Sonoff TH16 can handle max 16A continuously. If you have a stronger charger, Shelly EM could be an option

Install Home Assistant

If you don't already have Home Assistant installed on your Raspberry Pi, head to https://www.home-assistant.io/ and follow the installation instructions for your hardware.

Install the Sonoff

UPDATE: Don't use Sonoff for this hack! Mine got overheated. Instead, I recommend using the Aeotec Heavy Duty Switch from Tibber Store. See my next post.

Disclaimer: I don't know about regulations in other countries, but in Sweden, you are not allowed to install fixed electric equipment if you don't have the right certification.

I installed my Sonoff in the fuse box. There is a DIN rail hold available for Sonoff to make a neat installation. But you can also install the switch stand-alone or, if you have room for it, inside the charger. Just remember that you need WiFi coverage for it to work.

Register the switch in the eWeLink app (calling it for example "Charger") and make sure it works as supposed when turning it on and off.

Configure IFTTT

In this example, we will use IFTTT and the eWeLink REST API for integration with Home Assistant. Log in to IFTTT and click Create. Create a new Webhook with the following settings:
  • Receive a web request
  • Give it an event name (for example car_charger_on)
  • Action link eWeLink Smart Home
  • Turn 1 Channel Switch on or off
  • Select your switch (for example Charger) and On
  • Finish
  • Repeat the same steps to create an Off applet
You also need your IFTTT maker key. It is available at https://ifttt.com/maker_webhooks/settings and is the string at the end of the url. 

Add Tibber in Home Assistant

To add the Tibber integration in Home Assistant, simply follow the guide at https://www.home-assistant.io/components/tibber/

Add the events in Home Assistant

Now you need to hook the IFTTT events up with Home Assistant. To do this, you add the following lines to the end of your configuration.yaml, where <maker key> is the key from the Configure IFTTT step.

rest_command:
  turn_on_charger:
    url: https://maker.ifttt.com/trigger/car_charger_on/with/key/<maker key>
  turn_off_charger:
    url: https://maker.ifttt.com/trigger/car_charger_off/with/key/<maker key>

Don't forget that the indentation (number of spaces in the beginning of the line) is important in yaml files!

Set up the automation in Home Assistant

Add the following automation to automations.yaml. To find the name of your electricity price sensor, you can for example open States in Developer Tools in the left hand menu. 

- id: '1562442939112'
  alias: Start charging when price is low
  trigger:
  - minutes: '1'
    platform: time_pattern
  condition:
  - condition: template
    value_template: '{{ float(states("sensor.electricity_price_sensor_name"))
      <= float(state_attr("sensor.electricity_price_sensor_name", "avg_price"))
      }}'
  action:
  - service: rest_command.turn_on_charger
- id: '1562442939112'
  alias: Stop charging when price is high
  trigger:
  - minutes: '1'
    platform: time_pattern
  condition:
  - condition: template
    value_template: '{{ float(states("sensor.electricity_price_sensor_name"))
      > float(state_attr("sensor.electricity_price_sensor_name", "avg_price"))
      }}'
  action:
  - service: rest_command.turn_off_charger

You can of course also use the automation editor in Home Assistant.

After this is done, don't forget to restart Home Assistant.

DONE!

But... What if I need to turn on the charger?

If you want to turn on the charger even though the price is high, you can for example connect your Google Home to eWeLink and ask Google Assistant to turn it on for you. Of course, the charger will turn off again at the next full hour if the price still is high, so you also need to disable the IFTTT web hook. It's probably quite easy to make automate this better, but I haven't yet needed it.

Comments