Weather value only if greater than zero

Hi, I created the template sensor

- trigger:
    - platform: time_pattern
      hours: /1
  action:
    - service: weather.get_forecasts
      data:
        type: hourly
      target:
        entity_id: weather.openweathermap
      response_variable: hourly
  sensor:
    - name: Home Weather Forecast Hourly
      unique_id: f257b8b3-1cee-4130-9dd6-158a1e8f13da
      state: "{{ now().isoformat() }}"
      attributes:
        forecast: "{{ hourly['weather.openweathermap'].forecast }}"

And an automation

service: telegram_bot.send_message
data:
  message: >-
    Outside temperature is {{
    states.weather.openweathermap.attributes.temperature }} °C.

    The raining chances are:  {{- '\n' -}} At {{
    state_attr('sensor.home_weather_forecast_hourly', 'forecast')[0].datetime |
    as_timestamp | timestamp_custom('%H:%M') }} - {{
    state_attr('sensor.home_weather_forecast_hourly',
    'forecast')[0].precipitation_probability }}%  {{- '\n' -}} At {{
    state_attr('sensor.home_weather_forecast_hourly', 'forecast')[1].datetime |
    as_timestamp | timestamp_custom('%H:%M') }} - {{
    state_attr('sensor.home_weather_forecast_hourly',
    'forecast')[1].precipitation_probability }}%  {{- '\n' -}} At {{
    state_attr('sensor.home_weather_forecast_hourly', 'forecast')[2].datetime |
    as_timestamp | timestamp_custom('%H:%M') }} - {{
    state_attr('sensor.home_weather_forecast_hourly',
    'forecast')[2].precipitation_probability }}%  {{- '\n' -}} At {{
    state_attr('sensor.home_weather_forecast_hourly', 'forecast')[3].datetime |
    as_timestamp | timestamp_custom('%H:%M') }} - {{
    state_attr('sensor.home_weather_forecast_hourly',
    'forecast')[3].precipitation_probability }}%  {{- '\n' -}} At {{
    state_attr('sensor.home_weather_forecast_hourly', 'forecast')[4].datetime |
    as_timestamp | timestamp_custom('%H:%M') }} - {{
    state_attr('sensor.home_weather_forecast_hourly',
    'forecast')[4].precipitation_probability }}%  {{- '\n' -}} At {{
    state_attr('sensor.home_weather_forecast_hourly', 'forecast')[5].datetime |
    as_timestamp | timestamp_custom('%H:%M') }} - {{
    state_attr('sensor.home_weather_forecast_hourly',
    'forecast')[5].precipitation_probability }}%  {{- '\n' -}} At {{
    state_attr('sensor.home_weather_forecast_hourly', 'forecast')[6].datetime |
    as_timestamp | timestamp_custom('%H:%M') }} - {{
    state_attr('sensor.home_weather_forecast_hourly',
    'forecast')[6].precipitation_probability }}%  {{- '\n' -}} At {{
    state_attr('sensor.home_weather_forecast_hourly', 'forecast')[7].datetime |
    as_timestamp | timestamp_custom('%H:%M') }} - {{
    state_attr('sensor.home_weather_forecast_hourly',
    'forecast')[7].precipitation_probability }}%  {{- '\n' -}} At {{
    state_attr('sensor.home_weather_forecast_hourly', 'forecast')[8].datetime |
    as_timestamp | timestamp_custom('%H:%M') }} - {{
    state_attr('sensor.home_weather_forecast_hourly',
    'forecast')[8].precipitation_probability }}%

Where to input an if statement to receive only the lines if precipitation_probability is greater than zero and if all values are zero, to not receive at all?

You can use variables and a loop to reduce repetitive code.

The following will always return the temperature summary, but will only return the “raining chances” statement and time/% values if there are forecasts in the next 8 hours that include precipitation probability over 0%:

service: telegram_bot.send_message
data:
  message: |
    Outside temperature is {{ state_attr('weather.openweathermap','temperature') }} °C.
    {% set forecasts = state_attr('sensor.home_weather_forecast_hourly', 'forecast')[:8] %}
    {% for f in forecasts if f.precipitation_probability > 0 %}
      {%- if loop.index == 1 %}
        The raining chances are:  {{- '\n' -}}
      {% endif %}
      At {{ as_local(as_datetime(f.datetime)).strftime('%H:%M') }} - {{ f.precipitation_probability }}%
    {%- endfor %}
1 Like

Thanks, I get error when I trying to save automation

Missed a “)” after
Outside temperature is {{ state_attr(‘weather.openweathermap’,‘temperature’

1 Like