Changing states of text_input from a script

I’m trying to write a script that will change the value of text_input, but it doesnt seem to do anything when I run it.

my script:

my_script:
  alias: Do a thing
  sequence:
    - service: input_text.select_value
      data_template:
        entity_id: input_text.mytextbox
        value: 1234

my text_input:

input_text:
  mytextbox:
    name: Text Box

Activating the script does not give any errors, but also does not change the state of mytextbox. Im new to HA and Im sure Im missing something.

I don’t think there is ‘select_value’ service call for ‘input_text’. Try ‘set_value’.

You can find the services that are available for each domain using ‘Services’ under ‘Developer Tools’.

image

worked perfect, Thanks!

For anyone else reading this…
Ensure the length of the string value being set is smaller or equal to the max length of the input_text (if set). Longer strings throw an error in the log and thus don’t change the input_text value.

Example:
configuration.yaml:

input_text:
  lcd1:
    name: LCD1
    max: 2
  lcd2:
    name: LCD2
    max: 2
  lcd3:
    name: LCD3
    max: 2
  lcd4:
    name: LCD4
    max: 2

If you assume we have a variable ‘message’ with a value of “Hello”.
in scripts.yaml:
This calls the script tw_announce passing the variable ‘message’

      - service: script.turn_on
        entity_id: script.tw_announce
        data_template:
          variables:
            message: 'Target temperature now set to {{ states.climate.living_room.attributes.temperature }},'
  tw_announce:
    alias: "Announcement"
    sequence:
    - service: input_text.set_value
      data_template:
        entity_id: input_text.lcd4
        value: '{{message}}'

This will not set the value of input_text.lcd4 because the value of message exceeds 2 characters.

Is there a way to set the input text value to empty? I want to clear it after it is being set.

Looking for the same…

I did figure out a work around but it only works with single word text input:

The script to clear the contents instead replaces it with a single space.

  - service: input_text.set_value
    data:
      value: ' ' 
    target:
      entity_id: input_text.some_text

I leave that leading space while updating any text to the input_text.some_text.

Using the Developer Tools > Template you see that the space is removed using the Python “replace” method.

{{ states.input_text.some_text['state'].replace(' ', '') }}

I tried to use the Python “trim” method to remove any leading and training whitespace but I could not get it to work in the Template tool.

@eyalco @afishe2000

I was doing the same and for me this works

service: input_text.set_value
target:
  entity_id: input_text.some_text
data:
  value: ''

My problem now is with the ui. I want to check if the text is empty in a conditional card. But seems there is no way to make it works.

make a template binary_sensor

template:
- binary_sensor:
  - name: some_text empty
    state: "{{ is_state('input_text.some_text', '') }}"

Then use that in the conditional card using on or off.

I didn’t realize that.

thanks petro, it works perfect.

Hi, i got this also working with the white space. Any idea how i can make this as empty value?

Setting the input text to blank has recently stopped working for me. The below script was working fine for me for many months.

script:
  clear_fields:
    alias: Clear Input Text Fields
    mode: single
    sequence:
      - service: input_text.set_value
        target:
          entity_id: input_text.user_id
        data:
          value: ""
      - service: input_text.set_value
        target:
          entity_id: input_text.user_pin
        data:
          value: ""
1 Like

Working perfectly fine here. Are you sure the entity isn’t changing the value to nothing? Don’t look at the entity on your overview page as that can be affected by caching. Look at it in the developer tools → states page.

I’m seeing this too - I reported it on the release blog, but no-one seemed to pick up on it, or be seeing it.

As I said there, nothing in the logs, no change in the state in the Developer Tools → States page. Can still set the empty value via the frontend, just not via a service call.

As a workaround, to empty an input text, I now set the value to a single space " ", and return the value in scripts and automations with the jinja trim filter, which makes it at an empty string if it is only a single space.

Are you testing this in the service caller?

Yep, and via a script too. Believe me, I spent quite some time testing it from all angles, but haven’t posted a GitHub issue, as it seemed I was the only one.

And you see nothing in the logs, yet you get a “this worked” check when you press the call service button?

Yes, the new green check mark comes up, but with no change in the input text.

It also produces the red cross error (and reports an error in the log) when I try ‘null’ ‘none’ or ‘[]’, I guess because these are not string values.

EDIT: To be very precise, I only tested this on 2022.4.0, as I’ve been happy enough with the workaround to not investigate it any further.

Do you have a min value set on the input_text?

I tried it on input texts with min value set, and one’s without min value set, but with no difference that I could observe.

The documentation suggests that min_value defaults to 0 if you don’t set it, but I did try setting it explciitly to 0 anyway.