Modify transformed value (as shown in the example)

Hi there,

I set up an item to get data from a script like so:

Thing exec:command:info [command="/path/to/info.sh", interval=60, autorun=false]

The data returned is JSON formated (in my case: ‘{ “temperature”: 15.3, “voltage”: 4.98 }’). So the item uses a JSONPATH transform to get the correct data:

String temp “Temperature [JSONPATH($.temperature):%s]” { channel=“exec:command:info:output” }

The example shown here works, but I’d like to add a unit after transforming the value for the displayed value to read as “15.3 °C”. Unfortunally adding the unit in the item label is done before the transformation, so the example from https://docs.openhab.org/addons/transformations.html doesn’t work:

String temp “Temperature [JSONPATH($.temperature):%s °C]” { channel=“exec:command:info:output” }

will put ‘{ “temperature”: 15.3, “voltage”: 4.98 } °C’ through the JSONPATH transformation. I looked it up and it is actually done in the org.eclipse.smarthome.ui.internal.items.ItemUIRegistryImpl.transform function. Here a regex is used to get all relevant data out of the item label.

Where do I have to post this for the example to get fixed?
Is there a solution which does not involve any rules or extarnal scripts?

Greetings,
Josef

The nomenclature here is important. You have set up a Thing, not an Item.

That transform only shows the correct value. The Item temp’s state is still the full JSON string.

I think transforms are maintained on the Eclipse SmartHome project. I agree this is probably not correct behavior.

Yes, put the transform on the Thing so the temp just gets set to the temperature value rather than the whole JSON String.

Thing exec:command:info [command="/path/to/info.sh", interval=60, autorun=false transform="JSONPATH($.temperature)"]

String temp "Temperature [%d °C]" { channel="exec:command:info:output" }

However, if you also care about the voltage or you need to actually do something with the temp or voltage numerical values you will need to use a Rule to parse out the values and post them to separate temp and voltage Items.

Everything @rlkoshak said is a good advise.
And i would also do the transformation on the return value of the thing.
If i would like to have both values , i would use a proxy item info_string to store the return value and
use a rule to updatet the Temp and Volt items.

Nevertheless transformation in the label works, there is nothing to fix , imho.
LabelTest_arg is just to make the example complete, it is not used.

.things

// Testing Jsonpatp label transformation
Thing exec:command:labeltestjson [
        command="/etc/openhab2/scripts/justWait.py LabelTest", 
        interval=0, autorun=false ]

.items

Switch LabelTest     "[%s]" { channel="exec:command:labeltestjson:run"    }
String LabelTest_arg "[%s]" { channel="exec:command:labeltestjson:intput" }
String LabelTest_out "[JSONPATH($.temperature):%s]" { channel="exec:command:labeltestjson:output" }

.sitemap

// Name of file and name of sitemap has to be the same

sitemap test label="System Test"
{
    // Testing exec with date
    Frame label="Test Date Exec"
    {
        Switch  item=Test_date
        Text    item=Test_date_arg
        Text    item=Test_date_out
    }

justWait.py

#!/usr/bin/env python

import sys,time,random

# Catch no argument error
if len(sys.argv) > 1:

    # Test Transformation in labeln
    if sys.argv[1] == "LabelTest" :
        randnum =  random.uniform( -40, 40)
        print( "{ \"temperature\":  %.2f, \"voltage\": 4.98 }"% (randnum) )
else:
    print "Error! No Argument"
    sys.exit (1)

I would like to discuss this. From my point of view, I would not expect anything outside of the closing paren to be passed to the transformation. For example:

"My label [JSONPATH($.myvalue):%s Units]"

I would expect only the state of the Item to be passed to the JSONPATH. What OP is seeing is that the state of the Item + " Units" gets passed to the transformation. This behavior is counter-intuitive. The closing ) implies the end of what is being passed to the transform. The : further implies that we have started a new syntatical segment of the label. The %s is NOT passed to the transformation. And it doesn’t work for most of the sitemap UIs to put " Units" outside the ].

So, not only is this counter-intuitive, it also makes it much more difficult to use transforms in labels and supply units. Granted, there are probably not all that many cases where this problem would be encountered. But I’d still maintain that the current behavior is at best counter-intuitive and should be fixed.

1 Like

Ahh thats right, think that i lost sight of the question while building my test setup.

I think you are right here, the second part should be read as the string pattern to apply a formatter transformation at.

And yes this is not working.

1 Like

Hi and thank you two for your answers. I guess I will stick to rules for my example or modify my script to include the units inside of the JSON.

For the other discussion: The closing bracket does end the transformation but the colon afterwads tells what is passed to the transformation. I don’t know why it was done like that because normally soeone would think that the value is passed but here you can modify the value prior to the transformation.

I appended this to a similar problem.

1 Like