#Announce Grocery/Task list

1 messages · Page 1 of 1 (latest)

crystal fulcrum
#

I really don't know where to start for this but I'd like to create an automation that tells me the items inside my grocery/task list when I trigger the all lights off automation I've hooked to a specific button.
The obvious meaning of this is to stop me from forgetting things home. Has anyone ever done something like this?

chilly hull
#

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.

crystal fulcrum
chilly hull
#

No it is jinja to add into the message part of an automation.

chilly hull
#

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: ""

crystal fulcrum
chilly hull
#

Yes, just call the script from your automation. I tend to use scripts for any code I might want to reuse.