Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add LFP Battery setting for car settings #4036

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
- Customize intervals between vehicle_data calls (#3940 - @jlestel)
- feat: Create NixOS module (#3998 - @scottbot95)
- Update vehicle.ex with marketing name for Model Y Long Range RWD (#3989 - @stefanlange)
- feat: Add ability to enable and disable data collection for cars (#3993 - @mark3-dev)
- publish center_display_state to mqtt (#3813 - @Atrox)

### Improvements and bug fixes

Expand All @@ -31,6 +33,7 @@
- re-enable time picker by default in drive-details, charge-details dashboards (#3985)
- Improve "Charges" dashboard (#4031 - @jheredianet)
- Improve "Drives" dashboard (#4033 - @jheredianet)
- Update overview.json: Add Distance (#4034 - @DrMichael)

#### Translations

Expand Down
187 changes: 159 additions & 28 deletions grafana/dashboards/overview.json
Original file line number Diff line number Diff line change
Expand Up @@ -704,23 +704,25 @@
"type": "grafana-postgresql-datasource",
"uid": "TeslaMate"
},
"format": "time_series",
"format": "table",
"group": [],
"metricColumn": "none",
"rawQuery": true,
"rawSql": "SELECT $__time(date), range as \"range_$length_unit\"\nFROM (\n\t(SELECT date, convert_km([[preferred_range]]_battery_range_km, '$length_unit') AS range\n\tFROM positions\n\tWHERE car_id = $car_id AND [[preferred_range]]_battery_range_km IS NOT NULL\n ORDER BY date DESC\n\tLIMIT 1)\n\tUNION ALL\n\t(SELECT date, convert_km([[preferred_range]]_battery_range_km, '$length_unit') AS range\n\tFROM charges c\n\tJOIN charging_processes p ON p.id = c.charging_process_id\n\tWHERE p.car_id = $car_id\n\tORDER BY date DESC\n\tLIMIT 1)\n) AS data\nORDER BY date DESC\nLIMIT 1;",
"rawSql": "SELECT\n sum(GREATEST(start_[[preferred_range]]_range_km - end_[[preferred_range]]_range_km, 0) * car.efficiency * 1000) / \n convert_km(sum(distance)::numeric, '$length_unit') as \"consumption_$length_unit\"\nFROM drives\nJOIN cars car ON car.id = car_id\nWHERE $__timeFilter(start_date) AND car_id = $car_id",
"refId": "A",
"select": [
[
{
"params": [
"value"
"start_km"
],
"type": "column"
}
]
],
"timeColumn": "time",
"table": "drives",
"timeColumn": "start_date",
"timeColumnType": "timestamp",
"where": [
{
"name": "$__timeFilter",
Expand All @@ -730,7 +732,7 @@
]
}
],
"title": "Range",
"title": "Net",
"type": "stat"
},
{
Expand Down Expand Up @@ -813,7 +815,7 @@
"group": [],
"metricColumn": "none",
"rawQuery": true,
"rawSql": "SELECT\n sum(GREATEST(start_[[preferred_range]]_range_km - end_[[preferred_range]]_range_km, 0) * car.efficiency * 1000) / \n convert_km(sum(distance)::numeric, '$length_unit') as \"consumption_$length_unit\"\nFROM drives\nJOIN cars car ON car.id = car_id\nWHERE $__timeFilter(start_date) AND car_id = $car_id",
"rawSql": "WITH d AS (\n\tSELECT\n\t\tc.car_id,\n\t\tlag(end_[[preferred_range]]_range_km) OVER (ORDER BY start_date) - start_[[preferred_range]]_range_km AS range_loss,\n\t\tp.odometer - lag(p.odometer) OVER (ORDER BY start_date) AS distance\n\tFROM charging_processes c\n\tLEFT JOIN positions p ON p.id = c.position_id \n\tWHERE\n\t end_date IS NOT NULL AND\n\t c.car_id = $car_id AND\n\t $__timeFilter(start_date)\n\tORDER BY start_date\n),\n\nrange_loss_between_charges AS (\n SELECT sum(range_loss) AS range_loss\n FROM d\n WHERE distance >= 0 AND range_loss >= 0\n GROUP BY car_id\n),\n\ncharge_dates AS (\n\tSELECT\n\t\tmin(start_date) as first_charge,\n\t\tmax(end_date) as last_charge\n\tFROM\n\t\tcharging_processes\n\tWHERE\n\t\tend_date IS NOT NULL\n\t\tAND car_id = $car_id\n\t\tAND $__timeFilter(start_date)\n),\n\nrange_loss_before_first_charge AS (\n\tSELECT\n\t\tmax([[preferred_range]]_battery_range_km) - min([[preferred_range]]_battery_range_km) AS range_loss\n\tFROM positions, charge_dates\n\tWHERE\n\t\tcar_id = $car_id\n\t\tAND $__timeFilter(date)\n\t\tAND ((select first_charge from charge_dates) is null OR date < (select first_charge from charge_dates))\n),\n\nrange_loss_after_last_charge AS (\n\tSELECT\n\t\tmax([[preferred_range]]_battery_range_km) - min([[preferred_range]]_battery_range_km) AS range_loss\n\tFROM positions, charge_dates\n\tWHERE\n\t\tcar_id = $car_id\n\t\tAND $__timeFilter(date)\n\t\tAND date > (select last_charge from charge_dates)\t\n),\n\ntotal_range_loss AS (\n SELECT sum(range_loss) as range_loss\n FROM (\n SELECT range_loss FROM range_loss_between_charges\n UNION ALL\n SELECT range_loss FROM range_loss_before_first_charge\n UNION ALL\n SELECT range_loss FROM range_loss_after_last_charge\n ) r\n),\n\ndistance AS (\n SELECT max(odometer) - min(odometer) as distance\n FROM positions\n WHERE car_id = $car_id AND $__timeFilter(date)\n)\n\nSELECT \n NULLIF(range_loss, 0) * (c.efficiency * 1000) / convert_km(NULLIF(distance::numeric, 0), '$length_unit') as \"consumption_$length_unit\"\nFROM total_range_loss, distance\nLEFT JOIN cars c ON c.id = $car_id",
"refId": "A",
"select": [
[
Expand All @@ -837,7 +839,7 @@
]
}
],
"title": "Net",
"title": "Gross",
"type": "stat"
},
{
Expand All @@ -847,39 +849,47 @@
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"unit": "none"
},
"overrides": [
{
"matcher": {
"id": "byRegexp",
"options": ".*_km"
"id": "byName",
"options": "distance_km"
},
"properties": [
{
"id": "unit",
"value": "Wh/km"
"value": "km"
}
]
},
{
"matcher": {
"id": "byRegexp",
"options": ".*_mi"
"id": "byName",
"options": "distance_mi"
},
"properties": [
{
"id": "unit",
"value": "Wh/mi"
"value": "mi"
}
]
}
Expand All @@ -894,13 +904,13 @@
"id": 24,
"options": {
"colorMode": "value",
"graphMode": "none",
"graphMode": "area",
"justifyMode": "auto",
"orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": {
"calcs": [
"mean"
"lastNotNull"
],
"fields": "",
"values": false
Expand All @@ -916,25 +926,146 @@
"type": "grafana-postgresql-datasource",
"uid": "TeslaMate"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
"rawSql": "SELECT\r\n convert_km(sum(distance)::numeric, '$length_unit') AS distance_$length_unit\r\nFROM drives\r\nWHERE $__timeFilter(start_date) AND car_id = $car_id",
"refId": "A",
"sql": {
"columns": [
{
"parameters": [],
"type": "function"
}
],
"groupBy": [
{
"property": {
"type": "string"
},
"type": "groupBy"
}
],
"limit": 50
}
}
],
"title": "Distance",
"type": "stat"
},
{
"datasource": {
"type": "grafana-postgresql-datasource",
"uid": "TeslaMate"
},
"fieldConfig": {
"defaults": {
"decimals": 0,
"mappings": [
{
"options": {
"match": "null",
"result": {
"text": "N/A"
}
},
"type": "special"
}
],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "#c7d0d9",
"value": null
}
]
},
"unit": "none"
},
"overrides": [
{
"matcher": {
"id": "byName",
"options": "range_km"
},
"properties": [
{
"id": "unit",
"value": "lengthkm"
}
]
},
{
"matcher": {
"id": "byName",
"options": "range_mi"
},
"properties": [
{
"id": "unit",
"value": "lengthmi"
}
]
}
]
},
"gridPos": {
"h": 3,
"w": 3,
"x": 0,
"y": 8
},
"id": 14,
"maxDataPoints": 100,
"options": {
"colorMode": "value",
"fieldOptions": {
"calcs": [
"first"
]
},
"graphMode": "none",
"justifyMode": "auto",
"orientation": "horizontal",
"percentChangeColorMode": "standard",
"reduceOptions": {
"calcs": [
"mean"
],
"fields": "",
"values": false
},
"showPercentChange": false,
"textMode": "value",
"wideLayout": true
},
"pluginVersion": "11.1.0",
"targets": [
{
"datasource": {
"type": "grafana-postgresql-datasource",
"uid": "TeslaMate"
},
"format": "time_series",
"group": [],
"metricColumn": "none",
"rawQuery": true,
"rawSql": "WITH d AS (\n\tSELECT\n\t\tc.car_id,\n\t\tlag(end_[[preferred_range]]_range_km) OVER (ORDER BY start_date) - start_[[preferred_range]]_range_km AS range_loss,\n\t\tp.odometer - lag(p.odometer) OVER (ORDER BY start_date) AS distance\n\tFROM charging_processes c\n\tLEFT JOIN positions p ON p.id = c.position_id \n\tWHERE\n\t end_date IS NOT NULL AND\n\t c.car_id = $car_id AND\n\t $__timeFilter(start_date)\n\tORDER BY start_date\n),\n\nrange_loss_between_charges AS (\n SELECT sum(range_loss) AS range_loss\n FROM d\n WHERE distance >= 0 AND range_loss >= 0\n GROUP BY car_id\n),\n\ncharge_dates AS (\n\tSELECT\n\t\tmin(start_date) as first_charge,\n\t\tmax(end_date) as last_charge\n\tFROM\n\t\tcharging_processes\n\tWHERE\n\t\tend_date IS NOT NULL\n\t\tAND car_id = $car_id\n\t\tAND $__timeFilter(start_date)\n),\n\nrange_loss_before_first_charge AS (\n\tSELECT\n\t\tmax([[preferred_range]]_battery_range_km) - min([[preferred_range]]_battery_range_km) AS range_loss\n\tFROM positions, charge_dates\n\tWHERE\n\t\tcar_id = $car_id\n\t\tAND $__timeFilter(date)\n\t\tAND ((select first_charge from charge_dates) is null OR date < (select first_charge from charge_dates))\n),\n\nrange_loss_after_last_charge AS (\n\tSELECT\n\t\tmax([[preferred_range]]_battery_range_km) - min([[preferred_range]]_battery_range_km) AS range_loss\n\tFROM positions, charge_dates\n\tWHERE\n\t\tcar_id = $car_id\n\t\tAND $__timeFilter(date)\n\t\tAND date > (select last_charge from charge_dates)\t\n),\n\ntotal_range_loss AS (\n SELECT sum(range_loss) as range_loss\n FROM (\n SELECT range_loss FROM range_loss_between_charges\n UNION ALL\n SELECT range_loss FROM range_loss_before_first_charge\n UNION ALL\n SELECT range_loss FROM range_loss_after_last_charge\n ) r\n),\n\ndistance AS (\n SELECT max(odometer) - min(odometer) as distance\n FROM positions\n WHERE car_id = $car_id AND $__timeFilter(date)\n)\n\nSELECT \n NULLIF(range_loss, 0) * (c.efficiency * 1000) / convert_km(NULLIF(distance::numeric, 0), '$length_unit') as \"consumption_$length_unit\"\nFROM total_range_loss, distance\nLEFT JOIN cars c ON c.id = $car_id",
"rawSql": "SELECT $__time(date), range as \"range_$length_unit\"\nFROM (\n\t(SELECT date, convert_km([[preferred_range]]_battery_range_km, '$length_unit') AS range\n\tFROM positions\n\tWHERE car_id = $car_id AND [[preferred_range]]_battery_range_km IS NOT NULL\n ORDER BY date DESC\n\tLIMIT 1)\n\tUNION ALL\n\t(SELECT date, convert_km([[preferred_range]]_battery_range_km, '$length_unit') AS range\n\tFROM charges c\n\tJOIN charging_processes p ON p.id = c.charging_process_id\n\tWHERE p.car_id = $car_id\n\tORDER BY date DESC\n\tLIMIT 1)\n) AS data\nORDER BY date DESC\nLIMIT 1;",
"refId": "A",
"select": [
[
{
"params": [
"start_km"
"value"
],
"type": "column"
}
]
],
"table": "drives",
"timeColumn": "start_date",
"timeColumnType": "timestamp",
"timeColumn": "time",
"where": [
{
"name": "$__timeFilter",
Expand All @@ -944,7 +1075,7 @@
]
}
],
"title": "Gross",
"title": "Range",
"type": "stat"
},
{
Expand Down Expand Up @@ -983,8 +1114,8 @@
},
"gridPos": {
"h": 3,
"w": 4,
"x": 0,
"w": 3,
"x": 3,
"y": 8
},
"id": 2,
Expand Down Expand Up @@ -1105,8 +1236,8 @@
},
"gridPos": {
"h": 3,
"w": 5,
"x": 4,
"w": 3,
"x": 6,
"y": 8
},
"id": 6,
Expand Down Expand Up @@ -2030,6 +2161,6 @@
"timezone": "",
"title": "Overview",
"uid": "kOuP_Fggz",
"version": 8,
"version": 9,
"weekStart": ""
}
}
3 changes: 2 additions & 1 deletion lib/teslamate/import.ex
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ defmodule TeslaMate.Import do
settings = %CarSettings{
suspend_min: 0,
suspend_after_idle_min: 99999,
use_streaming_api: false
use_streaming_api: false,
enabled: true
}

