<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    Michael,<br>
    <br>
    looks all good to me, once again nice find with the decode argument.
    Adding decode to the Value() call was only for symmetry IIRC, the
    main use was with GetValues()
(<a class="moz-txt-link-freetext" href="https://docs.openvehicles.com/en/latest/userguide/scripting.html#ovmsmetrics">https://docs.openvehicles.com/en/latest/userguide/scripting.html#ovmsmetrics</a>).<br>
    <br>
    Don't forget to test arrays, e.g. "v.t.pressure" & "v.t.temp".<br>
    <br>
    Returning null for an undefined metric seems like a natural choice,
    but is a rather deep change, as for consistency not only the Duktape
    metrics API but also the Web UI metrics API would need to be changed
    accordingly. Unless you've got a real use case that needs that, we
    should be careful.<br>
    <br>
    Regards,<br>
    Michael<br>
    <br>
    <br>
    <div class="moz-cite-prefix">Am 07.11.22 um 15:00 schrieb Michael
      Geddes:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAH0p7uKjBYUB+-L2og1-QGjzn9Ftg=zYygU9DcT0SmSunkOnXg@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">
        <div dir="ltr">I have figured out a bunch of stuff and have
          implemented the following: (having done away with needing
          AsFloatUnit)
          <div><br>
          </div>
          <div>OvmsMetrics.Value( {metric} [, {decode}])</div>
          <div>OvmsMetrics.Value( {metric}, {unit} [,{decode}])</div>
          <div><br>
          </div>
          <div>It turns out that the [decode] flag wasn't working anyway
            (since the function was being registered as only having 1
            param)...</div>
          <div>This way it is still really 1 function.. but I check it
            the second parameter is a 'boolean', and if not.. try the
            second form. </div>
          <div><br>
          </div>
          <div>OvmsMetrics.AsFloat( {metric} [,{unit}] )</div>
          <div><br>
          </div>
          <div>and add the function</div>
          <div><br>
          </div>
          <div>Ovms.Metrics.ValueUnit( {metric} [,{unit}]) </div>
          <div>This prints the value and the unit.</div>
          <div><br>
          </div>
          <div>Here's a sample function and the output! This also shows
            the types of the output.</div>
          <div><br>
          </div>
          <div><font face="monospace">(function() {<br>
                 x = OvmsMetrics.Value("xiq.v.trip.consumption");<br>
                 print( (typeof x) + ": "+  x+"\n"  );<br>
                 x = OvmsMetrics.Value("xiq.v.trip.consumption", false);<br>
                 print( (typeof x) + ": "+  x +"\n" );<br>
                 x =
               OvmsMetrics.Value("xiq.v.trip.consumption","kmpkwh")<br>
                 print( (typeof x) + ": "+ x +"\n");<br>
                 x =  OvmsMetrics.Value("xiq.v.trip.consumption",
              "mipkwh", false) <br>
                 print( (typeof x) + ": "+ x +"\n");<br>
                 x =  OvmsMetrics.ValueUnit("xiq.v.trip.consumption")<br>
                 print( (typeof x) + ": "+ x +"\n");<br>
                 x =
               OvmsMetrics.ValueUnit("xiq.v.trip.consumption","mipkwh")<br>
                 print( (typeof x) + ": "+ x +"\n");<br>
                 x =  OvmsMetrics.AsFloat("xiq.v.trip.consumption")<br>
                 print( (typeof x) + ": "+ x +"\n");<br>
                 x =
               OvmsMetrics.AsFloat("xiq.v.trip.consumption","kmpkwh")<br>
                 print( (typeof x) + ": "+ x +"\n");<br>
              })();</font><br>
          </div>
          <div><br>
          </div>
          <div>
            <pre class="gmail-receiver" id="gmail-output" style="box-sizing:border-box;overflow:auto;font-family:ui-monospace,"Cascadia Mono","Segoe UI Mono",Hack,"Source Code Pro","Roboto Mono",Menlo,Monaco,Consolas,monospace;font-size:13px;padding:9.5px;margin-top:0px;margin-bottom:10px;line-height:1.42857;color:rgb(0,34,0);word-break:break-all;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);border-radius:4px;height:165px;white-space:pre-wrap">number: 17.0582
string: 17.0582
number: 5.86227
string: 3.64264
string: 17.0582kWh/100km
string: 3.64264mi/kWh
number: 17.0582
number: 5.86227</pre>
          </div>
          <div><br>
          </div>
          <div><br>
          </div>
          <div>It still might be an idea to use 'null' as a return value
            if the metrics is<font face="monospace"> !IsDefined() </font><font
              face="arial, sans-serif">but that would be changing the
              existing behaviour slightly.</font></div>
          <div><font face="arial, sans-serif"><br>
            </font></div>
          <div><font face="arial, sans-serif">//.ichael</font></div>
          <div><br>
          </div>
        </div>
        <div class="gmail_quote">
          <div dir="ltr" class="gmail_attr">On Mon, 7 Nov 2022 at 08:12,
            Michael Geddes <<a
              href="mailto:frog@bunyip.wheelycreek.net"
              moz-do-not-send="true" class="moz-txt-link-freetext">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">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-wrap">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" moz-do-not-send="true"
                  class="moz-txt-link-freetext">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" moz-do-not-send="true"
                      class="moz-txt-link-freetext">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"
                        moz-do-not-send="true">p_in.in</a> or p/<a
                        href="http://in.in" target="_blank"
                        moz-do-not-send="true">in.in</a> or <a
                        href="http://lb_in.in" target="_blank"
                        moz-do-not-send="true">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" moz-do-not-send="true"
                      class="moz-txt-link-freetext">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" moz-do-not-send="true"
                            class="moz-txt-link-freetext">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"
                                      moz-do-not-send="true">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" moz-do-not-send="true" class="moz-txt-link-freetext">OvmsDev@lists.openvehicles.com</a>
<a href="http://lists.openvehicles.com/mailman/listinfo/ovmsdev" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">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" moz-do-not-send="true"
                            class="moz-txt-link-freetext">OvmsDev@lists.openvehicles.com</a><br>
                          <a
                            href="http://lists.openvehicles.com/mailman/listinfo/ovmsdev"
                            rel="noreferrer" target="_blank"
                            moz-do-not-send="true"
                            class="moz-txt-link-freetext">http://lists.openvehicles.com/mailman/listinfo/ovmsdev</a><br>
                        </blockquote>
                      </div>
                    </div>
                  </blockquote>
                </div>
              </blockquote>
            </div>
          </blockquote>
        </div>
      </div>
      <br>
      <fieldset class="moz-mime-attachment-header"></fieldset>
      <pre class="moz-quote-pre" wrap="">_______________________________________________
OvmsDev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:OvmsDev@lists.openvehicles.com">OvmsDev@lists.openvehicles.com</a>
<a class="moz-txt-link-freetext" href="http://lists.openvehicles.com/mailman/listinfo/ovmsdev">http://lists.openvehicles.com/mailman/listinfo/ovmsdev</a>
</pre>
    </blockquote>
    <br>
    <pre class="moz-signature" cols="72">-- 
Michael Balzer * Helkenberger Weg 9 * D-58256 Ennepetal
Fon 02333 / 833 5735 * Handy 0176 / 206 989 26</pre>
  </body>
</html>