Change string to number and dimmer to number in variables

Hi,
i want to convert a dimmer into a number inside a variable. How can i do this?

Here are my two places inside the rules:

var Number volume_old = Squeezeplayer_EG_WC_volume.state

Squeezeplayer_EG_WC_volume is a dimmer-item.

error:

{
	"resource": "/w:/rules/squeezebox.rules",
	"owner": "_generated_diagnostic_collection_name_#0",
	"code": "org.eclipse.xtext.xbase.validation.IssueCodes.incompatible_types",
	"severity": 8,
	"message": "Type mismatch: cannot convert from State to Number",
	"startLineNumber": 199,
	"startColumn": 28,
	"endLineNumber": 199,
	"endColumn": 60
}

And the second problem in one of my rules:

How can i convert a string to a number? When i use a number-item between, i think the auto conversion will do this, but is this possible inside a variable declaration too?

This works:

val newValue_uid = transform("JSONPATH", "$.uid", rfid_all_haustuer.state.toString)
if (newValue_uid == 27) return;

This works not:

val newValue_uid = transform("JSONPATH", "$.uid", rfid_all_haustuer.state.toString)
if (newValue_uid < 99) return;

This is also not working:

val Number newValue_uid = transform("JSONPATH", "$.uid", rfid_all_haustuer.state.toString)

rfid_all_haustuer is a string-variable

How can i edit this to get it working?

Should be:

var Number volume_old = Squeezeplayer_EG_WC_volume.state as Number

Second problem:
The transform action returns Strings only, so you have to parse the string into a number:

val Number newValue_uid = Float::parseFloat(transform("JSONPATH", "$.uid", rfid_all_haustuer.state.toString))
1 Like

Thanks, first error is gone. I will try the second one too.

EDIT:
No errors anymore in Visual Studio Code, will try if it works, when i´m at home.

Today i saw, that the number to string conversion doesn´t work like expected. The string is with a big number, without point or decimals like this: 92348716. And with Float::partseFloat i get something like this:

1.2010493E7

What´s wrong with this? How can iget the right number from my string?

Is

Float::parseFloat

the wrong command for my usage? Is there another command which can do that?

I saw something about “new float”? But now idea how to use this…

Did some further tests:

When my rfid-chip gives me a uid number with only 7 digits, then Float:parseFloat gives me a number with .0 at the end.

When my uid-number has 8 digits, then i get a strange number like 1.3642712E7

I don´t need .0 at the end of the number. How can i convert my long string-number to the right format? Is there a maximum of 7 characters for the conversion?

Try Integer::parseInt, what does it return then?

1 Like

Thanks, this seems to work… Now i get a real number without any E7 or .0 at the end, even if the number has 8 digits.