Input_text matching - SOLVED

Note however that your template will generate errors and possibly stop your automation actions if something other than a 1 or 2 is received (the else case). A service must be supplied, it can not be null. You could put a script in there that just does a 1 second delay (scripts are services).

Though you might be better off with a choose action where you don’t have to supply an else case.

Ah. Thank you for pointing that out. I will amend accordingly.

I was wondering, what would a good way be to control multiple entities from the single automation that detects a change in analog1?

For example, as I start to implement this properly, it would look more like:

service: >
  # bedroom keypad button 1
  {% if is_state('input_text.analog1', '2101') %}
    media_player.bedroom.media_play
  # bedroom keypad button 12
  {% elif is_state('input_text.analog1', '2112') %}
    media_player.bedroom.media_stop
  # dining keypad button 1
  {% if is_state('input_text.analog1', '2601') %}
    media_player.dining.media_play
  # dining keypad button 12
  {% elif is_state('input_text.analog1', '2612') %}
    media_player.dining.media_stop
  {% else %}
    script.delay1s
  {% endif %}

I understand the above syntax won’t work, but it illustrates what I want to achieve, but I don’t quite know what the syntax would look like. It would be really nice if the above code does work though :slight_smile:

Your dining keypad if statement needs to be an elif statement and you have to be careful with comments in templates, put them in {# #} but other than that this will work:

service: >
  {# bedroom keypad button 1 #}
  {% if is_state('input_text.analog1', '2101') %}
    media_player.bedroom.media_play
  {# bedroom keypad button 12 #}
  {% elif is_state('input_text.analog1', '2112') %}
    media_player.bedroom.media_stop
  {# dining keypad button 1 #}
  {% elif is_state('input_text.analog1', '2601') %} 
    media_player.dining.media_play
  {# dining keypad button 12 #}
  {% elif is_state('input_text.analog1', '2612') %}
    media_player.dining.media_stop
  {% else %}
    script.delay1s
  {% endif %}

For your else case script you could replace the delay script with a more useful notification:

script:
  invalid_key:
    sequence:
      - service: notify.notify # replace with your messaging service
        data:
          message: "Invalid key number received: {{ states('input_text.analog1') }}"
1 Like

Probably, it is using to_json before setting the value.
You may use from_json to get that value.

Something like

{{ states('input_text.analog1') | from_json == 1 }}

Thanks. This particular issue is already solved.

1 Like
    media_player.bedroom.media_play
    media_player.dining.media_play

These sorts of calls don’t seem to work. I haven’t really spent much time trying to figure it out though.

The previous code works when this is included, but then I can’t target multiple players.

target:
  entity_id: media_player.dining

Ah, ok. You have to template the service and the entity separately.

service: >
  {# bedroom keypad button 1 #}
  {% if is_state('input_text.analog1', '2101') %}
    media_player.media_play
  {# bedroom keypad button 12 #}
  {% elif is_state('input_text.analog1', '2112') %}
    media_player.media_stop
  {# dining keypad button 1 #}
  {% elif is_state('input_text.analog1', '2601') %} 
    media_player.media_play
  {# dining keypad button 12 #}
  {% elif is_state('input_text.analog1', '2612') %}
    media_player.media_stop
  {% else %}
    script.delay1s
  {% endif %}
target:
  entity_id: >
    {# bedroom keypad button 1 #}
    {% if is_state('input_text.analog1', '2101') %}
      media_player.bedroom
    {# bedroom keypad button 12 #}
    {% elif is_state('input_text.analog1', '2112') %}
      media_player.bedroom
    {# dining keypad button 1 #}
    {% elif is_state('input_text.analog1', '2601') %} 
      media_player.dining
    {# dining keypad button 12 #}
    {% elif is_state('input_text.analog1', '2612') %}
      media_player.dining
    {% else %}
      script.delay1s
    {% endif %}

Now you really have to make sure the else case never happens with conditions. As the script service for the else case does not accept a target.

Ahh, I see. I am really very very grateful for the pointers. I am not sure how long it would have taken for me to figure this out! YAML syntax is really strange for me to wrap my head around.

I will report back after I have some time to test this.

You can use a condition like this:

condition:
  condition: template
  value_template: "{{ states('input_text.analog1') in ['2101', '2112', '2601', '2612'] }}"
1 Like

Currently I have the script as:

alias: delay1s
sequence:
  - service: notify.mobile_app_s
    data:
      message: "KP Pressed, else event:: {{ states('input_text.analog1') }}"
mode: single

This works great whilst I am trying to program up the system, but in the end I just want to log the events in the Logbook. How can I modify that to log to the generic built-in logbook? I did play around with this a little but there is not much documentation on this from what I could find.

This works perfectly. Thank you so very much.

Like this: https://www.home-assistant.io/integrations/logbook/#custom-entries

Got it, thank you :slight_smile:

alias: delay1s
sequence:
  - service: logbook.log
    data:
      name: KP Else Event
      message: "KP Pressed, else event:: {{ states('input_text.analog1') }}"
mode: single