You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I know this exists: #1831 but it is locked there for I am opening a new idea.
I do programming but not with nodeJS stuff so I asked o3 mini high (since I pay for it) what it can do and this is what it came up with, looks like a valid sollution to me but I quess the teslamate team can perhaps use any of this information:
(yes i hate it when I see chatGPT anwsers in github but i hope this sparks some live back into this subject and this stuff below can perhaps be used is some way or another in the future because this is a really usefull feature. I am now creating so much geofence places that it is getting out of hand haha)
New configuration option:
Add (for example in config/config.exs) an option such as:
config :teslamate, fetch_supercharger_cost: false
(Users can set this to true if they want auto–fetching.)
New API module:
Create a new module (for example, in lib/teslamate/api/supercharger_cost.ex) that calls Tesla’s (unofficial) endpoint. For instance:
defmodule TeslaMate.API.SuperchargerCost do
@moduledoc """
Retrieves actual supercharger fee data from the Tesla account API.
"""
require Logger
@endpoint "https://www.tesla.com/teslaaccount/charging/api/history"
def get_cost(vin, start_date, end_date) do
url = "#{@endpoint}?vin=#{vin}"
# Note: in a production implementation you’d need to handle authentication,
# e.g. by sending cookies or auth headers. For now we assume a simple GET.
case HTTPoison.get(url, [], recv_timeout: 10_000) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
case Jason.decode(body) do
{:ok, sessions} ->
find_matching_session(sessions, start_date, end_date)
{:error, err} ->
Logger.error("Failed to decode supercharger cost response: #{inspect(err)}")
nil
end
{:ok, %HTTPoison.Response{status_code: status}} ->
Logger.error("Unexpected status code #{status} from supercharger cost API")
nil
{:error, error} ->
Logger.error("HTTP error when calling supercharger cost API: #{inspect(error)}")
nil
end
end
defp find_matching_session(sessions, start_date, end_date) do
sessions
|> Enum.find(fn session ->
with {:ok, session_start, _} <- DateTime.from_iso8601(session["chargeStartDateTime"]),
{:ok, session_stop, _} <- DateTime.from_iso8601(session["chargeStopDateTime"]) do
abs(DateTime.diff(session_start, start_date)) < 300 and
abs(DateTime.diff(session_stop, end_date)) < 300
else
_ -> false
end
end)
|> case do
nil -> nil
session ->
fee = Enum.find(session["fees"] || [], fn fee -> fee["feeType"] == "CHARGING" end)
if fee, do: fee["totalBase"], else: nil
end
end
end
Integrate into charge finalization:
In the module that finalizes a charging session (for example in TeslaMate.Log.ChargingProcess or whichever module handles inserting/updating a completed charge), add a hook that, when a session finishes and its cost is still nil, checks if it qualifies as a supercharger session and, if so, calls our API module and then updates the record.
For example, add a helper function:
defmodule TeslaMate.Log.ChargingProcess do
alias TeslaMate.Repo
alias TeslaMate.Log.ChargingProcess
# Determine whether the session qualifies as a supercharger session.
defp qualifies_as_supercharger?(charging_process) do
case charging_process.address do
%{name: name} when is_binary(name) -> String.contains?(name, "Supercharger")
_ -> false
end
end
# Update cost if needed
def update_supercharger_cost(charging_process) do
if Application.get_env(:teslamate, :fetch_supercharger_cost, false) and
is_nil(charging_process.cost) and
qualifies_as_supercharger?(charging_process) do
vin = charging_process.car.vin
start_date = charging_process.start_date
end_date = charging_process.end_date
case TeslaMate.API.SuperchargerCost.get_cost(vin, start_date, end_date) do
nil ->
:noop
cost when is_number(cost) ->
changeset = ChargingProcess.changeset(charging_process, %{cost: cost})
case Repo.update(changeset) do
{:ok, _record} ->
Logger.info("Updated supercharger cost to #{cost} for session #{charging_process.id}")
{:error, err} ->
Logger.error("Failed to update supercharger cost: #{inspect(err)}")
end
end
else
:noop
end
end
end
Then, at the point in the charge finalization flow (for instance, after inserting the charging_process record) you would call:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I know this exists: #1831 but it is locked there for I am opening a new idea.
I do programming but not with nodeJS stuff so I asked o3 mini high (since I pay for it) what it can do and this is what it came up with, looks like a valid sollution to me but I quess the teslamate team can perhaps use any of this information:
(yes i hate it when I see chatGPT anwsers in github but i hope this sparks some live back into this subject and this stuff below can perhaps be used is some way or another in the future because this is a really usefull feature. I am now creating so much geofence places that it is getting out of hand haha)
Add (for example in config/config.exs) an option such as:
(Users can set this to true if they want auto–fetching.)
Create a new module (for example, in lib/teslamate/api/supercharger_cost.ex) that calls Tesla’s (unofficial) endpoint. For instance:
In the module that finalizes a charging session (for example in TeslaMate.Log.ChargingProcess or whichever module handles inserting/updating a completed charge), add a hook that, when a session finishes and its cost is still nil, checks if it qualifies as a supercharger session and, if so, calls our API module and then updates the record.
For example, add a helper function:
Then, at the point in the charge finalization flow (for instance, after inserting the charging_process record) you would call:
Beta Was this translation helpful? Give feedback.
All reactions