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

Handle sleep behavior of MCU2 upgraded cars #3262

Draft
wants to merge 28 commits into
base: master
Choose a base branch
from

Conversation

micves
Copy link
Contributor

@micves micves commented Jun 26, 2023

There is a lot more info in this issue: #3084

The short story is:
Older cars upgraded to MCU2 have a little wakeup every hour to check subsystems.
They report online but if vehicle_data is requested they wake up completely (cars clicks) and is awake for 15 minutes instead of a 2-3 minutes.
The only difference (known at this moment) is stream reports power=nil in subsystem online and reports power as a number when it is a real online.

Fix implemented by staying in state :start when online is detected on non-vehicle_data.
Stream receives data and new variable fake_online is set dependent on power is nil or a number.
The variable fake_online is used to decide whether to get only non-vehicle_data or the full blown wakeup vehicle_data
In suspended only non-vehicle_data is allowed to be better at going to sleep. Usage is detected if power>0 to go back to online state.

Other things:

  • Handle stream getting vehicle offline and change to state :offline in vehicle
  • Try to suspend in online when getting error on fetch_result
    This will lay of vehicle_data and use non-vehicle_data instead and
    hopefully not keep the car awake when errors suddenly stop again
  • I'm not quite into nice coding guidelines for Elixir, so some code-review might be good :)
  • I don't have possibility to test on other types of cars than my own 2017 Model S with upgraded MCU2.

micves and others added 6 commits May 27, 2023 22:45
…check subsystems.

They report online but if vehicle_data is requested they wake up completely (cars clicks) and is awake for 15 minutes instead of a 2-3 minutes.
The only difference (known at this moment) is stream reports power=nil in subsystem online and reports a power as a number when it is a real online.

Stay in state :start when online is detected on non-vehicle_data
stream receives data and new variable fake_online is set dependent on power is nil or a number

There are still some TODO's around the code.

Also handle stream getting vehicle offline and change to state offline in vehicle
…te is offline or asleep.

Before it caused a vehicle_data request in suspended even though not intended (since it actually went to state start where vehicle_data is allowed when false_online=false, which it is in online and suspended)