%Car{car | settings: settings}
Expand Down
2 changes: 1 addition & 1 deletion lib/teslamate/mqtt/pubsub/vehicle_subscriber.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriber do
charger_actual_current charger_voltage version update_available update_version is_user_present
model trim_badging exterior_color wheel_type spoiler_type trunk_open frunk_open elevation power
charge_current_request charge_current_request_max tpms_pressure_fl tpms_pressure_fr tpms_pressure_rl tpms_pressure_rr
tpms_soft_warning_fl tpms_soft_warning_fr tpms_soft_warning_rl tpms_soft_warning_rr climate_keeper_mode
tpms_soft_warning_fl tpms_soft_warning_fr tpms_soft_warning_rl tpms_soft_warning_rr climate_keeper_mode center_display_state
)a

defp add_simple_values(map, %Summary{} = summary) do
Expand Down
12 changes: 11 additions & 1 deletion lib/teslamate/settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ defmodule TeslaMate.Settings do
alias TeslaMate.Repo

alias __MODULE__.{GlobalSettings, CarSettings}
alias TeslaMate.{Log, Locations}
alias TeslaMate.{Log, Locations, Vehicles}
alias TeslaMate.Log.Car
import Core.Dependency, only: [call: 2]

def get_global_settings! do
case Repo.all(GlobalSettings) do
Expand Down Expand Up @@ -46,6 +47,7 @@ defmodule TeslaMate.Settings do
def update_car_settings(%CarSettings{car: %Car{}} = pre, attrs) do
Repo.transaction(fn ->
with {:ok, post} <- pre |> CarSettings.changeset(attrs) |> Repo.update(),
:ok <- on_enabled_change(pre, post),
:ok <- broadcast(pre.car, post) do
post
else
Expand Down Expand Up @@ -84,6 +86,14 @@ defmodule TeslaMate.Settings do
Locations.refresh_addresses(lang)
end

def on_enabled_change(%CarSettings{enabled: preEnabled}, %CarSettings{enabled: postEnabled}) do
if preEnabled != postEnabled do
call(Vehicles, :restart)
end

:ok
end

defp broadcast(car, settings) do
Phoenix.PubSub.broadcast(TeslaMate.PubSub, topic(car), settings)
rescue
Expand Down
Loading
Loading