Get only a part of a attribute

Guys, i have a template :

{{ ( int(state_attr('sensor.coinbase_eth_wallet', 'Balance in native currency')) )}}

This gives me back : 123,45 EUR

But i only want this part “123”

how can i strip the decimals and the EUR from it? how can i adjust my template?

thnx in advance

This will work with the value you provided but will fail if the value doesn’t contain a comma.

{{ state_attr('sensor.coinbase_eth_wallet', 'Balance in native currency').split(',')[0] }}

omg, you guys rock, always so easy
i was think by getting a regex to filter out only the value, and then use some round function

The problem with regex_findall is that if it doesn’t find a match for its pattern, it fails with an error. Ideally it should return None or something to indicate “failed to find a match”. Basically, for this application, it works no better than split failing if the value has no comma.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions. For more information refer to guideline 21 in the FAQ.

done, thnx for feedback!

I think a better method is to split on the space and replace the comma with dot to make it a float.

{{ state_attr('sensor.coinbase_eth_wallet', 'Balance in native currency').split(" ")[0].replace(",", ".") | int }}

This way it will not fail if there is no comma.
But it will fail if there is no space on the other hand, but since the currency is in the string that seems to be a sure thing.

If the currency is always EUR then the easiest is perhaps to remove EUR and replace the comma.

{{ state_attr('sensor.coinbase_eth_wallet', 'Balance in native currency').replace(" EUR", "").replace(",", ".") | int }}
1 Like

thnx, keeping this too :slight_smile:

Hi There all,

Interesting topic, I have a similar question relating to sun.sun

{{state_attr ('sun.sun','next_noon')}} results in: 2022-04-06T11:42:37+00:00

And what I’d like to have is only the time stamp preferably in local time?


{{as_timestamp(state_attr ('sun.sun','next_noon')) | timestamp_custom("%H:%M", true)}}

@hellis81 thanks you so much! Absolutely amazing.


I did change


in to


Otherwise it would not pass configuration as a sensor :wink:

That depends on how you write the sensor.

if you have:

value_template: "{{as_timestamp(state_attr ('sun.sun','next_noon')) | timestamp_custom("%H:%M", true)}}"

then yes it will fail.
But if you have:

value_template: >- 
               {{as_timestamp(state_attr ('sun.sun','next_noon')) | timestamp_custom("%H:%M", true)}}

Then it works fine.

My knowledge does not stretch that far as was already apparent from my initial question lol.

Nevertheless with willingness to learn, what is the difference? If you have time?

There are two string escaping characters. ' or ".
{{as_timestamp(state_attr ('sun.sun','next_noon')) | timestamp_custom("%H:%M", true)}}
both is used.
So adding a " on the outside will cause
"{{as_timestamp(state_attr ('sun.sun','next_noon')) | timestamp_custom("%H:%M", true)}}"

when you use >- and a line break you can use both in the string since none of them is needed to enclose the template.

When HA saves templates it’s usually converted to:

'{{as_timestamp(state_attr (''sun.sun'',''next_noon'')) | timestamp_custom(''%H:%M'', true)}}'

So double 'each time.

So in short there is many ways to do it. I should have used ' as you did in the timestamp_custom but I didn’t think about it.
Since I usually do these mistakes with using both, I have started using >- to my sensors.

Thanks for the explanation. Maybe i’ll start doing the same.

It took some time for me to figure out why it wasn’t working. that’s mainly because I solve these things with trial and error. I mentioned it in the thread so that if someone runs in to the same thing they have an idea what to look for. :wink:

I’d love to have some more in depth knowledge on this programming language but I just don’t have the time to study it. So these little bits of info I get from knowledgeable people like you are extremely valuable. I’m sure that goes for many HA enthusiasts here. So thanks again.