Where to learn about HA data types and conversions?

I have two temperatures displayed on my dashboard in different formats. One is a sensor and I think the other is a string. I would like to learn how to tell what format these “temperatures” are in and how to convert them to the same format so I could get the mean. My searching has not lead me to any source that teaches me about different data types in HA and how to convert them. I would be most appreciative if you would be able to point me to a good source to learn from. I know you experts could tell me how to do this quite easily but I need to learn how to do this. Thank you.

cards:
  - type: custom:mushroom-chips-card
    chips:
      - type: entity
        entity: sensor.krdg_temperature
        icon: mdi:thermometer
  - type: custom:mushroom-chips-card
    chips:
      - type: template
        content: '{{ state_attr(''weather.accuweather'', ''temperature'') }} °F'
        icon: mdi:thermometer

That is a tough one to answer because there isn’t likely to be a single place where you can easily learn all the ins and outs of manipulating values and templating your data in Home Assistant. In general, though, here are some good places to start:

https://jinja.palletsprojects.com/en/3.1.x/

https://docs.python.org/3/

I like to come on here not just to help others as I feel I should when I am able, but also to learn. I’ve written more code in Python than I can remember but even with that knowledge I was still confused with so much of the templating in HA, but where I’ve really supercharged my knowledge is right here on the forums (and Discord too if you are so inclined).

There are true masters of HA on this site that can do amazing things with Jinja/Python/YAML to really make HA sing.

So you learn YAML (which is easy) to figure out how to write your template line in Jinja that is a wrapper of Python. As you read these forums you’ll pick up on some tips, like with what you posted:

    chips:
      - type: template
        content: '{{ state_attr(''weather.accuweather'', ''temperature'') }} °F'
        icon: mdi:thermometer

becomes

    chips:
      - type: template
        content: >-
           {{ state_attr('weather.accuweather', 'temperature') }} °F
        icon: mdi:thermometer

Which saves you the double single quote strings you had there, that >- and indent remove the need to put the entire thing in quotes (which forced you to double your quotes).

I recommend started just like you have, post your code and let someone answer it for you so you start to understand why something was rewritten to work better for you. There’s no way I can name them all but users like @petro and @123 and @NathanCu and too many others to name really add so much to this site and to your continued learning.

Other than that there are more YouTube videos and sites than I could even count that go over this stuff, but having a bit of core knowledge, mostly from reading the HA Scripting Docs, will go a long way.

1 Like

Thank you @CO_4X4. I do spend hours and hours on this forum, HA docs as well as watching youtubes on HA and I have learned a lot but there is just a huge hole in what I’ve learned when it comes to data types and how to use them. I feel this is a very basic/important skill that I need to fill. Everyone on here and HA reddit have been extremely helpful and if I have a problem after reading up on something then I do post and ask for help. I will check out the links that you gave me - thank you.

Now discord is a different story. One of Lewis’ youtubes suggested using discord so I created an account. After signing up, I incorrectly thought discord wasn’t going to be helpful so I deleted my account. Now I can’t get back in and I’m not sure who or how to contact someone about my problem - any ideas?

You can just create another Discord account, I have three myself for different purposes. Once there you can join the Discord channel for HA where folks there are very helpful.

I was able to learn enough today to get this working but if any one else has some really basic tutorial sources I’m all ears. I have a lot more to learn.

The first thing to remember is that all states are always strings. Always.

Even if it looks like something else it’s a string. Always.

In your first example above the state of the sensor (sensor.krdg_temperature) is a string because again all states are strings.

There was a big change a while back that forced all attributes into their native data types by the way they “looked”. For example if the data looks like a float then it will be a float. There was a PR that explains that change here.

I know that there are some limited built-in tests to find out if the data type is a particular type but I can’t seem to find where that is in the docs.

so in your second example the template will render into whatever data type the resulting value is.

if for example {{ state_attr(’‘weather.accuweather’’, ‘‘temperature’’) }} returns 100, it’s an integer. if it returns 100.00, it’s a float.

I know it’s not likely in this example but say the result is [0,4,2,5]. that will be a list.

etc.

you can find the other examples in the PR linked above.

Unfortunately I don’t think there is an easy test for everything. you just either have to know by experience or use trial and error in the template editor under developers tools. At list having the list of available types and how they look gives you a step toward finding what they are.

@finity this really has me confused. The first time I read this I took it that all underlying data is a string but depending on how it’s represented it could be something else.

Even if it looks like something else it’s a string. Always.

This is the part that really has me confused because as you say later

so in your second example the template will render into whatever data type the resulting value is.

so not everything is a string. When I test different things in developer tools I’m told what type these items are and they aren’t all strings. I’m not sure I understand this at all.

The important part to take from that is the “states” part.

if you look in developers tools at the entity list there is a column called “state”. that will always be a string. even if it looks like something else.

and if you call the “states()” method in a template it will always be a string internally to the template.

in order for it to be something else (for example a float, integer, list etc) you need to convert that in the template to the data type needed.

for example I want to add an integer to the value of the state of “sensor.some_sensor”. Let’s say that the value of the sensor is 5 as shown in the states column. If I use it directly like this:

{{ states('sensor.some_sensor') + 12 }}

i’ll get an error saying that I can’t perform the addition to a string. It’s a data type mismatch.

however if I convert it to an integer it works fine:

{{ states('sensor.some_sensor') | int + 12 }}

but if I use it as string and do a concatenation it works fine as well:

{{ states('sensor.some_sensor') ~ 12 }}

the result of the above will be 512

but attributes are whatever data type they look like.

so in this example you want to add to an attribute of sensor.some_sensor. Let’s say the attribute is called “data” and it’s value is “10.3” . now the addition works fine without a type conversion because it’s data type is already a float because it looks like a float.

{{ state_attr('sensor.some_sensor', 'data') + 12}}

it returns 22.3.

@finity that helps SO much. Now I do understand. Thanks for taking the time to explain this to me.

1 Like

interestingly in 2023.11 there was just a newly added set of functions to test what the data type is of more complex data types.

I still can’t find the docs on the more basic data types tho.

I haven’t updated yet (and usually don’t for a while) so I won’t be testing it yet.

Thanks again - that will that a while to digest.

And two more…

I finally found the tests I was looking for. It wasn’t as thorough as I thought since it only allows for checking if the data type is a number or if it has a value (not unknown or unavailable). I thought I remembered more but it looks like I was just ahead of my time since they just added others. :wink:

is_number

The numeric functions and filters raise an error if the input is not a valid number, optionally a default value can be specified which will be returned instead. The is_number function and filter can be used to check if a value is a valid number.

last bullet point in here

has_value('sensor.my_sensor') will test if the given entity is not unknown or unavailable. Can be used as a filter or a test.

Thanks again @finity. I’m actually using the has_value already. All of the tests that I did to check if Mochad was functioning weren’t working so I decided to check if some of my entities were alive. has_value I learned would give me that answer. I’m just learning so many of these things piece-meal - that’s why I thought it was time to bite the bullet and learn about data types, tests and conversions and hopefully save myself a lot of time and frustration in the future.

1 Like