How to change a dashboard variable value from a plugin?

This worked for me to access variables in custom panel when using the “Mixed” mode (in the DS select box).
this is my code:

import { DataSourceApi } from '@grafana/data';
import { getDataSourceSrv } from '@grafana/runtime';

        let dataSource:DataSourceApi = getDataSourceSrv() as unknown as DataSourceApi;    
        let variable = _.find(dataSource.templateSrv.variables, {'name':'location_id'});