Input_text matching - SOLVED

Hi,

I’ve spent some days on this and am really pulling my hair out trying to work out the syntax before finally throwing in the towel and signing up here for help. I’m pretty new to HA but am a programmer of many years in proprietary control systems (but not typical PC programming).

I have an external control system successfully changing the value of an input_text in HA, which I am using as a way for the external control system to control HA entities/devices.

In the HA LogBook, I get messages on change:
analog1 changed to “1” triggered by service input_text.set_value

I have created an automation, which triggers when analog1 changes. The trigger works, but I am having trouble matching the input_text to trigger actions.

What is stumping me is how to match the text value. I’ve tried for hours and hours but just can’t seem to find the correct syntax.

In my actions, I have this:

service: |
  {% if states('input_text.analog1') == "1" %}
    media_player.media_play
  {% else %}
    media_player.media_stop
  {% endif %} 
entity_id: media_player.dining_room

When a change is received (analog1 changed to “1” triggered by service input_text.set_value), it triggers stop. Any other value triggers stop as well, so I can never seem to get it to trigger play.

I have tried converting it to int(0), making all sorts of workarounds without success, and it just feels like I must be missing something very simple.

Really appreciate any help anyone can offer! Thank you in advance.

looks like your service call is not properly formatted.

Try this:

service: |
  {% if states('input_text.analog1') == "1" %}
    media_player.media_play
  {% else %}
    media_player.media_stop
  {% endif %}
target:
  entity_id: media_player.dining_room

But about the problem you described, you can go to Developer Tools and select the tab Template and try the different ways of doing this to see which one will return true:

{{ states('input_text.analog1') == "1" }}
{{ states('input_text.analog1') == 1 }}
{{ states('input_text.analog1') | int(0) == 1 }}
{{ is_state('input_text.analog1', '1') }}
{{ is_state('input_text.analog1', 1) }}

Thank you for the super fast reply!

I tried your syntax, but same outcome - it triggers stop no matter what value analog1 is.

I didn’t know about the developer tools tab template, that is really really helpful.

I haven’t put too much more thought into how to test this, but I can report back that I first set the value to 1, check the Logbook:
analog1 changed to “1” triggered by service input_text.set_value

Then with each line, I carefully tested. After each test, I “reset to demo template” just in case, and all 5 show false. It’s so weird to me.

{{ states('input_text.analog1') == "1" }}
{{ states('input_text.analog1') == 1 }}
{{ states('input_text.analog1') | int(0) == 1 }}
{{ is_state('input_text.analog1', '1') }}
{{ is_state('input_text.analog1', 1) }}

A hidden space perhaps?

What does this return in the template editor:

"{{ states('input_text.analog1') }}"

Make sure you include the quotes.

So try this at that same Template tab:

{{ states('input_text.analog1') }}
{{ states('input_text.analog1') | int(0) }}

This returns:

"“1”"

1st query:

{{ states('input_text.analog1') }}

returns

“1”

2nd query:

{{ states('input_text.analog1') | int(0) }}

returns

0

Solution:

Thank you both! So the answer is pretty obvious now. No wonder I had so much trouble.

This is what I need:

{{ states('input_text.analog1') == "“1”" }}

Final code:

service: |
  {% if states('input_text.analog1') == "“1”" %}
    media_player.media_play
  {% elif states('input_text.analog1') == "“2”" %}
    media_player.media_stop
  {% else %}
    
  {% endif %}
target:
  entity_id: media_player.dining_room

It’s great that it is working now, but this is not the most beautiful solution… :stuck_out_tongue_winking_eye:

How are you adding this value to your input text? Are you using some service call or is this manually typing somewhere?
OK, reading your original post again I found the answer…
Can’t you change that external service to send just the number?

I am using Nick Pope’s code from here to integrate with Crestron:

The problem is I am just using it without much understanding of how it works, which is why I ran into the problem I did. However, it is working well now and understanding how to use the Developer Tools → Template was a huge help to being able to test things myself.

Thank you so much for the prompt replies, @EdwardTFN and @tom_l

1 Like

I could probably change the external service to send just the number, but this actually seems like a robust solution. It isn’t beautiful, but I can’t see why it wouldn’t be properly reliable, so I’ll just use it and see how it goes.

I believe the reason we are getting data like this is due to the raw data sent from Crestron’s XSIG. I probably don’t want to mess with that.

Could you please share the yaml where you take values from crestron and store it into the input text?

You have a point… :wink:

I am just doing something simple with the configuration.yaml:

I am using a single analog value to trigger different tasks at this point, and it works well now.

crestron:
  port: 16380
  from_joins:
    - join: a1
      script:
        service: input_text.set_value
        data:
          entity_id: input_text.analog1
          value: “{{ value | int }}”

Try this:

crestron:
  port: 16380
  from_joins:
    - join: a1
      script:
        service: input_text.set_value
        data:
          entity_id: input_text.analog1
          value: "{{ value | int }}"

I’ve replaced by ".

By the way, as you are always converting the value to an integer, you could use a numeric value helper instead.

I see the error now. Ok, all working good now: Thank so much @EdwardTFN

configuration.yaml

crestron:
  port: 16380
  from_joins:
    - join: a1
      script:
        service: input_text.set_value
        data:
          entity_id: input_text.analog1
          value: "{{ value | int }}"

automation action:

service: |
  {% if states('input_text.analog1') == "1" %}
    media_player.media_play
  {% elif states('input_text.analog1') == "2" %}
    media_player.media_stop
  {% else %}
    
  {% endif %}
target:
  entity_id: media_player.spare
1 Like

I tried it, but of course with the existing mistake, I could not make any progress.

Having said that, it makes more sense for my requirements to keep this particular implementation as text because each analog number represents a specific action and I will not be using arithmetic operations to evaluate the incoming number.

It’s pretty neat with so such a relatively simple implementation of just changing one single analog value from Crestron, I can already integrate 65535 actions from Crestron to HA.

Big thanks to @npope for his github share for the Crestron custom component as well.

Thank you again!

You can also simplify it a bit using this:

service: >
  {% if is_state('input_text.analog1', '1') %}
    media_player.media_play
  {% elif is_state('input_text.analog1', '2') %}
    media_player.media_stop
  {% else %}
    
  {% endif %}
target:
  entity_id: media_player.dining_room

Thank you @tom_l!