FL550 / dwd_weather

Deutscher Wetterdienst integration for Home-Assistant
MIT License
173 stars 11 forks source link

Rain forecast on dashboard #57

Open alanmilinovic opened 2 years ago

alanmilinovic commented 2 years ago

Hi, I would like to display on my dashboard when the next rain will start, what is the level (low, medium, strong) and when it will end. Is it possible with this integration? I saw there are precipitation sensors that one can play with, but didn't found any example of YML code that I can reuse.

something like this: Strong precipitation possible around 08:25 will end around 11:30.

Basically, I just want to know when the next rain will start and how long it will rain. If it is not that day maybe to display next date when the rain is forecasted.

FL550 commented 2 years ago

You can do this with this integration via the weather or precipitation sensor and a template sensor where you describe your logic.

alanmilinovic commented 2 years ago

Any tips how to do it? :)

nikipore commented 2 years ago

Yes ;-)

The following code interpolates the temperature forecast from the hourly temperature sensor:

template:
      - name: weather_forecast_temperature_interpolated
        state_class: measurement
        unit_of_measurement: "°C"
        state: >
          {% set temperature = state_attr('sensor.temperature_wiesbaden_biebrich_1h', 'data') %}
          {% set t = now() %}
          {% set index = ((as_timestamp(t) - as_timestamp(temperature[0]['datetime'])) // 3600) | int %}
          {% if index < 0 %}
            {% set result = temperature[0]['value'] %}
          {% else %}
            {% set result = temperature[index]['value'] + (temperature[index+1]['value'] - temperature[index]['value']) * t.minute / 60 %}
          {% endif %}
          {{ result | round(1) }}
        device_class: temperature

I hope you get the idea. You should study the Home Assistant template documentation, and then it should be straightforward to write your own template.

The hourly weather sensor should be your friend:

temperature: 24.4
humidity: 56
pressure: 1007.4
wind_bearing: 233
wind_speed: 14.8
visibility: 28.9
forecast: 
- datetime: '2022-06-24T00:00:00Z'
  condition: lightning-rainy
  temperature: 24
  templow: 18
  precipitation: 2.7
  wind_bearing: 245.57
  wind_speed: 4.12
  wind_gusts: 8.75
  precipitation_probability: 51
- datetime: '2022-06-25T00:00:00Z'
  condition: sunny
  temperature: 27
  templow: 15
  precipitation: 0
  wind_bearing: 198.46
  wind_speed: 3.09
  wind_gusts: 6.17
  precipitation_probability: 13
[...]

A possible (imperative) strategy would be to

  1. loop over the forecast attribute of the hourly weather sensor (which is an array),
  2. remember the datetime (and precipitation or condition) of the first entry with nonzero precipitation,
  3. remember the datetime of the last entry before the precipitation becomes zero,
  4. translate the timestamps to local time and render a result.

With advanced filters (like Python's takewhile) I would have implemented this in a functional way, but Jinja2 has very limited built-in filters, so I believe you'll have to live with imperative for loops (no, there is no break or while).

I could write the solution up, but you have to know what you want and you'll have to maintain it – "Give a man a fish, and you feed him for a day. Teach a man to fish, and you feed him for a lifetime."

alanmilinovic commented 2 years ago

Thank you for explanation. I get the idea. Let's see if I will have some time to play around with it.

FL550 commented 2 years ago

Maybe it's easier if this is implemented directly within the integration. There's more flexibility with python.

I cannot promise if I find time (and delight) the next days.

alanmilinovic commented 1 year ago

Maybe it's easier if this is implemented directly within the integration. There's more flexibility with python.

I cannot promise if I find time (and delight) the next days.

Hi, any news on this topic? Have you found some time to implement this? I think it would be super usefull and easier directly in python.

alanmilinovic commented 1 year ago

I think I found a solution. https://www.ajfriesen.com/rain-warning-sensor-with-home-assistant/

duczz commented 11 months ago

@FL550 i have also a question to the forecast? Where i can aktivate that feature, i dont have any forecast entity from your integration

FL550 commented 11 months ago

Please see issue #77.