Added a clause in suspended if power > 0 to go back to online (e.g. if climate is turned on remotely with app.
That works, not sure if opening door without climate on is enough to trigger this (power seems to be integers, but hopefuly rounded up)
This will lay of vehicle_data and use non-vehicle_data instead and
hopefully not keep the car awake when errors suddenly stop again

Changed some comment and logger texts
Could otherwise give a irrelevant warning from try_to_suspend function. (E.g. User present)
@JakobLichterfeld JakobLichterfeld added area:tesla api Related to the Tesla API area:teslamate Related to TeslaMate core labels Nov 16, 2023
@oivindoh
Copy link
Contributor

This is Beautiful - I've been wondering about this for a couple of years now but never thought to dig more into why the MCU2 upgraded car behaved differently to the other MCU2 car in the account

@micves
Copy link
Contributor Author

micves commented Nov 19, 2023

Would it make sense to make different PR's to the things that are not directly related to the MCU2 issue?
Especially the first to points in the PR description/comment under "other things":

  • Handle stream getting vehicle offline and change to state :offline in vehicle
  • Try to suspend in online when getting error on fetch_result
    This will lay of vehicle_data and use non-vehicle_data instead and
    hopefully not keep the car awake when errors suddenly stop again

Just to make it easier to review this, but also for me to actually get CI tests working for both this MCU2 issue and the other things

@USAFPride
Copy link

Is this PR still active?

@micves
Copy link
Contributor Author

micves commented Nov 19, 2023

Is this PR still active?

Yes, it just fails some of the workflow tests, so I think I need to figure that out.

Besides that I once in a while merge teslamate:master in to keep the PR up-to-date

@USAFPride
Copy link

USAFPride commented Nov 24, 2023

Currently installing 2023.38.9. Will report back

Works fine with 2023.38.9

@oivindoh
Copy link
Contributor

I can’t see any runs on this PR - @JakobLichterfeld do we have any elixir gurus who could take a look at this one?

@JakobLichterfeld
Copy link
Collaborator

I can’t see any runs on this PR - @JakobLichterfeld do we have any elixir gurus who could take a look at this one?

Run needs manual approval. I didn't push the button till now since this branch seems outdated in regards to workflow files

@micves
Copy link
Contributor Author

micves commented Nov 26, 2023

I just synced the fork, but I know from last run that there are a lot of test failures.
I just set up an test environment next to my running environment to try to fix those failures.

Previously I used WSL on windows for test, but it seems to fail even on master.
I think there is some timing that doesn't match. And it's also insanely slow to test on WSL

Copy link

netlify bot commented Dec 7, 2023

Deploy Preview for teslamate ready!

Name Link
🔨 Latest commit cc75c21
🔍 Latest deploy log https://app.netlify.com/sites/teslamate/deploys/6649bdb79b39f90008f5da23
😎 Deploy Preview https://deploy-preview-3262--teslamate.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@USAFPride
Copy link

Can we get this merged into the master?

@JakobLichterfeld
Copy link
Collaborator

JakobLichterfeld commented Dec 19, 2023

Can we get this merged into the master?

Once our 321 test cases run successfully and we are sure that we are not breaking anything with non-MCU2 upgraded cars, the answer will be yes.

@JakobLichterfeld JakobLichterfeld added the note:needs investigation The issue must be investigated first label Dec 19, 2023
@JakobLichterfeld JakobLichterfeld marked this pull request as draft December 19, 2023 13:08
…e state :start.

Added fallback if no data is comming from stream and allow vehicle_data fetch
When in online dont detect as charging when power < 0 and preconditioning or dog mode
Don't allow suspend when power > 0
More aggresive suspend timer, only wait 1 minute after last use to suspend, stream should detect anything relevant
@czras
Copy link

czras commented Feb 20, 2024

I deployed: ghcr.io/teslamate-org/teslamate:pr-3262@sha256:aef423f3ff54fa3452d05b94c51bbd884fd425d3b867d8ec57ceef0be8b80176

Getting errors like this:

2024-02-20T10:57:40.761 app[4d89270a633287] otp [info] 2024-02-20 10:57:40.760 [info] GET /
2024-02-20T10:57:40.791 app[4d89270a633287] otp [info] 2024-02-20 10:57:40.790 [error] GenStateMachine :"1" terminating
2024-02-20T10:57:40.791 app[4d89270a633287] otp [info] ** (FunctionClauseError) no function clause matching in TeslaMate.Vehicles.Vehicle.Summary.format_vehicle/1
2024-02-20T10:57:40.791 app[4d89270a633287] otp [info] (teslamate 1.28.3) lib/teslamate/vehicles/vehicle/summary.ex:72: TeslaMate.Vehicles.Vehicle.Summary.format_vehicle(nil)
2024-02-20T10:57:40.791 app[4d89270a633287] otp [info] (teslamate 1.28.3) lib/teslamate/vehicles/vehicle/summary.ex:44: TeslaMate.Vehicles.Vehicle.Summary.into/2
2024-02-20T10:57:40.791 app[4d89270a633287] otp [info] (teslamate 1.28.3) lib/teslamate/vehicles/vehicle.ex:196: TeslaMate.Vehicles.Vehicle.handle_event/4
2024-02-20T10:57:40.791 app[4d89270a633287] otp [info] (stdlib 5.2) gen_statem.erl:1397: :gen_statem.loop_state_callback/11
2024-02-20T10:57:40.791 app[4d89270a633287] otp [info] (stdlib 5.2) proc_lib.erl:241: :proc_lib.init_p_do_apply/3
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] 2024-02-20 10:57:40.792 [error] Task #PID<0.2930.0> started from #PID<0.2924.0> terminating
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] ** (stop) exited in: :gen_statem.call(#PID<0.2911.0>, :summary, :infinity)
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] ** (EXIT) an exception was raised:
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] ** (FunctionClauseError) no function clause matching in TeslaMate.Vehicles.Vehicle.Summary.format_vehicle/1
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] (teslamate 1.28.3) lib/teslamate/vehicles/vehicle/summary.ex:72: TeslaMate.Vehicles.Vehicle.Summary.format_vehicle(nil)
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] (teslamate 1.28.3) lib/teslamate/vehicles/vehicle/summary.ex:44: TeslaMate.Vehicles.Vehicle.Summary.into/2
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] (teslamate 1.28.3) lib/teslamate/vehicles/vehicle.ex:196: TeslaMate.Vehicles.Vehicle.handle_event/4
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] (stdlib 5.2) gen_statem.erl:1397: :gen_statem.loop_state_callback/11
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] (stdlib 5.2) proc_lib.erl:241: :proc_lib.init_p_do_apply/3
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] (stdlib 5.2) gen.erl:246: :gen.do_call/4
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] (stdlib 5.2) gen_statem.erl:923: :gen_statem.call/3
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] (elixir 1.16.1) lib/task/supervised.ex:101: Task.Supervised.invoke_mfa/2
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] (elixir 1.16.1) lib/task/supervised.ex:36: Task.Supervised.reply/4
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] Function: &:erlang.apply/2
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] Args: [#Function<4.33331538/1 in TeslaMate.Vehicles.list/0>, [{TeslaMate.Vehicles.Vehicle_1, #PID<0.2911.0>, :worker, [TeslaMate.Vehicles.Vehicle]}]]
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] 2024-02-20 10:57:40.794 [error] Ranch protocol #PID<0.2924.0> of listener TeslaMateWeb.Endpoint.HTTP (connection #PID<0.2907.0>, stream id 2) terminated
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] exited in: :gen_statem.call(#PID<0.2911.0>, :summary, :infinity)
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] ** (EXIT) an exception was raised:
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] ** (FunctionClauseError) no function clause matching in TeslaMate.Vehicles.Vehicle.Summary.format_vehicle/1
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] (teslamate 1.28.3) lib/teslamate/vehicles/vehicle/summary.ex:72: TeslaMate.Vehicles.Vehicle.Summary.format_vehicle(nil)
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] (teslamate 1.28.3) lib/teslamate/vehicles/vehicle/summary.ex:44: TeslaMate.Vehicles.Vehicle.Summary.into/2
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] (teslamate 1.28.3) lib/teslamate/vehicles/vehicle.ex:196: TeslaMate.Vehicles.Vehicle.handle_event/4
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] (stdlib 5.2) gen_statem.erl:1397: :gen_statem.loop_state_callback/11
2024-02-20T10:57:40.795 app[4d89270a633287] otp [info] (stdlib 5.2) proc_lib.erl:241: :proc_lib.init_p_do_apply/3

Also trying to load the web interface responds with 500

@micves
Copy link
Contributor Author

micves commented Feb 20, 2024

@czras are you using docker-compose.yml with something like this:

services:
  teslamate:
    image: ghcr.io/teslamate-org/teslamate:pr-3262

I'm not using docker at the moment but a more manual install. But there is at least one that has pulled the latest from docker with no issues.

I can't really figure out what is going on based on the log output :(

Does it happen as soon as you start up or is it triggered by the GET ? That you try to access the web.

Can you post some more of the log? From startup maybe.

@czras
Copy link

czras commented Feb 21, 2024

@czras are you using docker-compose.yml with something like this:

services:
  teslamate:
    image: ghcr.io/teslamate-org/teslamate:pr-3262

I am using the image with the SHA ghcr.io/teslamate-org/teslamate@sha256:aef423f3ff54fa3452d05b94c51bbd884fd425d3b867d8ec57ceef0be8b80176 from the GithubAction.
It pulls the same image as ghcr.io/teslamate-org/teslamate:pr-3262.

Does it happen as soon as you start up or is it triggered by the GET ? That you try to access the web.

As far as I can judge it is happening right after start. Without accessing the web UI, the same error continues to pop up.

Can you post some more of the log? From startup maybe.

Certainly. Hopefully I can retest it this evening to gather all the logs.

…rev, commit date) and build date and print it at startup. Hopefully gives some good info on PR image builds
@brianmay
Copy link
Collaborator

My suspicion is that the build information commit may not work correctly when doing the Docker build. As in does the build have access to the git information? If it does work, then disregard :-)

@micves
Copy link
Contributor Author

micves commented Feb 22, 2024

I'm also anxious to see if it works :) The Build GHCR images do alot of git commands, but that might be in a different scope somehow.
And my docker environment doesnt really work right now, so can't even test myself :(

…in/erlexec: /lib/x86_64-linux-gnu/libc.so.6: version 'GLIBC_2.34' not found (required by /opt/app/erts-14.2.2/bin/erlexec)
@micves
Copy link
Contributor Author

micves commented Feb 22, 2024

I guess it didn't #3084 (comment)

@czras
Copy link

czras commented Mar 5, 2024

Certainly. Hopefully I can retest it this evening to gather all the logs.

Sorry, got the flu which knocked me out for a bit of time. I deployed now from a local docker build and this works fine. Altough I had to switch from netcat-traditional to netcat-openbsd for IPv6 support but that is a different story.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:tesla api Related to the Tesla API area:teslamate Related to TeslaMate core note:needs investigation The issue must be investigated first
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants