#Hi. I am looking to setup a cast

1 messages · Page 1 of 1 (latest)

vocal zinc
#

I imagine I would need to have a trigger sensor and then pull some information from the trigger event (entity name probably) then add that entity name to the list. However when I need print out the string I will need to clear the sensor

#

Also how would I programatically build the string? I know individually how to for loop and set variables but not how to append strings etc

set string = "Hi, while you were out list[0]"
for list[1] to list.end
  string += ", and list[i]"
string += " finished"
return string
#
2024-02-11 15:41:01.555 WARNING (MainThread) [homeassistant.components.sensor.helpers] sensor.completed_tasks rendered invalid timestamp: ['entity_id', 'state', 'attributes', 'last_changed', 'last_updated', 'context', Undefined]
#
- trigger:
    - platform: state
      entity_id: vacuum.sl60d
      to: docked
      for:
        minutes: 1
    - platform: state
      entity_id: sensor.big_gulp
      from: "on"
      to: "off"
  sensor:
    - unique_id: completed_tasks
      name: "Completed Tasks"
      state: "{{ (this | list | default([])) + [trigger.to_state.attributes.name] }}"
      device_class: timestamp
      icon: mdi:check-circle
mystic carbon
vocal zinc
#

Why not just add completed jobs to a to-do list
I tried this and have it set up with a google task list

#

and then read them out on your return

Can I just read out all items in a todo list in the way I wanted to (somewhat conversational than just listing it)?

versed hedge
#

You can use the template sensor, but you'll have to make it empty again after the announcement.
I would suggest to create an automation which turns on an input boolean for each task finished.

Then, when you arrive home you send the announcement based on the input booleans which are on, and then turn them off again

mystic carbon
#

Yes there is a service to get the list back then you can just build a sentence from it, or look at the page I pointed you to. It has the read messages macro.

vocal zinc
mystic carbon
#

The documentation has examples. See response variables.

vocal zinc
#

So how do I build a string? II can't work out how to append

{% set s = "Welcome back, the list[0]" %}
{{ for item in list }} // How to skip 1?
  s += ", and item
{{ endfor }}
s += "has completed while you were away"
versed hedge
#

Just join them

Welcome, the {{ (list[:-1] | join(', ') ' and ' list[-1]) if list | count > 2 else  list | join(' and ') }} {{ 'is' if list | count == 1 else 'have' }} completed while you were away
vocal zinc
#

ok thank you, I think I get it now

#

I have a follow up while here, how would I gen a random number to select a random greeting for a list?

{% set greetings = ['Welcome back!', 'Welcome, ', 'Hi, ', ...] %}
{% set greeting = greetings[rand] %}
{{ greeting }} the {{ (list[:-1] | join(', ') ' and ' list[-1]) if list | count > 2 else  list | join(' and ') }} {{ 'is' if list | count == 1 else 'have' }} completed while you were away
versed hedge
#

greetings | random

vocal zinc
#

thats easy hahaha

#
action:
  - service: todo.get_items
    metadata: {}
    data:
      status: needs_action
    target:
      entity_id: todo.home_assistant_notifications
    response_variable: tasks
  - service: tts.speak
    metadata: {}
    data:
      cache: true
      media_player_entity_id: media_player.kitchen_assistant
      message: >
        {% set greetings = ['Welcome back! ', 'Welcome, ', 'Hi, '] %} {{
        (greetings | random) }} the {{ (tasks[:-1] | join(', ') ' and
        'tasks[-1]) if tasks | count > 2 else  tasks | join(' and ') }} {{ 'is'
        if tasks | count == 1 else 'have' }} completed while you were away
    target:
      entity_id: tts.google_en_com_au
#

so all up something like this should get the job done

#

Message malformed: template value should be a string for dictionary value @ data['action'][1]['data']

#

damn, no

#
{% set list = ["Item 1", "Item 2"] %}
{{ list }}
Welcome, the {{ (list[:-1] | join(', ') ' and ' list[-1]) if list | count > 2 else list | join(' and ') }} {{ 'was' if list | count == 1 else 'have' }} completed while you were away
#

Threw this into the dev tools to test it and errors with TemplateSyntaxError: expected token ')', got 'string but can't see the missing )

#

Seems to be the ' and '.
{{ (list[:-1] | join(', ') ' and ' list[-1]) }} errors but {{ (list[:-1] | join(', ')) }} doesn't

#

{{ (list[:-1] | join(', ') + ' and ' + list[-1]) }} looks like you need to + the strings

#
{% set greetings = ['Welcome back!', 'Welcome, ', 'Hi, '] %}
{{ greetings | random }} the {{ (list[:-1] | join(', ') + ' and ' + list[-1]) if list | count > 2 else  list | join(' and ') }} {{ 'is' if list | count == 1 else 'have' }} completed while you were away
#

hahahahaha,

service: todo.get_items
metadata: {}
data:
  status: needs_action
target:
  entity_id: todo.home_assistant_notifications
response_variable: tasks
#

set tasks to todo.home_assistant_notifications

#
todo.home_assistant_notifications:
  items:
    - summary: Item 2
      uid: TFBFTmtvTkJoMkZ3aTdzLQ
      status: needs_action
    - summary: Item 1
      uid: YmxacXNyU0tZb3Y3WmtUYw
      status: needs_action
#

dev tools shows it returning this

#

looks like I need to get the items from the dict
{% set list = tasks['todo.home_assistant_notifications']['items'] %} gives me everything still. How do I access the name only

#

{% set list = tasks['todo.home_assistant_notifications']['items']['summary'] %}

I'd guess this but isn't thereultiple summary

vocal zinc
#

Is there an easy way to test and visualise this sort of thing? I could work it out if I could see the data structure

mystic carbon
#

Just use a script and look at the traces. You can see the returned variable and then split out what you need.

vocal zinc
#

Ok. I'll use that to give it a test. While you're around, do you know how to pull the names off the top of your head?

mystic carbon
#

Something like

{% set list = tasks['todo.home_assistant_notifications']['items'] %}
{%for item in list %}
{{item.summary}}
{% endfor %}

I expect.

vocal zinc
#

I've never used scripts before, is there a way to just print to an output so I can see the steps. Like see what the list is, see what's in the list etc

versed hedge
#

A script is exactly the same as the action section of an automation. It supports the same actions and you can also view traces