Converting String to Int, but keep decimal place?

Hello everyone. New to Home Assistant here, but trying to learn rapidly.

I have an issue where I am trying to do math on electricity usage based on a varying tariff. This part is done and working great. However, the output from these sensors is a string with data that I need to strip out before doing math. Not an issue here either… but, in order to convert the string to an integer, I need to remove the decimal place. Otherwise, it doesn’t convert to an integer.

Here is my template code:

{{
(
((states.sensor.winter_offpeak_math.state.split()[2])
|replace("'","")|replace(".","")|int
+ 
(states.sensor.winter_onpeak_math.state.split()[2]) |replace("'","")|replace(".","")|int
)|int/100 )
}}

The code for my Winter Onpeak and Winter Offpeak calculations looks like this:

winter_Offpeak_Offset:
    value_template: '{{ ((states.sensor.newmeter_winter_offpeak.state | multiply(1) | float * 0.23)) | round(2) }}'

The output BEFORE I do the stripping from this code looks like this:

winter_Offpeak_Offset: value_template: '0.2'

I have tried leaving the decimal in, but the resulting |int only gives me a 0
I have tried adding round(2) and |float as well…

Can anyone suggest better math or code to accomplish the intended goal?

I’m not sure how much of that literal string value is real, but whatever gets you to the 0.2 will convert to float with this:

{{ float('0.2', 0) }}

Ok, so I updated some of the code to get the result to change (making it easier to see the string data) using your suggestion. My output from this is now .2. But, I still have to strip that decimal place to convert it to an integer…

Math code:

{{
(
((states.sensor.winter_offpeak_math.state)
|replace("'","")|replace(".","")|int
+ 
(states.sensor.winter_onpeak_math.state) 
|replace("'","")|replace(".","")|int
)|int/100 )
}}

But my new code and output from the math state looks like this:

{{ float(((states.sensor.newmeter_winter_offpeak.state 
| multiply(1) | float * 0.23)) | round(2),0) }}

with an output of simply:

.2

(That last int/100 is there to turn the whole number created into a decimal place… but can easily be removed if we can fix the conversion)

You don’t have to strip the decimal, if you do then there’s something else. As you can see from my example, I passed a string with a decimal and here’s the output:

Interesting… maybe I am doing the math wrong then? I get the simple value of .2 and .01 as an output state from the two sensors. When I don’t convert them to an int, and just do the math with the below code, this is what I get:


{{

((states.sensor.winter_offpeak_math.state)
|replace("'","")
+ 
(states.sensor.winter_onpeak_math.state) 
|replace("'","")
)
}}

Output:

0.200.01

If I convert to an integer, I have to strip the decimal place to do the math. Otherwise, the math just puts the two results next to eachother “0.20” and “0.01”

Does it really have TWO decimal places? Like a version number?

Wow I actually have a reason now to use live chat, LOL. I though it was the most worthless feature on this forum upgrade, but I guess there is a use for it after all :slight_smile:

You’d literally just use |float instead of |int it’s that simple.

1 Like

That’s not his issue in the end, I live chatted him to an answer, I’ll ask him to post results.

1 Like

Here it is though:

{{

((states.sensor.winter_offpeak_math.state)
|replace("'","")
+ 
(states.sensor.winter_onpeak_math.state) 
|replace("'","")
)
}}

He’s just removed |int from there, and hasn’t replaced it with |float

{{ float((states.sensor.winter_offpeak_math.state) ,0) + float((states.sensor.winter_onpeak_math.state) ,0) }}

This is how we resolved it in live chat. The problem is each of those strings needed converted to float so you can add them, he was concatenating them because they were strings, then trying to strip it down to a number from there.

I use float(‘sensor…’, 0) instead of ‘sensor…’ | float just to avoid math errors if it’s unavailable, it’ll default to 0. After being bit by this numerous times it’s how I always do floats now. Everyone has there own way, I’m sure, this is mine :smiley: