Automation with MQTT and transform Input_select text to integers

I successfully use an input_select and an automation to pick a value of 0, 1 or 2 in the input_select in the GUI and send that value as the payload of an MQTT message. I have a 2nd automation updates the input_select when HA sees a new value appear inbound on that same MQTT topic. Thats all working fine.
What I’d like to do is put text in the input_select like “Red”, “Green” etc and when they are selected, convert them to an integer say 0 or 1 or 2 etc and send that out over the MQTT topic. And vice versa, if HA sees an integer update inbound on the MQTT topic it will update the input_select to the correct text.
I have been trying various templates, but I can’t get my head around what I need to do. Any ideas?

Here is the original input_select and the new one I’m working on.

pj_office_leds_dynamic_pattern_sub_mode:
  name: PJ Office LEDs Dynamic Pattern Sub Mode (dev231)
  options:
    - "1"
    - "2"
    - "3"
    - "4"
  initial: "1"
  icon: mdi:flashlight:

 pj_office_leds_dynamic_pattern_sub_mode:
  name: PJ Office LEDs Dynamic Pattern Sub Mode (dev231)
  options:
    - "Black"
    - "Red"
    - "Green"
    - "Blue"
  initial: "Black"
  icon: mdi:flash

Here are my two automations that work with the integer based input_select but need to be modified to work with the text based input_select.

- alias: Sync PJ Office LEDs Dynamic Pattern Sub Mode Selector
  trigger:
    platform: mqtt
    topic: "home/rfm_gw/nb/node03/dev231"
  action:
     service: input_select.select_option
     data_template:
      entity_id: input_select.pj_office_leds_dynamic_pattern_sub_mode
      option: '{{ trigger.payload }}'

- alias: Set PJ Office LEDs Dynamic Pattern Sub Mode
  trigger:
    platform: state
    entity_id: input_select.pj_office_leds_dynamic_pattern_sub_mode
  action:
    service: mqtt.publish
    data_template:
      topic: "home/rfm_gw/sb/node03/dev231"
      retain: true
      payload: '{{ states.input_select.pj_office_leds_dynamic_pattern_sub_mode.state }}'

Ok, I have figured this out and thought I’d doco it here in case someone else needs to know.
So just to remind you what the goal was, it was to use a text based input_select config like this one

pj_office_leds_dynamic_pattern_sub_mode:
name: PJ Office LEDs Dynamic Pattern Sub Mode (dev231)
options:
- “Black”
- “Red”
- “Green”
- “Blue”
initial: “Black”
icon: mdi:flash

but have an integer (corresponding to each of the text entries) sent in an MQTT payload, ratyer than the text itself. For example, when the user selects Red, I need to send a 2 in MQTT to my LED controller.
I got it working with the following automation

- alias: Set PJ Office LEDs Dynamic Pattern Sub Mode
  trigger:
    platform: state
    entity_id: input_select.pj_office_leds_dynamic_pattern_sub_mode
  action:
    service: mqtt.publish
    data_template:
      topic: "home/rfm_gw/sb/node03/dev231"
      retain: true
      payload: >-
        {% if states.input_select.pj_office_leds_dynamic_pattern_sub_mode.state == "Black" %}1
        {% elif states.input_select.pj_office_leds_dynamic_pattern_sub_mode.state == "Red" %}2
        {% elif states.input_select.pj_office_leds_dynamic_pattern_sub_mode.state == "Green" %}3
        {% elif states.input_select.pj_office_leds_dynamic_pattern_sub_mode.state == "Blue" %}4
        {% endif %}

I also needed to have the input_select in the GUI automatically update if HA sees an MQTT message with the corresponding topic and then integer payload. I got that working successfully with this automation

- alias: Sync PJ Office LEDs Dynamic Pattern Sub Mode Selector
  trigger:
    platform: mqtt
    topic: "home/rfm_gw/nb/node03/dev231"
  action:
     service: input_select.select_option
     data_template:
      entity_id: input_select.pj_office_leds_dynamic_pattern_sub_mode
      option: >-
        {% if trigger.payload == "1" %}Black
        {% elif trigger.payload == "2" %}Red
        {% elif trigger.payload == "3" %}Green
        {% elif trigger.payload == "4" %}Blue
        {% endif %}

Hi there,

I did something along these line. Have a look here: