I use a Shelly Plus PM to monitor generated solar power.
The provided power value from the thing is negative.
I would like to invert a channel value.
I belief a transformation profile should be best.
I didn’t manage to get examples from similar posts running.
I would prefer a DSL transformation.
Is there somewhere a full example available?
Thank you!
Hi Rich and Udo,
thanks for your replies. Sounds easy but in practice, I don’t get it.
The attempt from the following screenshot results in
" [ERROR] [n.module.script.profile.ScriptProfile] - Failed to process script ‘DSL(| Double.parseDouble(input).doubleValue * -1)’: Could not get script for UID ‘DSL(| Double.parseDouble(input).doubleValue * -1)’.
"
thanks for this hint.
I’m asking myself why this is not mentioned in the docu, or is it?
One hurdle done, at least one mor to take:
The inline scripts fails to parse the input string to double because the string includes a unit, see below: Failed to process script '| -Double.parseDouble(input).doubleValue': For input string: "0 W"
Is there a simple solution, or do I need to implement some string magic to get parsing into double to work?
This thread seems to go into the right direction, will check tomorrow…
Hmm, that’s strange, because the function parseDouble should ignore anything other than the numeric part of the string. Maybe you can get away with this:
this: input.toString.split(' ').get(0) will only get the part before the first space within the input string. Maybe you don’t need the .toString (so input.split(' ').get(0) would suffice) , but it shouldn’t hurt.
This is the shortest variant working for me: | -Double.parseDouble(input.split(' ').get(0))
I don’t need to add the unit, since the item carries the unit within its metadata.
But why I don’t need to convert the double back to a string?
This seems to be done implicitly somewhere.
Note that this is only true if the unit supplied by the Channel matches the unit in the Item metadata. If, for example, the Channel delivers W but the Item’s metadata is set to kW, then when you strip off the unit the W won’t be convert automatically to kW and it will just be treated as kW.
Common types like numbers get converted back to a String automatically by the SCRIPT transform.
If it were not for the initial requirement to use Rules DSL, I would have pointed you to this approach.