Making a Weather Message and setting values in a loop
Home Assistant
Inspired by Slacker Labs video. I thought I would enhance the morning message which plays in the kitchen and store the message for future use.
service: tts.cloud_say data: message: >- {% set value = namespace(value=0) %} {% set forecast = state_attr('weather.owm_weather', 'forecast')[0] %} {% set value.max = -30 %}{% set value.min = 100 %}{% set date = now().strftime('%Y-%m-%d') %} {%- for item in state_attr('weather.owm_weather', 'forecast') %} {%- if date in item.datetime %} {%- set temp = item.temperature|float(default=0) %} {%- if temp < value.min %}{% set value.min = temp%}{% endif %} {%- if temp > value.max %}{% set value.max = temp%}{% endif %} {%- endif%} {%- endfor %} Good Morning, todays forecast is {{ forecast.condition }}, {% if forecast.precipitation == 0 %} No rain is expected.{% else %} {{ forecast.precipitation }} mm of rain is expected.{% endif %} The maximum temperature is expected to be {{ value.max }} degrees c, with a low of {{value.min}} degrees c entity_id: media_player.kitchen
Note above the use of value = namespace in the first line of the message, this is because simple values set in loops will be reset at the end of the loop.
Setting Long values
The video states you can only include 256 characters in the sensor, but I found by using json fields you can store the longer message and other attributes in the attributes for the sensor.
So you can use something like
{ "title": "Short Title","message": "Lorem ipsum dolor sit amet. Aut repellat laborum id consequatur libero est corrupti rerum eos molestias dolorem hic libero porro qui nemo molestias! Sit sunt fuga qui earum tempora qui modi quidem in minus necessitatibus ea iste dolor harum quas! Sed culpa dolores et quia voluptatibus quo praesentium consequatur sit soluta molestiae aut impedit velit!\nSed excepturi repellendus et laudantium voluptatum id cupiditate reiciendis. In dolores quasi aut laborum a eos ullam voluptas aut quaerat eius quo ducimus fugit. Eos unde molestiae ea consectetur provident ab a culpa a nemo placeat! Ut magni debitis et reiciendis voluptatem est tempore porro et asperiores obcaecati et odio repudiandae.\nId sequi distinctio ut delectus corrupti non laudantium iste ut optio facilis aut internos expedita qui reiciendis pariatur. Est quidem suscipit quo excepturi harum qui similique praesentium. Quo numquam omnis ex voluptate tenetur qui praesentium aliquam hic voluptas vero. At laborum internos aut quia voluptatem sit laboriosam facere sit quisquam incidunt"}
as your message and then set up your MQTT sensor as
- name: "last_message" state_topic: "notification/voice" value_template: "{{ value_json.title }}" json_attributes_topic: "notification/voice"
The title will be in the main value for the sensor and the long message stored as an attribute.