Negative value in a chart

I’ve another silly question, also related to the chart-pages in OH3/4:

Is there a possibility to use the value of an item in a chart NEGATIVE. I have an item showing my EVU-consumption/upload. Generally it has the correct algebraic sign, means a “-” (negative) when I buy power and no sign (positive) when I upload. For various reasons I do not want to change that at the item.
But for some charts it would be more intuitive to understand if the value would be negative instead of positive and vice versa.

Currently I spend an additional item and a rule which just do a *(-1) every time the item changes (which is VERY often and therefore the rule is also fired pretty often.

Any better idea to just the chart to use the value in the item *(-1) ???

Thanks from a programming-idiot :wink:

If it’s OK for the Item to be negative all the time, I’d use a transform profile with a simple JS transform (or language of your choice) to multiply it by -1.

But in general, a rule that runs a lot isn’t necessarily a problem per say. Only if you are seeing actual negative behaviors would I worry about it.

thanks for quick response:
No i do not want to make the item-state negative, as this would cause several issues in other automations; overall it IS correct. I want to do it just in one (of some) charts.
I can’t currently tell you, if the rule is causing load issues at the moment, as I currently run this on a spare-pi, which I use for test for an upcoming update and this on have not to much load at all.
The production-pi is the same type but have MUCH more to do, roughly 150 things, 1000 items, around 100 rules and run a debmatic as well as a home-bridge and a mosquito-server. So I want not going the risk here.

and btw. JS-Transformation is something I struggle with since ages :wink:

This seems to me is something much above my knowledge…

You could still use the profile on the additional proxy Item but it’s not really going to save you anything in terms of load.

Note that in OH 4:

  • you can define the transform in the UI
  • you can use any rules language including Rules DSL

Even in OH 3.4 you can use the SCRIPT transformation and write the transform in Rules DSL.

Anyway, Using the JS transform is pretty simple:

(function(data) {
  var toNum = parseFloat(data);
  if(toNum == 'NaN') return data; // handles UNDEF and NULL
  else return toNum * -1;

Rules DSL would look something like:

var rval = input;

if(input != "NULL" || input != "UNDEF")  {
    rval = new Float.parseFloat(input) * -1

I’m assuming the Item doesn’t have units. (Note I just typed in the above without testing. The biggest gotcha is knowing that input is a String representation of the Item state so you have to parse it to multiply it by -1.

1 Like

great help, playing a bit around with that.
As the item is coming from an generic MQTT-Thing, it’s probably an idea to read the MQTT-value for the the second “negative” item once again from mosquito and do a transformation here. So I save at least the rule.

The whole story is not really to get the stuff working (what it already does) but more to do it the best and optimized way. And I thought, if the only place I need the negative value is the chart, it would be best idea to do it at the chart (and only if the chart is showing). But sometimes there is no direct way…

Thanks so much Rick.

Playing (again) around with the profiles and did:

and I got:

So it looks like the script is not found…

What did I do wrong??

ok, found this:

Looks like the transformation (in general?) do not find a script, which is done in the UI. So I assume, the (transformation-)scripts has to be done in the filesystem directly.

I manually created a file in the transform-folder and followed the workaround in the above linked thread and it seems to work now (to small numbers at the moment to be sure).

Before OH 4.0 M3 the only way to drive a transform was either inline (added in OH 3.3 for the JS transformation only) or by defining it in a file. In OH 4.0 M3+ there is a new Settings → Transformations menu where you can define transforms.