Tesla charging on solar, optimization with Node-Red

Hi,

I’ve been looking into optimizing charging my Tesla at home and created a flow in Node-red like below in the screenshot.
I’m sure it can be optimized a bit further, so all tips or ideas are welcome :slight_smile:

To explain a bit:
Make sure you integrate the Tesla via the API first.

I’ll start with a check every 30 seconds if the Tesla is plugged in & charging state = ‘On’. If so, I’ll check if it’s located at Home (location_tracker).
If not, do nothing, if so, then I’ll start checking on the Solar power overage.
If it’s more then 250Wh, then I’ll check the current Amp it’s charging and increase it by 1. In this function I’ll also check if the Amp is not = to 20 (max that I can set in my Tesla App and to prevent that it’ll stop charging by the failsafe of the car, which happens if I let it go to 21). If Amps = 20, do nothing.
If it’s less then 250Wh, I’ll check current Amp it’s charging and decrease it by 1.
In this function I do the same, but for setting the minimum value (5 for now, I’m aware that via the API you can even go lower then 5).

Code for Amp ++

// Access the value from msg.payload.value
let currentValue = msg.payload || 0;
// Check if the current value is 20
if (currentValue !== 20) {
    // If it's not 20, increment by 1
    let newValue = currentValue + 1;
    // Update the payload with the new value
    msg.payload = newValue;
} else {
    // If it is 20, leave the value unchanged
    msg.payload = currentValue;
}
// Return the modified message
return msg;

For Amp – it’s the same, but change 20 to 5 and instead of + do a -

This will continuously make sure that my Charging Amps are optimized with the solar overage return at that moment.

As you can see I’ll also have a second line starting from the ‘Solar Returng > 250Wh?’ node.
If this is not the case I’ll start a check of the total Wh my solar panels are generating, for me I’ll had to create a Custom helper entity where I’ll summerize all the returns on each phase which I can read out (My P1 plug doesn’t have a entity for this, so I’ve created it myself).
I want this check to set the charger to off or on, based on my terms.
In the Switch node ‘Total Solar> 500Wh & < 1500’, I’ll have 2 terms, one for ‘In between 500 & 1500’ and another if < 500.
The first one will check if the Tesla switch.<car_name>.charger entity is in the state ‘Off’. If so it will put it back to on as my solare panels are generating between 500Wh & 1500Wh. If it’s still in ‘On’ state, it does nothing

For the second option (<500), I’ll check the current Battery percentage of the Tesla. If it’s under 35% (enough for my day by day car usage), then do nothing (keep charging at a minimum of 5A).
If over 35%, then stop charging.

In the very first node, in case of the Tesla charge state is ‘off’, I’ll go to the ‘Wh Returned’ node to set the charge state back to ‘on’ if all my conditions are fulfilled.

One thing I also do need to implement, which came to my mind in writing this, is to also check for the currunt power import. For example if it’s over 3000Wh, then maybe stop charging, even if the battery is under 35%? This just to make sure that my max quarter-hour-peak-usage stay’s below 4000Wh with all the other appliances in use.

Any other optimizations I could be missing?

1 Like

Why reinvent the wheel? Use https://evcc.io/ to charge your Tesla and let evcc take care of everything.

What’s the call service look like to update the charging amps (+1 or -1)?
I keep receiving an error whenever I try to call that service.

Should be number.carname_charging_amps :wink: