Hi,
the MG ZS EV doesn't have a brilliant dashboard when it comes to wanting to conserve range by driving efficiently, you only know the power as a percentage of maximum.
This you might want to stay within 3.5mi/kWh, so should you slow to 60mph at 15% power, or can you risk driving 65mph at 20% power?
And thus I wrote a plugin, find it in my fork, at https://github.com/speculatrix/Open-Vehicle-Monitoring-System-3/tree/master/plugin/efficiency
But I don't properly understand how to get the values and do the maths. Can anyone help please?
thanks
The metric widgets subscribe to metric changes through the "data-metric" attribute. As there are no metrics "miles_per_kwh" or "kwh_per_km", your widgets won't get any updates. Your calculation for the miles_per_kwh chart depends on metrics "v.p.speed" and "v.b.power", so you should set data-metric for that widget to "v.p.speed,v.b.power".
You should avoid polluting the framework metrics object with custom data, in this case your calculation result. That won't work as expected and will confuse others. If you really want to add custom metrics created on the web UI (thus only available in that web session), place them in the custom metrics namespace for the vehicle (e.g. "xmg") and send them as a metrics update event, so all receivers can get them. But in this case, that's totally unnecessary, simply set the result directly via setData().
On your calculation: a) metric "v.b.consumption" is provided by the framework as a standard metric, defined as power by speed (Wh/km), smoothed over 5 samples, so should be a better option to base your chart on. It's used for the chart in the upper right of the default dashboard. b) Metric values are always in their default units in the metrics object. If you want miles, you need to convert the values.
Regards,
Michael
Hi Dexter, I realised I copied and pasted the wrong metric, v.s or v.b, probaly fixed now and committed to my branch.
Also, having been writing Python, my javascript was missing brackets in the conditional statements!
I've not tested it but it doesn't crash out so it might work.
What are the units for speed? Or, how do I know whether it's returning speed in mi/h or km/hr?
thanks
Paul
Metric values are always in their default units in the metrics object. If you want miles, you need to convert the values.
You can look up metric default units in the documentation or by "metric list".
Regards,
Michael
I managed to test out the plugin but found that the dials didn't animate at all. My guess is that some jscript "plumbing" is missing to get them to update at regular intervals?
I tried on both my phone and a laptop (running chrome on linux). I tried refreshing the web page and didn't see much.
I also tried the main dashboard and the dials didn't change very regularly either though.
Testing is tricky because I need a co-pilot, and the edit-upload-debug cycle isn't instant.
sorry if these questions seem dumb, but I have never tried to do anything like this before so I am just trying to intelligently copy and paste bits of jscript and html from the other examples (I used regenmon) n hope I can stumble on to make it work. please feel free to take or leave any part of whatever I have written and make a working version. thanks! I do want to learn though.
You didn't follow my first hint:
That's the only thing missing from your plugin to make it work.
Also, adding a test data generator is simple and allows you to test it without driving.
Example for your metrics:
Regards,
Michael
Hi, that's great, thanks, it seems to work, I pushed the code to my branch.
Shall I raise a pull request to submit it to master?
Sure.
The units configuration is stored in config vehicle units.distance. To retrieve it from the web UI, you can e.g. do a loadjs() on OvmsConfig.Get():
You'll get "K" for kilometers and "M" for miles. Instead of logging the output you should copy it to a variable or pass it to an init function.
Regards,
Michael
May I ask one stupid question about metrics and geting them through REST api?
I'm getting all sorts of metrics from command <metrics list> which is great. But I also get a lot of metrics from REST api f.e. (status, charging, location, etc.) but specifically I'm looking for consumption. In my version it's:
So, the question is how can I get this consumption info to REST api, so I can use it in f.e. Home assistant. Other metrics from REST api are working fine...I'm getting info I need, but consumption could be useful.
Thank you!
The V2 protocol and server does not know about new V3 and custom metrics. If you want to tunnel these through a V2 server, you need to use "historical" records, which can be sent from the module as "data" notifications, e.g. by a script.
Template:
The REST API call returns all records stored within the expiry period with their respective times in "h_timestamp", most recent = last entry.
To reduce data volume and speed up the query, add a reference timestamp as the "since" parameter to the API call.
Regards,
Michael
Correction: the REST API call is "GET /api/historical/<VEHICLEID>/<DATATYPE>" and does not support the "since" parameter.
Michael,
thank you very much for this explanation. It helps a lot! I'll try...
Have a nice day!
I've added the example/template and some more hints to the documentation:
https://docs.openvehicles.com/en/latest/userguide/metrics.html#tunnel-through-v2-server
Regards,
Michael
Thanks Michael again for help and template for script! I don't know much about programming but have some basic knowledge...do I see correctly that it send (tunnels) data with script only if the car in ON?
I have been trying to use this script in web UI - Editor. I did copy/paste your template into the /store/scripts directory, evaluated it by pressing Evaluate JS button on the bottom and I got
when I press Reload JS Engine I get:
Reloading javascript engine
Then I tested it with downloadtool as advised in your fresh "tunnel guide" and I got historical cvs file only with:
rec_time,rec_nr
So, is it possible that it only works (tunnels data) if car in ON?
If I run
Something came up after all from historical REST call:
[{"distinctrecs":63,"first":"2023-01-10 06:05:06","h_recordtype":"*-Log-Notification","last":"2023-01-13 15:22:29","totalrecs":63,"totalsize":"6125"},{"distinctrecs":1,"first":"2023-01-07 19:10:54","h_recordtype":"*-OVM-DebugCrash","last":"2023-01-11 13:29:57","totalrecs":3,"totalsize":"788"},{"distinctrecs":5409,"first":"2023-01-11 20:02:25","h_recordtype":"*-OVM-ServerLogs","last":"2023-01-13 19:56:59","totalrecs":5615,"totalsize":"767874"},{"distinctrecs":4,"first":"2023-01-13 00:00:00","h_recordtype":"*-OVM-Utilisation","last":"2023-01-13 00:00:00","totalrecs":4,"totalsize":"188"},{"distinctrecs":1,"first":"2023-01-11 20:02:25","h_recordtype":"D","last":"2023-01-13 19:56:59","totalrecs":1479,"totalsize":"128206"},{"distinctrecs":1,"first":"2023-01-12 05:20:16","h_recordtype":"F","last":"2023-01-13 18:35:50","totalrecs":91,"totalsize":"16769"},{"distinctrecs":1,"first":"2023-01-11 20:02:25","h_recordtype":"L","last":"2023-01-13 19:56:59","totalrecs":1783,"totalsize":"185281"},{"distinctrecs":1,"first":"2023-01-11 20:02:25","h_recordtype":"S","last":"2023-01-13 19:56:59","totalrecs":1483,"totalsize":"233896"},{"distinctrecs":1,"first":"2023-01-12 05:20:16","h_recordtype":"W","last":"2023-01-13 18:35:50","totalrecs":81,"totalsize":"4516"},{"distinctrecs":1,"first":"2023-01-13 19:36:52","h_recordtype":"XKN-LOG-Consumption","last":"2023-01-13 19:36:52","totalrecs":1,"totalsize":"147"},{"distinctrecs":1,"first":"2023-01-12 05:20:16","h_recordtype":"Y","last":"2023-01-13 18:35:50","totalrecs":81,"totalsize":"7899"}]
So this is recordtype...how can I get metrics?
And I would also ask for some help regarding this in template:
is there any example to show how the line should look like?
Thank you!!
OK, I got more data from RESTapi by calling:
/api/historical/<vehicle-ID>/*-Log-Notification/
I got:
[{"h_data":"I,Trip 20.0km Avg 28km/h Alt -12m\rEnergy 125Wh/km; 26% recd\rSOC -2.0% = 41.5%\rRange -9.4km = 188.0km\r","h_recordnumber":20670,"h_timestamp":"2023-01-10 06:05:06"},{"h_data":"I,Trip 21.0km Avg 32km/h Alt -5m\rEnergy 129Wh/km; 25% recd\rSOC +0.0% = 41.5%\rRange -0.2km = 187.8km\r","h_recordnumber":20696,"h_timestamp":"2023-01-10 06:10:19"},{"h_data":"I,Trip 23.0km Avg 34km/h Alt +11m\rEnergy 130Wh/km; 23% recd\rSOC +0.0% = 41.0%\rRange -0.2km = 185.3km\r","h_recordnumber":20721,"h_timestamp":"2023-01-10 06:18:48"},{"h_data":"A,12V Battery critical: 12.8V (ref=14.6V)","h_recordnumber":21357,"h_timestamp":"2023-01-10 08:19:47"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":21582,"h_timestamp":"2023-01-10 08:52:49"},{"h_data":"A,12V Battery critical: 12.8V (ref=14.6V)","h_recordnumber":21757,"h_timestamp":"2023-01-10 09:24:59"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":21922,"h_timestamp":"2023-01-10 09:57:10"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":22295,"h_timestamp":"2023-01-10 10:29:19"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":23608,"h_timestamp":"2023-01-10 11:01:29"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":24106,"h_timestamp":"2023-01-10 11:33:40"},{"h_data":"A,12V Battery critical: 12.8V (ref=14.6V)","h_recordnumber":24296,"h_timestamp":"2023-01-10 12:05:52"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":24485,"h_timestamp":"2023-01-10 12:38:00"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":24677,"h_timestamp":"2023-01-10 13:10:10"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":24852,"h_timestamp":"2023-01-10 13:42:22"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":25056,"h_timestamp":"2023-01-10 14:14:31"},{"h_data":"A,12V Battery critical: 13.0V (ref=14.6V)","h_recordnumber":25590,"h_timestamp":"2023-01-10 15:50:04"},{"h_data":"A,12V Battery restored: 14.4V (ref=14.6V)","h_recordnumber":25591,"h_timestamp":"2023-01-10 15:50:04"},{"h_data":"A,12V Battery critical: 12.2V (ref=14.6V)","h_recordnumber":25592,"h_timestamp":"2023-01-10 15:50:04"},{"h_data":"A,12V Battery restored: 14.0V (ref=14.6V)","h_recordnumber":25593,"h_timestamp":"2023-01-10 15:50:04"},{"h_data":"A,12V Battery critical: 12.4V (ref=14.6V)","h_recordnumber":25594,"h_timestamp":"2023-01-10 15:50:04"},{"h_data":"A,12V Battery restored: 14.0V (ref=14.6V)","h_recordnumber":25595,"h_timestamp":"2023-01-10 15:50:04"},{"h_data":"I,Trip 23.0km Avg 28km/h Alt +0m\rEnergy 22Wh/km; 79% recd\rSOC -1.5% = 64.5%\rRange -6.3km = 294.9km\r","h_recordnumber":25598,"h_timestamp":"2023-01-10 15:50:28"},{"h_data":"A,12V Battery critical: 12.4V (ref=14.6V)","h_recordnumber":25599,"h_timestamp":"2023-01-10 15:50:33"},{"h_data":"A,12V Battery restored: 13.9V (ref=14.6V)","h_recordnumber":25608,"h_timestamp":"2023-01-10 15:52:33"},{"h_data":"I,Trip 23.0km Avg 0km/h Alt +0m\rEnergy 22Wh/km; 79% recd\rSOC +0.0% = 64.5%\rRange +0.3km = 295.3km\r","h_recordnumber":25612,"h_timestamp":"2023-01-10 15:53:15"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":26238,"h_timestamp":"2023-01-10 17:52:31"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":26429,"h_timestamp":"2023-01-10 18:24:41"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":26595,"h_timestamp":"2023-01-10 18:56:51"},{"h_data":"A,12V Battery critical: 12.8V (ref=14.6V)","h_recordnumber":26616,"h_timestamp":"2023-01-10 19:00:51"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":26803,"h_timestamp":"2023-01-10 19:33:52"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":26850,"h_timestamp":"2023-01-10 20:06:02"},{"h_data":"A,12V Battery critical: 12.4V (ref=14.6V)","h_recordnumber":26910,"h_timestamp":"2023-01-10 20:38:12"},{"h_data":"A,12V Battery critical: 12.5V (ref=14.6V)","h_recordnumber":26970,"h_timestamp":"2023-01-10 21:10:23"},{"h_data":"A,12V Battery critical: 12.4V (ref=14.6V)","h_recordnumber":27029,"h_timestamp":"2023-01-10 21:42:33"},{"h_data":"A,12V Battery critical: 12.4V (ref=14.6V)","h_recordnumber":27070,"h_timestamp":"2023-01-10 22:14:43"},{"h_data":"A,12V Battery critical: 12.4V (ref=14.6V)","h_recordnumber":27115,"h_timestamp":"2023-01-10 22:46:53"},{"h_data":"A,12V Battery critical: 12.4V (ref=14.6V)","h_recordnumber":27178,"h_timestamp":"2023-01-10 23:19:03"},{"h_data":"A,12V Battery critical: 12.3V (ref=14.6V)","h_recordnumber":27232,"h_timestamp":"2023-01-10 23:51:13"},{"h_data":"A,12V Battery critical: 12.3V (ref=14.6V)","h_recordnumber":27269,"h_timestamp":"2023-01-11 00:23:23"},{"h_data":"A,12V Battery critical: 12.3V (ref=14.6V)","h_recordnumber":27311,"h_timestamp":"2023-01-11 00:55:33"},{"h_data":"A,12V Battery critical: 12.3V (ref=14.6V)","h_recordnumber":27358,"h_timestamp":"2023-01-11 01:27:43"},{"h_data":"A,12V Battery critical: 12.2V (ref=14.6V)","h_recordnumber":27515,"h_timestamp":"2023-01-11 01:59:54"},{"h_data":"A,12V Battery critical: 12.2V (ref=14.6V)","h_recordnumber":27560,"h_timestamp":"2023-01-11 02:32:03"},{"h_data":"A,12V Battery critical: 12.2V (ref=14.6V)","h_recordnumber":27593,"h_timestamp":"2023-01-11 03:04:14"},{"h_data":"A,12V Battery critical: 12.2V (ref=14.6V)","h_recordnumber":27623,"h_timestamp":"2023-01-11 03:36:27"},{"h_data":"A,12V Battery critical: 12.1V (ref=14.6V)","h_recordnumber":27655,"h_timestamp":"2023-01-11 04:08:34"},{"h_data":"A,12V Battery critical: 12.1V (ref=14.6V)","h_recordnumber":27689,"h_timestamp":"2023-01-11 04:40:44"},{"h_data":"A,12V Battery critical: 12.1V (ref=14.6V)","h_recordnumber":27763,"h_timestamp":"2023-01-11 05:12:55"},{"h_data":"A,12V Battery critical: 12.1V (ref=14.6V)","h_recordnumber":27881,"h_timestamp":"2023-01-11 05:45:05"},{"h_data":"A,12V Battery restored: 14.3V (ref=14.6V)","h_recordnumber":27949,"h_timestamp":"2023-01-11 06:06:05"},{"h_data":"A,12V Battery critical: 12.4V (ref=14.6V)","h_recordnumber":27992,"h_timestamp":"2023-01-11 06:17:49"},{"h_data":"A,12V Battery restored: 14.4V (ref=14.6V)","h_recordnumber":27993,"h_timestamp":"2023-01-11 06:17:49"},{"h_data":"A,12V Battery critical: 12.9V (ref=14.6V)","h_recordnumber":27994,"h_timestamp":"2023-01-11 06:18:05"},{"h_data":"A,12V Battery restored: 12.0V (ref=12.6V)","h_recordnumber":28052,"h_timestamp":"2023-01-11 06:32:05"},{"h_data":"I,Trip 20.0km Avg 33km/h Alt -11m\rEnergy 85Wh/km; 43% recd\rSOC -2.0% = 71.0%\rRange -9.0km = 322.7km\r","h_recordnumber":880,"h_timestamp":"2023-01-11 15:11:56"},{"h_data":"I,Trip 21.0km Avg 28km/h Alt -17m\rEnergy 95Wh/km; 41% recd\rSOC -0.5% = 69.5%\rRange -2.7km = 315.4km\r","h_recordnumber":3324,"h_timestamp":"2023-01-12 06:16:47"},{"h_data":"I,Trip 29.0km Avg 36km/h Alt +16m\rEnergy 107Wh/km; 37% recd\rSOC -2.5% = 67.0%\rRange -12.1km = 303.3km\r","h_recordnumber":3367,"h_timestamp":"2023-01-12 06:38:27"},{"h_data":"I,Trip 37.0km Avg 33km/h Alt -19m\rEnergy 108Wh/km; 37% recd\rSOC -2.0% = 64.0%\rRange -9.8km = 289.0km\r","h_recordnumber":4001,"h_timestamp":"2023-01-12 15:03:45"},{"h_data":"I,Trip 45.0km Avg 36km/h Alt +20m\rEnergy 104Wh/km; 36% recd\rSOC -1.5% = 62.5%\rRange -7.2km = 281.8km\r","h_recordnumber":4028,"h_timestamp":"2023-01-12 15:20:09"},{"h_data":"I,Trip 54.0km Avg 37km/h Alt -20m\rEnergy 104Wh/km; 36% recd\rSOC -2.0% = 60.5%\rRange -9.6km = 272.2km\r","h_recordnumber":4100,"h_timestamp":"2023-01-12 16:05:05"},{"h_data":"I,Trip 69.0km Avg 30km/h Alt +9m\rEnergy 120Wh/km; 33% recd\rSOC -6.0% = 54.5%\rRange -29.9km = 242.3km\r","h_recordnumber":10343,"h_timestamp":"2023-01-13 06:40:09"},{"h_data":"I,Trip 77.0km Avg 37km/h Alt -30m\rEnergy 117Wh/km; 34% recd\rSOC -2.0% = 52.5%\rRange -10.2km = 232.1km\r","h_recordnumber":25561,"h_timestamp":"2023-01-13 15:12:47"},{"h_data":"I,Trip 78.0km Avg 27km/h Alt +3m\rEnergy 119Wh/km; 33% recd\rSOC -0.5% = 52.0%\rRange -3.9km = 228.2km\r","h_recordnumber":25707,"h_timestamp":"2023-01-13 15:22:29"}]
Is there any other way I get only last record data?
And also: in Home Assistant I get via REST the first record (the oldest) and not the latest...like this:
h_data: I,Trip 20.0km Avg 28km/h Alt -12m Energy 125Wh/km; 26% recd SOC -2.0% = 41.5% Range -9.4km = 188.0km h_timestamp: 2023-01-10 06:05:06 friendly_name: ev_consumption
Again, Michael, thank you very much!!!
You should be careful with posting log data on a public forum.
"(OK, no output)" is exactly what's expected from a script run, as it does not do any output, it only sends data to the server.
"script run 10" tells me you saved it as "10", which cannot work. It's Javascript, so needs to have the ".js" extension. If you want to call it manually, save it e.g. as "/store/scripts/v2data.js". Then, to run it, issue "script run v2data.js" or the short form ". v2data.js".
Also, if you want it to send data while parking / charging, you need to comment out or change the condition:
To bypass that check, simply prefix the "if" with "//". Be aware it will then send data on every run, i.e. if bound to the 60 second ticker, you will get 1.440 records per day. I suggest to only disable the check for an initial test, to have some data on the server.
On retrieving: https://docs.openvehicles.com/en/latest/protocol_httpapi/requests.html#get-api-historical-vehicleid
The script sends records for…
…so you'll query those by: GET /api/historical/<vehicleid>/XKN-LOG-Consumption
The result will look similar to your result for "*-LOG-Notification", but with the JSON payload in "h_data". From the example/template, that looks like this:
[{"h_data":"{\"v.b.consumption\":0,\"xvu.b.energy.range\":22.7,\"xvu.b.soh.range\":94.0077}","h_recordnumber":0,"h_timestamp":"2023-01-13 09:39:27"},{"h_data":"{\"v.b.consumption\":0,\"xvu.b.energy.range\":22.7,\"xvu.b.soh.range\":94.0077}","h_recordnumber":0,"h_timestamp":"2023-01-13 09:52:12"}]
I cannot really help you with decoding in Home Assistant, I don't use/know that system. In Javascript you would do something like this:
Regards,
Michael
Michael, thank you very much! Really helped a lot.
I managed to figure the script out and saving it in events folder (like advised in your template) I changed to 120 seconds...
I'll keep the condition with only when vehicle is on.
And finally...the recordtype (I don't know why at first the recordtype XKN-LOG-Consumption didn't work - maybe I was impatient :) ) but now it works!
I get:
[{"h_data":"{\"v.b.consumption\":0,\"xkn.v.trip.consumption.KWh/100km\":17.8205,\"xkn.v.trip.consumption.km/kWh\":5.6115}","h_recordnumber":0,"h_timestamp":"2023-01-13 19:36:52"}]
which is exactly what I was looking for!
Thank you!!!
I've just added support for the "since" parameter to the REST API.
Template:
The "since" parameter restricts the results to entries newer than the given timestamp.
The timestamp needs to be given in ISO format "YYYY-MM-DD HH:MM:SS", with time separated by either a space (URL encoded!) or a 'T'. Using 'T' has the advantage of not needing URL encoding.
The "since" feature is already installed on my server (dexters-web.de).
@Mark: the change is in the V3 server repository, it's a small change but please audit before installing.
Updated documentation: https://docs.openvehicles.com/en/latest/protocol_httpapi/requests.html#get-api-historical-vehicleid
Regards,
Michael
Just in case: if you did that by assuming there is a "ticker.120" event, that will not work.
See https://docs.openvehicles.com/en/latest/userguide/events.html#standard-events for the ticker events available.
Regards,
Michael
Yes indeed it was not working :)) I left it with 60 sec :)
I migrated to dexters-web server and I have trouble getting (tunneling) 'historical' (wif-signal, consumption etc.) into the HA. I tried using command line like described here (https://github.com/openvehicles/Open-Vehicle-Monitoring-System-3/issues/624) but can't figure out how to configure sensor for historical dala from script above.
I managed to configure sensors for metrics from 'status', 'charge', location'... but I don't know how to change arguments in command line sensor
Any help would be appreciated?
You're asking how to decode a JSON-embedded JSON field in Home Assistant, I would normally suggest asking that in the HA forum.
A quick duck though just told me:
So you need to pipe "h_data" to the "from_json" filter to decode the embedded object. According to the HA forum something like this could work:
{{ (value_json.h_data|from_json)["v.b.consumption"] }}
Regards,
Michael
Thank you Michael, but I don't get any data piped to HA... I use this and don't know if the call is OK? Specifically the part: "$ip/api/cookie/historical/$vehicleid/XKN-LOG-WIFI-SQ/"
with help of ChatGPT it works now. The code is: