Issue Accessing Data from Event

Hello!

I am trying to access the “description” field when creating a new to-do list item. The event looks like this:

event_type: call_service
data:
  domain: todo
  service: add_item
  service_data:
    item: testing green
    description: red
    entity_id:
      - todo.home_to_do_list
origin: LOCAL
time_fired: "2024-07-07T17:28:47.661354+00:00"
context:
  id: 01J273VDSCD969GX5SJN0E73CT
  parent_id: null
  user_id: 9b26344dab61489fb8e0bd30f11c2826

My automation is:

alias: to do list description transfer test
description: ""
trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: todo
      service: update_item
      service_data:
        status: completed
    variables:
      list_name: >
        {{ state_attr((trigger.event.data.service_data.entity_id)[0],
        'friendly_name') }}
      task: "{{ trigger.event.data.service_data.rename }}"
      description: "{{ trigger.event.data.service_data.description }}"
condition: []
action:
  - service: notify.mobile_app_pixel_5
    metadata: {}
    data:
      message: "{{description}}"
    enabled: true
mode: single

The list_name and task variables work but the description doesn’t grab any data.

I have tried reproducing in the template editor based on the instructions found here: Help accessing trigger data from service_call - #4 by 123
and I am able to get it to work. See:

{% set trigger = { "event":
{
    "event_type": "call_service",
    "data": {
        "domain": "shopping_list",
        "service": "add_item",
        "service_data": {
            "name": "***retrieve this name***",
            "description": "testing"
        }
    },
    "origin": "LOCAL",
    "time_fired": "2022-01-24T20:45:43.162198+00:00"
}
} %}

{{ trigger.event.data.service_data.description }}

What am I doing wrong in my automation that I am doing in the template that I can grab the description?

What exactly are you trying to do?
You stated:

But then your trigger is limited to updates to completed tasks… for a new task you need to listen for the add_item service (which is shown in both of the event examples you supplied):

trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: todo
      service: add_item

Wow you are 100% right, I was looking at the wrong event. I am trying to grab the description of to-do list items that are marked as complete not items that are added.
The description isn’t an available data point for update_item service. Any idea how to grab it? This is the full automation I was going to incorporate this into.
The idea is that once a week my wife and I have to select a task from our long term to do list which will move over to more visible list. I have another automation which checks to ensure we did this task by the middle of the week otherwise the other person is notified. I would like to move the description of the item in addition to the name.

alias: "To-do list transfer "
description: ""
trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: todo
      service: update_item
      service_data:
        status: completed
    variables:
      list_name: >
        {{ state_attr((trigger.event.data.service_data.entity_id)[0],
        'friendly_name') }}
      task: "{{ trigger.event.data.service_data.rename }}"
condition: []
action:
  - if:
      - condition: template
        value_template: "{{list_name == 'Dan Long Term List'}}"
        enabled: true
      - condition: state
        entity_id: input_boolean.dan_s_weekly_long_term_task_status
        state: "on"
    then:
      - service: todo.add_item
        metadata: {}
        data:
          item: "{{task}}"
        target:
          entity_id: todo.long_term_to_do
      - service: todo.remove_completed_items
        metadata: {}
        data: {}
        target:
          entity_id: todo.long_term_to_do
        enabled: true
    enabled: true
    else: []
  - if:
      - condition: template
        value_template: "{{list_name == 'Dan Long Term List'}}"
        enabled: true
      - condition: state
        entity_id: input_boolean.dan_s_weekly_long_term_task_status
        state: "off"
    then:
      - service: input_text.set_value
        metadata: {}
        data:
          value: "{{task}}"
        target:
          entity_id: input_text.dan_s_weekly_long_term_task
      - service: todo.add_item
        metadata: {}
        data:
          item: "{{task}}"
        target:
          entity_id: todo.dan_s_long_term_task_for_the_week
      - service: todo.remove_completed_items
        metadata: {}
        data: {}
        target:
          entity_id: todo.long_term_to_do
        enabled: true
      - service: input_boolean.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: input_boolean.dan_s_weekly_long_term_task_status
    enabled: true
  - if:
      - condition: template
        value_template: "{{list_name == 'Emily Long Term List'}}"
        enabled: true
      - condition: state
        entity_id: input_boolean.emily_s_weekly_long_term_task_status
        state: "on"
    then:
      - service: todo.add_item
        metadata: {}
        data:
          item: "{{task}}"
        target:
          entity_id: todo.emily_long
      - service: todo.remove_completed_items
        metadata: {}
        data: {}
        enabled: true
        target:
          entity_id: todo.emily_long
    enabled: true
    else: []
  - if:
      - condition: template
        value_template: "{{list_name == 'Emily Long Term List'}}"
        enabled: true
      - condition: state
        entity_id: input_boolean.emily_s_weekly_long_term_task_status
        state: "off"
    then:
      - service: input_text.set_value
        metadata: {}
        data:
          value: "{{task}}"
        target:
          entity_id: input_text.emilys_weekly_long_term_task
      - service: todo.add_item
        metadata: {}
        data:
          item: "{{task}}"
        target:
          entity_id: todo.emily_s_long_term_task_for_the_week
      - service: todo.remove_completed_items
        metadata: {}
        data: {}
        enabled: true
        target:
          entity_id: todo.emily_long
      - service: input_boolean.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: input_boolean.emily_s_weekly_long_term_task_status
    enabled: true
  - repeat:
      count: 2
      sequence:
        - service: todo.remove_completed_items
          metadata: {}
          data: {}
          enabled: true
          target:
            entity_id:
              - todo.emily_long
              - todo.long_term_to_do
mode: single

The description is available in the service_data property of the trigger you are using.

If these are tasks that need to be done, why are you specifically listening for events involving completed tasks?

The way you have used If/Then actions is making the automation inefficient. I would structure it as a single If/Then/Else and use templates to supply the target entities based on the active person. Something like the following:

Automation Configuration
alias: "To-do list transfer"
description: ""
trigger:
  - alias: Trigger on updates to completed to-dos
    platform: event
    event_type: call_service
    event_data:
      domain: todo
      service: update_item
      service_data:
        status: completed
    variables:
      list_name:  |
        {% set ent = trigger.event.data.service_data.entity_id[0] %}
        {{ state_attr(ent, 'friendly_name') }}
condition:
  - alias: Only run if the list that was edited was one of the Long Term lists
    condition: template
    value_template: "{{ list_name in ['Dan Long Term List', 'Emily Long Term List']}}"
action:
  - variables:
      task: "{{ trigger.event.data.service_data.rename }}"
      description: "{{ trigger.event.data.service_data.description }}"
      active: "{{ list_name.split(' ')[0] | lower }}"
      boolean: "input_boolean.{{ active }}_s_weekly_long_term_task_status"
      text_input: "input_text.{{ active }}_s_weekly_long_term_task"
      weekly_todo: "todo.{{ active }}_s_long_term_task_for_the_week"
      todo_long: 
        dan: todo.long_term_to_do
        emily: todo.emily_long
  - if:
      - condition: template
        value_template: "{{ is_state(boolean, 'on') }}"
    then:
      - service: todo.add_item
        metadata: {}
        data:
          item: "{{ task }}"
          description: "{{ description }}"
        target:
          entity_id: "{{ todo_long[active] }}"
      - service: todo.remove_completed_items
        metadata: {}
        data: {}
        target:
          entity_id: "{{ todo_long[active] }}"
    else:
      - condition: template
        value_template: "{{ is_state(boolean, 'off') }}"
      - service: input_text.set_value
        metadata: {}
        data:
          value: "{{ task }}"
        target:
          entity_id: "{{ text_input }}"
      - service: todo.add_item
        metadata: {}
        data:
          item: "{{ task }}"
        target:
          entity_id: "{{ weekly_todo }}"
      - service: todo.remove_completed_items
        metadata: {}
        data: {}
        target:
          entity_id: "{{ todo_long[active] }}"
        enabled: true
      - service: input_boolean.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: "{{ boolean }}"
  - repeat:
      count: 2
      sequence:
        - service: todo.remove_completed_items
          metadata: {}
          data: {}
          enabled: true
          target:
            entity_id:
              - todo.emily_long
              - todo.long_term_to_do
mode: single

