How chain a JS transformation after a JSONPATH one?

I am trying to read electricity unit prices for the Octopus Agile tariff into a series of items using an HTTP thing with a JSONPATH transformation and then a JS transformation.

The HTTP binding retrieves the JSON containing the unit prices correctly and the JSONPATH transformation can pick out the particular values that I’m interested in. The URL for the prices is here and returns data like this:

{"count":5614,"next":"https://api.octopus.energy/v1/products/AGILE-23-12-06/electricity-tariffs/E-1R-AGILE-23-12-06-G/standard-unit-rates/?page=2","previous":null,"results":[{"value_exc_vat":11.13,"value_inc_vat":11.6865,"valid_from":"2024-03-18T22:30:00Z","valid_to":"2024-03-18T23:00:00Z","payment_method":null},
etc
etc
etc
(there are 48 sets of data for each 30 minute slot throughout the day)

The JSONPATH transformation I’m using is of the format:

JSONPATH:$.results[0].value_inc_vat

Which retrieves the unit price correctly, however the price is in UK pence (e.g. 11.6865 pence today at 22:30) and the number item is defined using the Energy/Price unit which is GBP/kWh so the value from the JSONPATH transformation needs to be divided by 100.

I’ve created a transform in the transform directory called divide100.js and set the contents to:

(function (data) {
    return (data / 100)
})(input)

I then updated the channel transformation to:

JSONPATH:$.results[0].value_inc_vat ∩ JS:divide100.js

However, I get an error in openhab.log:

2024-03-18 11:49:23.538 [WARN ] [.transform.SingleValueTransformation] - Transformation service  JS for pattern divide100.js not found!

I have the JavaScript scripting addon installed, the one for ECMAScript 2022+ (has a bright yellow background in the add-on store) and have tried restarting OH after I created the divide100.js transform but that didn’t help.

Can anyone suggest what magic I’m missing to get the chaining of transforms to work?

Try removing the spaces around the intersection symbol

1 Like

That fixed it, thank you! The error message made me think there was something wrong with the syntax of the JS transform rather than the way in which it was included in the state transformation field.

I think it should be allowed, but this needs some change in the add-on or core… which binding is this for?

EDIT: It’s HTTP binding, and it seems you’re using openhab 4.1 or earlier.

This issue is fixed in 4.2 - you can have spaces around the symbol now. The handling of this for HTTP binding has been DRY’ed into core.

mqtt, modbus and serial binding haven’t yet been updated to use core’s code.

Yes, the HTTP binding on 4.1, I forgot to mention which version I’m using in the OP.

That’s great news, thanks for the info.