I'm new with yaml and trying to do a simple IF

I’m trying to create a notifcation message depending on the temperature, but I can’t get my elif to work. What is wrong with this code?
Error message: Message malformed: template value should be a string for dictionary value @ data[‘action’][0][‘data’]

   service: notify.notify
data:
  title: notification title
  message: 
     {% set temp_now = 0 %}
     temp_now = {{state_attr ('weather.mycity_forecast','temperature')}} 
     forecast_msg = ""
     {% if  temp_now < -10 %} forecast_msg = "msg1"
     {% elif temp_now temp_now >-9 AND temp_now <5 %} forecast_msg = "msg2"
     {% elif temp_now >6 AND temp_now <20 %} forecast_msg = "msg3" 
     {% elif temp_now >20 AND temp_now <25 %} forecast_msg = "msg 4"
     {% else %} forecast_msg = "msg5" 
     {% endif %}
   
  "Forecast: " {{ states("weather.mycity_forecast")}}  " Temperature: "  temp_now °C."  forecast_msg
   

Thanks for helping a very newbie with yaml. :slight_smile:

You need to use set statements to bind the value to your variable and surround your variable expressions in {{}} in you final section. Also, you were missing the multi-line quote indicator.

service: notify.notify
data:
  title: notification title
  message: > 
     {% set temp_now = state_attr('weather.mycity_forecast','temperature')| float(0) %} 
     {% if  temp_now <= -10 %} 
       {% set forecast_msg = "msg1" %}
     {% elif  -9 < temp_now <= 5 %} 
       {% set forecast_msg = "msg2" %}
     {% elif 6 < temp_now <= 20 %} 
       {% set forecast_msg = "msg3" %}
     {% elif 20 < temp_now <= 25 %} 
       {% set forecast_msg = "msg 4" %}
     {% else %} 
       {% set forecast_msg = "msg5" %} 
     {% endif %}
       Forecast:  {{ states("weather.mycity_forecast") | title }}  
       Temperature:  {{temp_now}}°C.  {{forecast_msg}}

The float(0) added to the temp_now statement is likely unecessary, but it won’t hurt to have it if you are unsure whether the data type of the attribute is a string or float.

1 Like

Thanks! It works!