How are you planning on keeping it from running the actions when it is not supposed to? As configured, there are many editing acts that will trigger the automation and you will end up with a bunch of seemingly random items added to your to do lists. I have added one condition in the automation above, but you are going to have to think about what options you can harness for conditional logic.

I tried listening to the event but the description isn’t showing up (below) nor is anything returned when I try using “{{ trigger.event.data.service_data.description }}”

event_type: call_service
data:
  domain: todo
  service: update_item
  service_data:
    item: 5e4581ea-3c86-11ef-bd30-dca632970fc3
    rename: testing green
    status: completed
    entity_id:
      - todo.home_to_do_list
origin: LOCAL
time_fired: "2024-07-09T02:05:10.176247+00:00"
context:
  id: 01J2AKSN4ZRJQ03J2W2QPXZMW9
  parent_id: null
  user_id: 9b26344dab61489fb8e0bd30f11c2826

Thank you for the advice on making the automation more efficient! I am just starting to learn how to use variables and I am taking notes. What does the split(' ') of active: "{{ list_name.split(' ')[0] | lower }}" do?

I am not worried about this, the fact that it is only triggered by the longer-term to-do lists already limits the triggers, I also have added a condition so that it only triggers if the item is selected through the use of one of my displays.

I don’t know what to tell you… I can’t recreate what you are seeing.

This is the automation I am using for testing:

ToDo update
alias: zTest ToDo transfer
description: ""
trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: todo
      service: update_item
      service_data:
        status: completed
    variables:
      list_name: |
        {% set ent = trigger.event.data.service_data.entity_id[0] %}
        {{ state_attr(ent, 'friendly_name') }}
condition:
  - alias: Only run if the list that was edited was one of the specified lists
    condition: template
    value_template: "{{ list_name in ['Chores']}}"
action:
  - variables:
      task: "{{ trigger.event.data.service_data.rename }}"
      description: "{{ trigger.event.data.service_data.description }}"
  - service: persistent_notification.create
    metadata: {}
    data:
      message: |-
        List: {{ list_name }}
        Task: {{ task }}
        Description: {{ description }}
mode: single

I’ve tested against a bunch of different items and the only time description isn’t defined is when the item originally didn’t have a description and it is checked off as completed (the bottom entry in the image below). But if a description is added or modified at any time the description variable is defined.

It turns a space-separated sentence/phrase string into a list of word strings. It is one of the basic Python strings methods available in HA’s implementation of Jinja templating.

I tried listening to a bunch of variants of the update_item event and only when you change the item’s status does the description not show otherwise all other changes returns a description.

Changing name, description or due date:

event_type: call_service
data:
  domain: todo
  service: update_item
  service_data:
    item: c2754006-3e4d-11ef-bd30-dca632970fc3
    rename: testing
    status: needs_action
    description: now
    due_date: null
    entity_id:
      - todo.home_to_do_list
origin: LOCAL
time_fired: "2024-07-09T23:48:49.327841+00:00"
context:
  id: 01J2CYCQ1A86RN688F9ZQ41A2K
  parent_id: null
  user_id: 9b26344dab61489fb8e0bd30f11c2826

Marking as complete:

event_type: call_service
data:
  domain: todo
  service: update_item
  service_data:
    item: c2754006-3e4d-11ef-bd30-dca632970fc3
    rename: testing
    status: completed
    entity_id:
      - todo.home_to_do_list
origin: LOCAL
time_fired: "2024-07-09T23:48:52.804009+00:00"
context:
  id: 01J2CYCTE3P7PF9WB8NXWKPJJQ
  parent_id: null
  user_id: 9b26344dab61489fb8e0bd30f11c2826

Making as needs action:

event_type: call_service
data:
  domain: todo
  service: update_item
  service_data:
    item: c2754006-3e4d-11ef-bd30-dca632970fc3
    rename: testing
    status: needs_action
    entity_id:
      - todo.home_to_do_list
origin: LOCAL
time_fired: "2024-07-09T23:54:46.115585+00:00"
context:
  id: 01J2CYQKF2TJ4R96N4R1QJZ429
  parent_id: null
  user_id: 9b26344dab61489fb8e0bd30f11c282

If you can also confirm this then maybe I might make a feature request or something