Openweathermap forecasts not general daily forecast

  1. I tried that. It just comes back with an unavailable value.
  2. I played with SoapUI and it gave me a headache lol

Hey @Emphyrio,

thanks for posting this. It helped me setup my sensors for daily rain with the one call API :slight_smile:

@lawsuitup - this should work for the description:

value_template: "{{ state_attr('sensor.openweather_report', 'daily')[0].weather[0].description }}

I included [0] after weather. You can easily find the path using https://jsonpathfinder.com

1 Like

Thanks! I’m definitely going to try that asap!

OP is happening to me as well. Does the API workaround posted above allow use of the weather card, or does it create a sensor input?

Any news on an official fix?

I am also searching for a solution to use the daily rain as a condition for an autmotion. Can you please post your code?

Hey @Visi,

this is what my sensor.yaml looks like:

- platform: rest
  name: openweather_report
  json_attributes:
    - current
    - daily
  value_template: "{{ value_json['current']['dt'] | timestamp_custom('%Y-%m-%d %H:%M', true) }}"
  resource: https://api.openweathermap.org/data/2.5/onecall?lat=XXX&lon=XXX&exclude=hourly,minutely&appid=XXX&units=metric&lang=de
  scan_interval: 3600
- platform: template
  sensors:
    openweather_rain_last_hour:
      value_template: > 
        {% if state_attr('sensor.openweather_report', 'current').rain is defined %}        
        {{ state_attr('sensor.openweather_report', 'current').rain['1h'] }}
        {% else %}
        {{ "0" }}
        {% endif %}      
      unit_of_measurement: 'mm'    
    openweather_rain_today:
      value_template: >
        {% if state_attr('sensor.openweather_report', 'daily')[0].rain is defined %}      
        {{ state_attr('sensor.openweather_report', 'daily')[0].rain }}
        {% else %}
        {{ "0" }}
        {% endif %} 
      unit_of_measurement: 'mm'
    openweather_rain_tomorrow:
      value_template: >
        {% if state_attr('sensor.openweather_report', 'daily')[1].rain is defined %}      
        {{ state_attr('sensor.openweather_report', 'daily')[1].rain }}
        {% else %}
        {{ "0" }}
        {% endif %} 
      unit_of_measurement: 'mm'

First sensor is for fetching the JSON payload from the rest interface. Make sure to adjust lat, lon and the API key.

The next three sensors extract the JSON info for rain for last hour, today and tomorrow and make it available as a value in mm. You can use these sensors in your automations then.

I pull new data once every hour.

3 Likes

Thank you for sharing your code. I get the Error:

  • Could not render template openweather_rain_today: UndefinedError: None has no element 0
  • Could not render template openweather_rain_tomorrow: UndefinedError: None has no element 1

Coud it be a n error because it will not rain today and tomorrow?

Hm - the template should actually be able to handle the case when there isn’t any rain.

Have you adjusted the lat, lon and API key values?
Is the openweather_report sensor defined? Can you check its state to make sure the JSON data is fetched correctly and written to the sensor?

1 Like

Was my fault. My already defined sensor name missmatching yours. Now it works! Thank you!

Hi @lawsuitup!
Would you be so kind to share the temperature sensors settings? I tried above code but no luck with current temperature… Daily works well :frowning:

Which ones?

I don’t think ‘rain’ is defined under ‘current’ so the value below is always 0.
Correct?

value_template: > 
        {% if state_attr('sensor.openweather_report', 'current').rain is defined %}        
        {{ state_attr('sensor.openweather_report', 'current').rain['1h'] }}
        {% else %}
        {{ "0" }}
        {% endif %}      
      unit_of_measurement: 'mm'    
1 Like

I haven’t looked at this for quite some time, but I think it is defined if there was rain in the last hour.
Works fine for me and is not always 0.

You are absolutely right! It’s not always 0.
It just shows me it’s not accurate at all for my location. :unamused:
It was raining a few hours but value kept 0.

Very nice! This is exactly what I have needed.
I want my heating regulator to adjust the right temperature depending on the temperature of the following day… Thank you very much!

Need help figuring this out:
I am trying to call the OpenWeather REST API to get rain probability (“pop”)
When I call the API directly from a browser, the “pop” values are returned correctly
However when I parse as shown below, the state of openweather_pop_today remains empty.
A different value such as “uvi” works fine
It almost seems to me that “pop” is not correctly processed by the yaml interpreter.
Is there any way to get this to work?

- platform: rest
  name: openweather_report
  json_attributes:
    - current
    - daily
    value_template: "{{ value_json['current']['dt'] | timestamp_custom('%Y-%m-%d %H:%M', true) }}"
    resource: https://api.openweathermap.org/data/2.5/onecall?lat=xLAT&lon=xLON&exclude=hourly,minutely&appid=xAPIkey&units=metric&lang=en
    scan_interval: 3600
- platform: template
  sensors:
    openweather_uvi_today:
      value_template: >
        {{ state_attr('sensor.openweather_report', 'daily')[0].uvi}}
    openweather_pop_today:
      value_template: >
        {{ state_attr('sensor.openweather_report', 'daily')[0].pop}}" 

I noticed the following warning in my core log:
2021-05-03 22:45:56 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: access to attribute ‘pop’ of ‘dict’ object is unsafe. when rendering ‘{{ state_attr(‘sensor.openweather_report’, ‘daily’)[0].pop }}’
So this seems to be an issue with homeassistant treating “pop” differently than any other of the values in the reply to the rest api call.

And now I am happy to provide the solution :slight_smile:
Instead of
state_attr(‘sensor.openweather_report’, ‘daily’)[0].pop
This specific attribute (reason unknown) can be correctly retrieved by
state_attr(‘sensor.openweather_report’, ‘daily’)[0].['pop']