Construct/Format String in Blockly - How?

Hey everyone,

I am currently trying to move all my rules into the Main UI and only my Alexa rules are missing now.
I am currently stuck when trying to construct a string in the Blockly UI and would appreciate it if someone could guide me in the right direction.

This is the rule I am trying to recreate in blockly:

rule "Alexa Dieselpreis Rule"
when
	Member of AllAlexaDevices changed
then   
    if(Dieselpreis.state == ON){
        Dieselpreis.postUpdate(OFF)
        val String formattedForAlexa = (String::format("%.2f", (TankstelleOIL_Diesel.state as DecimalType).floatValue())).replace(".",",")
        val String sourceRoom = triggeringItem.name.split("_").get(0) // der erste Teil des Itemnamens 
        logInfo("Alexa Specific Rules","SourceRoom " + sourceRoom)
        val String DestinationRoom = sourceRoom + "_Sprich"
        logInfo("Alexa Specific Rules","DestinationRoom " + DestinationRoom)
        DestinationRoom.sendCommand('<speak>Aktueller Dieselpreis ist <say-as interpret-as="unit">EUR' + formattedForAlexa + '</say-as></speak>')
    }
end

How can I format a String of an item state in Blockly?

Could you show us what you have tried so far?

Well I created the rule up to the point where I need to construct the string.

I created a variable but other than that I am not sure how to continue now.

Can you show us?

Currently it is only this, because I do not know how to continue in blockly:

Great! And can you give an example value for your Item TankstelleOIL_Diesel?

It like this:

Well this is not fun! To convert

val String formattedForAlexa = (String::format("%.2f", (TankstelleOIL_Diesel.state as DecimalType).floatValue())).replace(".",",")

into Blockly I’ve come up with this ugly thing:

The first bit forces the string into a number, and then rounds to the nearest two decimal places. Blockly has no native function for that. The code in that block is:

number_only = parseFloat(TankstelleOIL_Diesel).toFixed(2)

There’s no native string replace function in Blockly, so I’ve had to hack my way round by finding the position of the dot, splitting there, and then re-combining with a comma in-between. You could do this all on one line in Blockly, but that makes taking a screenshot difficult…!

The whole generated code looks like:

var TankstelleOIL_Diesel, position_dot, euros, number_only, cents, formattedForAlexa;


TankstelleOIL_Diesel = '2.079 €';
number_only = parseFloat(TankstelleOIL_Diesel).toFixed(2)
position_dot = number_only.indexOf('.') + 1;
euros = number_only.slice(0, position_dot - 1);
cents = number_only.slice(((position_dot + 1) - 1), number_only.length);
formattedForAlexa = [euros,',',cents].join('');

To be honest, there are much quicker ways of achieving this using pure Javascript inside the inline script (advanced) block. If it were me, I’d go down that route…

Thank you very much for this really appreciate the time you have put into this!
Will rebuild it later today.

Could you elaborate on that? Would love to hear more about this aswell

Sure. You can reduce all of that Blockly code into the following:

image

Where the inline script has the following:

formattedForAlexa = parseFloat(TankstelleOIL_Diesel).toFixed(2).replace(".",",")

The limitations in Blockly would, I think, end up with me just using a plain Javascript rule/script here instead of Blockly, because replacing (and later for you, splitting) text is a little painful (and the inline script (advanced) block is difficult to work with for anything more than a short line, at least for me on Firefox).

1 Like