How to pass input value as service param

Hello,

im trying to pass the value of an input to a service.

In developer tools > template I tested {{ states.input_text.myinput }} and it returns the value of the Input correctly.
In developer tools > services I tested passing {{ states.input_text.myinput }} as a parameter for a service which expects a string value and I am getting the following error:
Value should be a string for dictionary value data.

What am I doing wrong?

I don’t believe you can pass template values when using the Services tab in the developer tools. You can however use template values in your lovelace cards and automations.

I tried to create a button with the following code

type: button
tap_action:
  action: call-service
  service: microsoft_todo.new_task
  service_data:
    subject: {{ states.input_text.newtask}}
icon: 'hass:plus'
icon_height: 25px

but it does not work. Directly after saving the code turns to

type: button
tap_action:
  action: call-service
  service: microsoft_todo.new_task
  service_data:
    subject:
      '[object Object]': null
icon: 'hass:plus'
icon_height: 25px

so i get the same error.

Whoops, you are right about that. I can think of two options. The first is to have the button call a script then have the script perform the templating. See https://community.home-assistant.io/t/how-to-use-service-template-with-tap-action

The 2nd option is to use a custom card for that.

- type: "custom:card-templater"
  entities:
    - input_text.newtask
  card:
    type: button
    tap_action:
      action: call-service
      service: microsoft_todo.new_task
      service_data:
        subject_template: {{ states.input_text.newtask}}
    icon: 'hass:plus'
    icon_height: 25px

I’m not familiar with scripts so I tried the custom cards but that doesn’t work for me.

It turns to

subject: '[object Object]': null

too.

Also tried to create a sensor for input_text and pass the sensor to the service_data. It’s not working too.

I just noticed this when rereading your post:

Directly after saving the code turns to

Where are you editing your card configuration? In the lovelace dashboard? Or via yaml mode for lovelace in an editor?

Unrelated but the docs recommend that you don’t reference states directly and use the states function instead. e.g.

subject_template: >-
  {{ states('input_text.newtask') }}
2 Likes

Got it to work!

Here is my code:

type: 'custom:layout-card'
layout: vertical
cards:
  - type: entity
    entity: sensor.todo_all_tasks
    icon: 'hass:shopping'
  - type: 'custom:card-templater'
    entities:
      - input_text.new_task
    card:
      type: button
      tap_action:
        action: call-service
        service: microsoft_todo.new_task
        service_data:
          subject_template: '{{ states(''input_text.new_task'') }}'
      icon: 'hass:plus'
      icon_height: 25px

In the docs of the todo integration, there is only “subject” but “subject_template” did the trick. Thanks @fahr :slight_smile:

Celebrated too early :frowning:

The values arriving in microsoft todo, are not up to date.

Test 1:

  • Fill input with value “Test 1”
  • Klick button
  • value in microsoft todo: “Test 1”

Test 2:

  • Fill input with value “Test 2”
  • Klick button
  • value in microsoft todo: “Test 1”

Test 3:

  • Fill input with value “Test 3”
  • Klick button
  • value in microsoft todo: “Test 2”

This is not a problem with yaml code in cards, it is a problem with the microsoft todo integration, isn’t it? Any ideas to fix this?

In the docs of the todo integration, there is only “subject” but “subject_template” did the trick.

The integration is documented correctly, it’s the custom card that allows you to template any field by adding a _template suffix to it’s name. See the card documentation for more info: GitHub - gadgetchnnel/lovelace-card-templater: Custom Lovelace card which allows Jinja2 templates to be applied to other cards

1 Like

I’ll need a little more context, where are you changing the value of the input_text.new_task ? Is this in another card? If so, could you share that?

It’s all inside a layout-card:

type: 'custom:layout-card'
layout: vertical
cards:
  - type: entity
    entity: sensor.todo_all_tasks
    icon: 'hass:shopping'
  - type: entities
    entities:
      - input_text.new_task
  - type: 'custom:card-templater'
    entities:
      - input_text.new_task
    card:
      type: button
      tap_action:
        action: call-service
        service: microsoft_todo.new_task
        service_data:
          subject_template: '{{ states(''input_text.new_task'') }}'
      icon: 'hass:plus'
      icon_height: 25px

sensor.todo_all_tasks is defined in configuration.yaml:

sensor:
  - platform: template
    sensors:
      todo_all_tasks:
        friendly_name_template: "Shopping list"
        value_template: > 
            {% for task in states.calendar.einkauftest.attributes['all_tasks'] -%}
            - {{ task }} {{ '\n' }}
            {%- endfor %}

Everything you have setup looks correct. It must have something to do with how the card-templater works as I experience the same issue. A workaround is to use a script.

lovelace:

type: 'custom:layout-card'
layout: vertical
cards:
  - type: entity
    entity: sensor.todo_all_tasks
    icon: 'hass:shopping'
  - type: entities
    entities:
      - input_text.new_task
  - type: button
    tap_action:
      action: call-service
      service: script.add_microsoft_todo_task
    icon: 'hass:plus'
    icon_height: 25px

script:

add_microsoft_todo_task:
  alias: Add Microsoft todo Task
  sequence:
  - service: microsoft_todo.new_task
    data:
      subject: > {{ states('input_text.new_task') }}

I verified this fixes the issue where the state was 1 character behind for your input_text.

With a script it seems to work, but my code looks slightly diferent:

lovelace:

type: 'custom:layout-card'
layout: vertical
cards:
  - type: entity
    entity: sensor.todo_all_tasks
    icon: 'hass:shopping'
  - type: entities
    entities:
      - input_text.new_task
  - type: button
    tap_action:
      action: call-service
      service: script.add_microsoft_todo_task
    icon: 'hass:plus'
    icon_height: 25px

script:

alias: add_microsoft_todo_task
sequence:
  - service: microsoft_todo.new_task
    data:
      subject: '{{ states(''input_text.new_task'') }}'
  - service: input_text.set_value
    data:
      value: ''
    entity_id: input_text.item

I have two more questions:

  1. As you can see, I added the input_text.set_value service to the script for clearing the input field. It works but takes very long time to empty the field. How can I speed this up?

  2. Currently my shopping list looks like this:


    How can I list the items vertically?

I’m not going to pretend to know how the internals of automations work, but I do have a guess. It doesn’t appear that the custom component makes asynchronous network calls when you create a new task. Since the automation is a sequence of steps, my guess is that it waits until the request is fired and the response is received which may take a few seconds. Then after the response is received it clears the input text. I’m pretty sure if the request was synchronous it would just proceed to the next step of clearing the input text after it fired off the request which would appear instant. This is just an educated guess, though.

Since the state is just a string, I don’t think the value you have there will show line breaks, even with your newlines. Perhaps add an attributes template to your sensor and store the raw array in an attribute that you can iterate over in a custom card. I’m sure there is some custom card in HACS that will allow this.