Map() with real numbers not strings

Hi,

I have the following sensor (which works fine):

- name: weather forecast wind
  unique_id: weather_forecast_wind
  state: "{{ state_attr('weather.openweathermap','wind_speed') }}"
  attributes:
    forecast_state: |
      {{
        (state_attr('weather.openweathermap', 'forecast')
        | map(attribute="wind_speed")
        | list)
      }}        
    forecast_time: |
      {{
        (state_attr('weather.openweathermap', 'forecast')
        | map(attribute="datetime")
        | list)
      }}        

It gives me a list of 48 readings for the wind speed forecast that I can plot (with another sensor for history) using Apexcharts to show a forecast and history…
Screenshot 2023-03-01 at 09.20.33

However, the results are strings in kph and I want them in mph (by multiplying by 0.62) but I cannot work out how to convert them to real numbers to make the calculation.

Can anyone advise? I have tried |float(0)*0.62 but keep getting errors or undefined.

You can cast the items in the list to the float() and multiply() functions using the map filter.

{{state_attr('weather.openweathermap', 'forecast')
| map(attribute="wind_speed")
| map("float")
| map("multiply", 0.62 )
| map('round', 1)
| list }}
1 Like

that’s awesome, thanks!

I have been trying to learn more about map() and didn’t realise that float would be inside it. Is there an idiots guide to where you can learn about these things? …I don’t find the ninja2 docs that easy to understand.

There’s no single source that covers all the things you can do with templates in HA, but make sure to check out:

Home Assistant Templating: This is the the most comprehensive source for HA-specific functions.

Jinja Docs: As you have discovered, these docs are not geared toward new/casual users, but they contain a lot of important information.

Jinja for Ninjas: @skalavala has put together a well organized tutorial and group of examples. It’s been a while since this was updated, so there are better/easier ways to do some of the things shown, but it’s still a good source.

Many Python methods also to work in templates. I’ve never seen a complete list of which methods work and which don’t… But the Official Python docs have come in handy for me multiple times to figure out what’s going on in templates posted by some of the more advanced users on this forum.

1 Like