Hi everybody,
I am struggling with a JavaScript transformation.
What I’ve got so far:
1. MQTT sensor data from power outlet
I am using a Gosund SP1 Outlet flashed with Tasmota to read energy data from my washing machine. The data is transferred to openHAB through MQTT. The MQTT state topic of the Gosund device is subscribed to in the Gosund thing. The topic is called:
sonoff-washer/tele/SENSOR
And will yield the following string when subscribed to:
{"Time":"2022-12-06T14:11:03","ENERGY":{"TotalStartTime":"2022-11-29T22:57:37","Total":2.316,"Yesterday":0.000,"Today":0.000,"Period":0,"Power":0,"ApparentPower":0,"ReactivePower":0,"Factor":0.00,"Voltage":230,"Current":0.000}}
2. JavaScript Transformation to get TotalPower consumed and calculate cost
Then I set up a JS transformation to retrieve the MQTT topic string as input, perform a RegEx match to get the Total Power consumed value and multiply that value by my current electricity price. The item page looks like this, with the linked channel and JS transformation file:
The JS transformation file looks like this (be gentle I am no coder, forum posts combined with trial and error ):
processed = input.replace(/"/g, ''); //removes all quotes (") from the input string. Otherwise JS won't work
(function(i) {
var regex = /Total:\d*.\d*/g; //RegEx extracting the string "Total:" + the following number
var found = i.match(regex); //performing match with the RegEx above
strip = JSON.stringify(found).replace('["Total:', '').replace('"]', ''); //output of above is object, therefore converting back to string and removing all string components except the desired number
count = Number(strip); //converting the string from above to a number
output = count * 0.22; //multiplying the extracted total power consumption with current electricity price
return output; //returning the result of the calculation > this will be the linked item's state.
})(processed)
In the End, I have an item which directly converts the total power consumption measured by the Gosund SP1 to a price value. (Gosund > MQTT > JS Transformation > items with price as number)
Problem: As you might have noticed, the current energy price is “hard coded” into the JS transformation file. This leads me to What I want to have: I want the current energy price to be passed to the JS transformation as an additional parameter, which is taken from a corresponding item.
I read on the JS transformation page [Link] that additional parameters can be passed to a JS transformation script. I changed the whole thing around a bit. The page of my Test26 item now looks like this. Notice that I am using a different JS file and added ?price=0.22
behind the JS filename:
The slightly altered JS file looks like this. It mostly has the same functionality with the addition that the value I state behind the JS filename is now passed into the transformation (see also comments):
processed = input.replace(/"/g, '');
(function(i, p) { //p is added to the function
var regex = /Total:\d*.\d*/g;
var found = i.match(regex);
strip = JSON.stringify(found).replace('["Total:', '').replace('"]', '');
count = Number(strip);
output = count * p; //instead of a fixed value the added parameter value is used
return output;
})(processed, price) //price is imported as a additional parameter
So far, so good. Now I have the energy price no longer in the JS transformation file but hardcoded into the item settings in Main UI. And this is where I am stuck!
Question: Can I somehow reference an item state in my item’s profile configuration?
I have tried the following lines (energy_electricity_price is a number item with value 0.22):
gosund_costtotal_price.js?price=energy_electricity_price.state
gosund_costtotal_price.js?price=items.energy_electricity_price.state
gosund_costtotal_price.js?price=http://192.168.178.23:16680/rest/items/energy_electricity_price/state
gosund_costtotal_price.js?price=items.getItem(“energy_electricity_price”).state
In all above cases, no item state is transferred as an additional parameter to the JS transformation. Instead the string after the “=” sign is transferred. E.g. for the first case the string “energy_electricity_price.state”.
I am completely clueless how to call an item state within the item’s profile configuration, or whether this is at all possible? Maybe the item state could be called within the JS transformation file?
Any help is greatly appreciated