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: