<div dir="ltr">I've worked out what the decode flag is for and how it works, and I think how optional params work. <br><div>I'm pretty sure I won't  need the 'AsFloatUnit' function; the unit would be an option to AsFloat(); I'll know that soon.</div><div><br></div><div>The 'Value' function is more complicated because of the optional decode bool. I guess I could add the Unit to the end of that.</div><div><br></div><div>ValueUnit could be still useful then to provide a 'Value + Unit'.</div><div><br></div><div>Question:  Is there a reason we shouldn't be returning with  <span style="color:rgb(220,220,170);background-color:rgb(30,30,30);font-family:Consolas,"Courier New",monospace;font-size:14px;white-space:pre">duk_push_null</span>    if the metric !IsDefined()  in both AsFloat() and Value(metric,true) cases?</div><div><br></div><div>//.ichael</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, 6 Nov 2022 at 11:22, Michael Geddes <<a href="mailto:frog@bunyip.wheelycreek.net" target="_blank">frog@bunyip.wheelycreek.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Right, so I've implemented some stuff that seems to work quite well. <br></div><div><br></div><div><a href="https://github.com/openvehicles/Open-Vehicle-Monitoring-System-3/pull/764" target="_blank">https://github.com/openvehicles/Open-Vehicle-Monitoring-System-3/pull/764</a>  should be ready now after a couple of stupid mistakes slipped through.   This absolutely needs somebody to review it please! (There's a reason why I've converted some if()'s to switch() - which is that it will be used in the follow-up commit).</div><div><br></div><div>The commit that will follow on from that it implements the new Units: kWh/100km, km/kWh  and  mi/kWh.</div><div><br></div><div>This is a summary of what I've implemented for scripting - including showing the unit codes I have so far.  I've considered a few things:</div><div>  * Should some of the longer unit codes be shortened  (eg mi, mins, m, ft, deg, perc)</div><div>  * The unit codes could be much more re<font face="arial, sans-serif">gular and separated by dots  eg:  </font></div><div><font face="arial, sans-serif">        watthours -> w.h</font></div><div><font face="arial, sans-serif">        kwhp100km -> kw.h_100km or kw.h/100km</font></div><div><font face="arial, sans-serif">        miph ->  mi_h or mi/h  (or should it be mph).</font></div><div><font face="arial, sans-serif">        psi -> <a href="http://p_in.in" target="_blank">p_in.in</a> or p/<a href="http://in.in" target="_blank">in.in</a> or <a href="http://lb_in.in" target="_blank">lb_in.in</a> (yes, slightly weird, but predictable)</font></div><div><font face="arial, sans-serif"> </font><br></div><b style="font-family:monospace">OVMS# metric units</b><br><font face="monospace">          km : km</font><br><font face="monospace">       miles : M</font><br><font face="monospace">      meters : m</font><br><font face="monospace">        feet : ft</font><br><font face="monospace">     celcius : °C</font><br><font face="monospace">  fahrenheit : °F</font><br><font face="monospace">         kpa : kPa</font><br><font face="monospace">          pa : Pa</font><br><font face="monospace">         psi : psi</font><br><font face="monospace">       volts : V</font><br><font face="monospace">        amps : A</font><br><font face="monospace">    </font>amphours<font face="monospace"> : Ah</font><br><font face="monospace">          kw : kW</font><br><font face="monospace">         kwh : kWh</font><br><font face="monospace">       watts : W</font><br><font face="monospace">   </font>watthours<font face="monospace"> : Wh</font><br><font face="monospace">     seconds : Sec</font><br><font face="monospace">     minutes : Min</font><br><font face="monospace">       hours : Hour</font><br><font face="monospace">         utc : UTC</font><br><font face="monospace">     degrees : °</font><br><font face="monospace">        kmph : km/h</font><br><font face="monospace">        </font>miph<font face="monospace"> : Mph</font><br><font face="monospace">      </font>kmphps<font face="monospace"> : km/h/s</font><br><font face="monospace">      </font>miphps<font face="monospace"> : Mph/s</font><br><font face="monospace">        mpss : m/s²</font><br><font face="monospace">         dbm : dBm</font><br><font face="monospace">          sq : sq</font><br><font face="monospace">     percent : %</font><br><font face="monospace">       whpkm : Wh/km</font><br><font face="monospace">       </font>whpmi<font face="monospace"> : Wh/mi</font><br><font face="monospace">   kwhp100km : kWh/100km</font><br><font face="monospace">      </font>kmpkwh<font face="monospace"> : km/kWh</font><br><font face="monospace">      </font>mipkwh<font face="monospace"> : mi/kWh</font><br><font face="monospace">          nm : Nm</font><br><font face="monospace"><br><b>OVMS# metric unit mi</b><br>       miles : M<br>     minutes : Min<br>        miph : Mph<br>      miphps : Mph/s<br>       whpmi : Wh/mi<br>      mipkwh : mi/kWh</font><br><br><b style="font-family:monospace">OVMS# metric get xiq.v.trip.consumption<br></b><font face="monospace">17.0597kWh/100km</font><br><b style="font-family:monospace">OVMS# metric get xiq.v.trip.consumption kpkwh<br></b><font face="monospace">5.86177km/kWh</font><br><b style="font-family:monospace">OVMS# metric get xiq.v.trip.consumption mpkwh<br></b><font face="monospace">3.64233mi/kWh</font><br><br><b style="font-family:monospace">OVMS# metric set xiq.c.speed 5 miph<br></b><font face="monospace">Metric set</font><br><b style="font-family:monospace">OVMS# metric get xiq.c.speed<br></b><font face="monospace">8.04673km/h</font><br><b style="font-family:monospace">OVMS# metric get xiq.c.speed miph<br></b><font face="monospace">5Mph</font><br><br><font face="arial, sans-serif">And then in DukTape - there are some questions I have about the implementation:</font><div><font face="arial, sans-serif">* Names of functions? Better ideas? </font></div><div><font face="arial, sans-serif">* Should ValueUnit output the units?</font></div><div><font face="arial, sans-serif">* In Value() there is the line  </font>  bool decode = duk_opt_boolean(ctx, 1, true);</div><div>    * What does 'decode' mean here?</div><div>    * Do I need it for ValueUnit() ?</div><div><br><div><b><br><font face="monospace">(function() {</font><br><font face="monospace">   print( OvmsMetrics.Value("xiq.v.trip.consumption"));</font><br><font face="monospace">   print("\n")</font><br><font face="monospace">   print( OvmsMetrics.ValueUnit("xiq.v.trip.consumption",""));</font><br><font face="monospace">   print("\n")</font><br><font face="monospace">   print( OvmsMetrics.ValueUnit("xiq.v.trip.consumption","</font>mipkwh<font face="monospace">"));</font><br><font face="monospace">   print("\n")</font><br><br><font face="monospace">   print( OvmsMetrics.AsFloatUnit("xiq.v.trip.consumption","</font>kmpkwh<font face="monospace">"));</font><br><font face="monospace">})();</font></b><br><font face="monospace">--- Output ---</font><br><font face="monospace">17.0597</font><br><font face="monospace">17.0597kWh/100km</font><br><font face="monospace">3.64233mi/kWh</font><br><font face="monospace">5.86177</font><br><font face="monospace">------</font><br><br></div></div><div>The basic stuff all works - it's just quibbling over the details.. but let's get them right!</div><div><br></div><div>//.ichael</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, 5 Nov 2022 at 20:09, Michael Geddes <<a href="mailto:frog@bunyip.wheelycreek.net" target="_blank">frog@bunyip.wheelycreek.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Yeah - this was copied code from kia/kona and is what triggered these ideas; I totally agree this shouldn't be doubled up on.</div><div><br></div><div>I've got some commits centred round Metrics that I'll just check over and push up ... and then I'll just have the single <span style="font-family:monospace">xiq.v.</span><span style="font-family:monospace">trip.consumption </span>metric (unless you have some ideas for the namespace) which will be much neater.</div><div><br></div><div>If it's ok with you then I might do that unit conversion proposal.</div><div>Would it ok if the unit specifications were the same as to the programatic codes in ovms_metrics.h?</div><div>(kWh,   WattHours , MetersPSS ) </div><div>I would probably add a command</div><div><font face="monospace">metric units</font> <spec> <br></div><div>to list all (matching) units and their associated Labels.</div><div><br></div><div>//.ichael </div><div><br></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, 5 Nov 2022 at 18:48, Michael Balzer <<a href="mailto:dexter@expeedo.de" target="_blank">dexter@expeedo.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
  
    
  
  <div>
    Michael,<br>
    <br>
    adding unit conversion support to the shell and Duktape commands is
    a good idea.<br>
    <br>
    Metrics are not meant to provide a user interface, they should be
    defined to be efficient and non-redundant.<br>
    <br>
    Btw, metrics names also shall not use upper case characters, and
    shall only use "." as a separator.<br>
    <br>
    Regards,<br>
    Michael<br>
    <br>
    <br>
    <div>Am 05.11.22 um 11:22 schrieb Michael
      Geddes:<br>
    </div>
    <blockquote type="cite">
      
      <div dir="ltr">Hi all,<br>
        <div>Some of the code I copied from Kona/Kia code had both
          kwh/100km and km/kwh metrics in the code as 'Other'. </div>
        <div>Adding the various power consumption Units is not
          particularly hard (I will have a pull-request soon) - though
          the conversions between them all required some thought!</div>
        <div>... but it also made me think these two metrics that are
          (with the consumption units added) defined like this:  </div>
        <div><font face="monospace">m_v_trip_consumption1 =
            MyMetrics.InitFloat("xiq.v.trip.consumption.KWh/100km", 10,
            0, kWHP100K);<br>
            m_v_trip_consumption2 = MyMetrics.InitFloat("<a href="http://xiq.v.trip.consumption.km/kWh" target="_blank">xiq.v.trip.consumption.km/kWh</a>",
            10, 0, kPkWH);<br>
          </font></div>
        <div><br>
        </div>
        <div>These are effectively the same metric but in different
          units!</div>
        <div>I'm wondering if we would be better to have scripting and
          Duktape support for converting metrics to different unit! 
          This might be also quite useful for those strange countries
          that insist on using miles as a measurement.</div>
        <div><br>
        </div>
        <div>On top of the 'metric list' and 'metric set' we could add a
          'metric get' which gets a single value.. and add unit support
          for get/set.</div>
        <div><br>
        </div>
        <div>I've also got a pull request that improves the precision of
          the km<->mi conversions and factors it out.</div>
        <div><br>
        </div>
        <div>//.ichael</div>
      </div>
      <br>
      <fieldset></fieldset>
      <pre>_______________________________________________
OvmsDev mailing list
<a href="mailto:OvmsDev@lists.openvehicles.com" target="_blank">OvmsDev@lists.openvehicles.com</a>
<a href="http://lists.openvehicles.com/mailman/listinfo/ovmsdev" target="_blank">http://lists.openvehicles.com/mailman/listinfo/ovmsdev</a>
</pre>
    </blockquote>
    <br>
    <pre cols="72">-- 
Michael Balzer * Helkenberger Weg 9 * D-58256 Ennepetal
Fon 02333 / 833 5735 * Handy 0176 / 206 989 26</pre>
  </div>

_______________________________________________<br>
OvmsDev mailing list<br>
<a href="mailto:OvmsDev@lists.openvehicles.com" target="_blank">OvmsDev@lists.openvehicles.com</a><br>
<a href="http://lists.openvehicles.com/mailman/listinfo/ovmsdev" rel="noreferrer" target="_blank">http://lists.openvehicles.com/mailman/listinfo/ovmsdev</a><br>
</blockquote></div></div>
</blockquote></div>
</blockquote></div>