#Announce Grocery/Task list
1 messages · Page 1 of 1 (latest)
You can use a template to pull all the values from your list into a string which you can use as content for your speaker connection.
{%- macro read_list(list,preamble,empty,item_prefix) -%}
{%- set count = list | count -%}
{%- if count > 0 -%}
{{preamble -}}{{' ' -}}
{%- for item in list -%}
{%- if loop.index == 1 %}{{- item_prefix}} {{ item -}}
{%- elif loop.index == count %} and {{item_prefix}} {{ item -}}
{%- else %},{{- item_prefix}} {{ item -}}{% endif -%}
{%- endfor -%}
{%- else -%}
{{- empty -}}
{%- endif -%}
{%- endmacro -%}
{%- macro read_shopping_list() -%}
{%- set list = state_attr('sensor.shopping_list','items') %}
{%- set ns = namespace(l=[]) %}
{%- for l in list %}
{%- set ns.l = ns.l + [l.message] %}
{%- endfor %}
{{read_list(ns.l,'','','')}}
{%- endmacro -%}
{{read_shopping_list()}}
I assume you already know how to send a string of text to your speaker.
I don't know how to send a message to the speaker using YML files, but I can recover that from other automations that I've set up using the GUI.
What you sent me is a blueprint, right?
No it is jinja to add into the message part of an automation.
I have written a complete script for you, but you will need to use the TTS option you have enabled on your system and your media player.
sequence:
- action: todo.get_items
metadata: {}
data:
status: needs_action
response_variable: list
target:
entity_id: todo.shopping_list
- variables:
message_list: >
{%- macro read_list(list,preamble,empty,item_prefix) -%}
{%- set count = list | count -%} {%- if count > 0 -%}
{{preamble -}}{{' ' -}}
{%- for item in list -%}
{%- if loop.index == 1 %}
{{- item_prefix}} {{ item -}}
{%- elif loop.index == count %}
and {{item_prefix}} {{ item -}}
{%- else %},{{- item_prefix}} {{ item -}}
{% endif -%}
{%- endfor -%}
{%- else -%} {{- empty -}} {%- endif -%}
{%- endmacro -%}
{{read_list(list,'Get from Shops','Nothing to get','')}}
- metadata: {}
data:
cache: true
media_player_entity_id: media_player.kitchen
message: "{{message_list}}"
target:
entity_id: tts.home_assistant_cloud
action: tts.speak
alias: Read Shopping List
mode: single
description: ""
Sorry for the very late reply. First thing first than you so much for having taken the time to write this script for me, I really appreciate it.
So this becomes a script, I will adapt all the parts like entity id of my lists, the media player id, target and so on. Then my automation will call this script and the device will hopefully start to tell me what's in my lists, correct?
Yes, just call the script from your automation. I tend to use scripts for any code I might want to reuse.