Extract precipitation from OpenWeatherMap

Hi,

I want to use the forecasted ‘precipitation’ and ‘probability of precipitation’ to make the sprinklers for my lawn smarter. I see other post about this topic, still I struggle. I feel I make a basic failure.

If I go to developer tools, in the template tab I see that my OWM integration pulls the forecast data via API.

Red boxes indicate I get values for ‘precipitation’ and ‘probability of precipitation’ in the forecast.

If I try to extract the individual data and display the values, I get “TRUE” for every field (forecasted day) even if values exist.
In the screenshot I try to extract
value 0, with value 0. Reported output is TRUE → expected TRUE → OK
value 1 with value 0.16. Reported output is TRUE → expected FALSE → NOT OK

My goal is to use it in code like this;

  - platform: template
    sensors:
      precipitation48h:
        friendly_name: "Rainy in next 48 Hours"
        value_template: >-
          {% set p0 = state_attr('weather.openweathermap', 'forecast[0].precipitation') == None %}
          {% set p1 = state_attr('weather.openweathermap', 'forecast[1].precipitation') == None %}
          {{ 'false' if p0 and p1 else 'true' }}

or even better maybe is to multiply the precipitation * precipitation probability. If the sum of this outcome for P0 and P1 is beyond a certain threshold I want to run a sprinkler cycle.

Q1: What am I doing wrong?

Q2: What code do I need to combine precipitation * precipitation probability?

Thanks!

Regards,
Jules

Take a look at the templating documentation, you shouldn’t mix like abc[0].xyz, instead use sth. like abc[0]['xyz']
IIRC a probability of 31% means that on 31% of the days with the same conditions it will rain.
So maybe you’ll want to run the sprinklers if the probability is below 60% and the precipitation is 0?
In which case, maybe something like this?

{{ (state_attr('weather.openweathermap','forecast')[0]['precipitation'] + state_attr('weather.openweathermap','forecast')[1]['precipitation'] > 0) and (state_attr('weather.openweathermap','forecast')[0]['precipitation_probability'] > 0.6 or state_attr('weather.openweathermap','forecast')[1]['precipitation_probability'] > 0.6) }

this is an interesting topic but i get this error when I test this template:

template value should be a string for dictionary value @ data[‘value_template’]. Got None

what am I doing wrong? :roll_eyes:

sorry, misread the output :slight_smile:

I’m just beginning to fool around with value templates and weather data. I’m a new HA user without much programming experience.

All I want to do is have a template that stores the current day precipitation from Open Weather Map. I want to use that value as a condition to water my garden or skip the following day.

If I run this action, I get the response list from OWM, including temperature, humidity, precipitation etc. etc. for the upcoming week. Other than ‘condition’ all the displayed values are numerical:

action: weather.get_forecasts
data:
  type: daily
target:
  entity_id: weather.openweathermap

If I test various fields in the template section in Developer Tools, many return a numerical value, which is what I would expect. For example, for pressure I get a number:

{{state_attr (‘weather.openweathermap’,‘pressure’)}}

The result type is “number” with a value. So far, so good.

If I test some fields that look like they have a numerical value, I get a result type of “dict” and a null value. For example, precipitation:

{{state_attr (‘weather.openweathermap’,‘precipitation’)}}

The result type is “dict” with a value of “null.” This despite the action response showing a value of .05 for the current day.

I’m using the One Call API 3.0, not sure if that makes a difference.

Is there a way to force the interpretation of the response value to be an integer or number with decimal places? Or are some attribute values simply not accessible?

Also, I’ve tried various formats to get a value from one or two days out. Nothing I’ve tried seems to work.

I think there’s something strange happening with Developer Tools.
This is OK:

But this isn’t:

That is bizarre…. Agreed, something is off with templates in developer tools.

What I’ve done since posting the original question is install the OWM History integration. I have no idea how it works, but it seems to expose a bunch of new entities that I hope to use for watering automation. So far, I don’t understand the entity values (for example, what is day 1 versus day 2 rain, etc. - the values seem the same, and it did show precipitation for a date/time where I know for sure it didn’t rain).

That said, I’ll also try your template example again the next time it rains or if forecast to rain to see what happens.