How to use variables instead of inputs?

I need to perform an operation ( split ) on input value before I use it in my template. So, I think, variables should work. I tried many syntaxes, but none of them work for me. Please, coukld anybody advice, what is correct approach?

My definition is:

input:
    ctrl:
      name: CTRL
      description: Select ctrl
      selector:
        select:
          mode: dropdown
          options:
            - "10:1"
            - "10:2"
 
variables:
  ctrl_val: !input ctrl
  hub_nbr: "{{ ctrl_val.split(':')[0] }}"
  switch_nbr: "{{ ctrl_val.split(':')[1] }}"

triggers:
  - trigger: event
    event_type: esphome.button_pressed
    event_data:
      hub: >-
        {{ hub_nbr }}
      switch: >-
        {{ switch_nbr }}
1 Like

Thanks, but there is something else. I changed it according your tip and I made it very very simple:

trigger_variables:
  hub_nbr: "10"

triggers:
  - trigger: event
    event_type: esphome.button_pressed
    event_data:
      hub: >-
        {{ hub_nbr }}
      switch: "2"

It still doesn’t work, but this works:

triggers:
  - trigger: event
    event_type: esphome.button_pressed
    event_data:
      hub: "10"
      switch: "2"

The event trigger may not accept templates then. I don’t know.
If that’s the case trigger on the event_type only,
Then check in the conditions (or actions if you have more than one trigger) that the hub and switch numbers are what you are looking for.

IIRC, you need to template the whole event_data object:

triggers:
- trigger: event
event_type: esphome.button_pressed
event_data: |
{{ {“hub”: hub_nbr, “switch”: switch_nbr} }}

EDIT: Just tested that and it doesn’t work either…

Using a more general Event trigger with a condition does work:

triggers:
  - trigger: event
    event_type: esphome.button_pressed
variables:
  hub_nbr: "10"
  switch_nbr: "3"
conditions:
  - condition: template
    value_template: >-
      {{ (trigger.event.data.hub, trigger.event.data.switch) ==
      (hub_nbr, switch_nbr) }}

That makes sense.

it’s because templates are changing the values from a string to a number.

Only way to get around it would be to template the entire event_data section, which I’m not 100% if that’s possible.

Other way would be to make the esphome event returns ints not strs.

Shouldn’t using an int as the value for the trigger variable work then… it doesn’t.

Tried that but the automation wouldn’t save because a dictionary is expected.

That doesn’t seem to be an option:

image

Then there’s no way to do this. Maybe making it a single field and concatenating the strings on esphome side? Then using the input as-is