How to keep internal state for Automation condition

Hello

What i want to do:
Keeping internal state of my monitors [D-SUB,DVI,HDMI] and acting upon automation.
Depending on the internal state need to send different command to change the input.

The pseudo-code should look like:

>  #initialization
>   var1 = DVI 
> 
>   if automatization triggered (change to HDMI):
>        if var1 in [D-SUB,DVI]
>             - open input menu
>             if var1 = D-sub 
>                  - go up one in the menu
>             if var2 = DVI
>                  - go down one in the menu
>             - click to accept
>             var1 = HDMI

I found a hass-variables https://github.com/rogro82/hass-variables package in the net, but i not sure if this is the right way to go?

variable:
  Monitor_1_input:
    value: "DVI"
  Monitor_2_input:
    value: "DVI"

Also how to use the created variable in the automation condition?

what about using a inputselect

input_select:
  select_output:
    name: "select your output"
    options:
        - 'D - sub'
        - 'DVI' 
        - 'HDMI'
        - 'Select one'
    initial: 'Select one'
    icon: mdi:logout-variant

that give you s drop down list to pick one and the HDMI will be selected
now the automation bit

- id: 'output select changed'
  alias: 'output select changed'
  trigger:
  - platform: state
    entity_id: input_select.select_output
    to: 'DVI'
  condition: []
  action:

this should point down the path

Thank you Myle.

So i should do something like this:


input_select:
  Monitor2_input:
    name: "Monitor2_input"
    options:
      - "DVI"         # moved to first place so at the first time start this will be the initial value
      - "D-SUB"
      - "HDMI"
    icon: mdi:target
    #initial: "DVI"   //removed so restore state after reboot
    
    
- id: 'Monitor2_Input_HDMI'
  alias: Monitor2_Input_HDMI
  description: Monitor2_Input_HDMI
  trigger:
  - platform: mqtt
    topic: RFdemux/GA_remote001e
  condition: template
    value_template: '{{ input_select.Monitor_2_input != "HDMI" }}'   # if already HDMI nothing to do here
  action:
  - data: {}
  #
  # open Menu
  - service: switch.turn_on
    entity_id: switch.GA_WS_Monitor_2_Relay_5
  - service: switch.turn_off
    entity_id: switch.GA_WS_Monitor_2_Relay_5
    #
    # select HDMI
    service_template: >
      {% if (is_state("input_select.Monitor_2_input", "DVI")) -%}  # select HDMI from DVI menupoint
          - service: switch.turn_on
            entity_id: switch.GA_WS_Monitor_2_Relay_2
          - service: switch.turn_off
            entity_id: switch.GA_WS_Monitor_2_Relay_2
      {%- else -%}                                                                        # select HDMI from D-SUB menupoint
          - service: switch.turn_on
            entity_id: switch.GA_WS_Monitor_2_Relay_2
          - service: switch.turn_off
            entity_id: switch.GA_WS_Monitor_2_Relay_2
          - service: switch.turn_on
            entity_id: switch.GA_WS_Monitor_2_Relay_2
          - service: switch.turn_off
            entity_id: switch.GA_WS_Monitor_2_Relay_2
      {%- endif %}
  #
  # save
  - service: switch.turn_on
    entity_id: switch.GA_WS_Monitor_2_Relay_3
  - service: switch.turn_off
    entity_id: switch.GA_WS_Monitor_2_Relay_3

I am not sure how to use the service template part i just found simple examples
Also i am not sure if i can use service with service template