Here's my code : 

import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense
from firebase import firebase
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
import numpy as np
from sklearn.model_selection import train_test_split
from keras.layers import Dropout, Dense, LSTM
import pandas


def create_dataset(dataset, look_back=1):
    dataX, dataY = [], []
    for i in range(len(dataset) - look_back - 1):
        a = dataset[i:(i + look_back), 0]
        dataX.append(a)
        dataY.append(dataset[i + look_back, :])
    return np.array(dataX), np.array(dataY)


def create_lstm_model(trainX, trainY, testX, testY, lstm_units, name='LSTM Model'):
    # Define the LSTM autoencoder model
    model = Sequential()
    model.add(LSTM(lstm_units[0], activation='relu', input_shape = (1, 1), return_sequences = True))
    model.add(LSTM(lstm_units[1], activation='relu', return_sequences = True))
    model.add(LSTM(lstm_units[2], activation='relu'))
    model.add(Dense(lstm_units[3]))
    model.add(Dense(4))

    # Compile the model
    model.compile(optimizer='adam', loss ='mae')

    # Train the model on the training data
    history = model.fit(trainX, trainY, epochs=50, batch_size=12, verbose=1, validation_split=0.2)

    # Evaluate the model on the testing data
    loss = model.evaluate(testX, testY, batch_size=12)
    print(f'{name} loss: {loss}')

    return model


def ensemble_models(data1, data2, data3, data4):
    min_length = min(len(data1), len(data2), len(data3), len(data4))

    # Slice each array to the minimum length
    data1 = data1[len(data1) - min_length:]
    data2 = data2[len(data2) - min_length:]
    data3 = data3[len(data3) - min_length:]
    data4 = data4[len(data4) - min_length:]

    # Pad the sequences with zeros so that they all have the same length
    max_length = max(len(data1), len(data2), len(data3), len(data4))
    all_data = np.zeros((4, max_length, 1))
    for i, data in enumerate([data1, data2, data3, data4]):
        all_data[i, :len(data), :] = np.array(data).reshape(-1, 1)

    d = {'hum': data1, 'light': data2, 'temp': data3, 'mois': data4}
    dataset = pandas.DataFrame(d)
    dataset = dataset.values
    dataset = dataset.astype("float32")

    train_size = int(len(dataset) * 0.7)
    test_size = len(dataset) - train_size
    train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :]
    trainX, trainY = create_dataset(train)
    testX, testY = create_dataset(test)

    trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
    testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))

    # Create the first LSTM model
    lstm1 = create_lstm_model(trainX, trainY, testX, testY, lstm_units=[128, 64, 32, 64], name='LSTM Model 1')

    # Create the second LSTM model with a different architecture
    lstm2 = create_lstm_model(trainX, trainY, testX, testY, lstm_units=[64, 32, 16, 32], name='LSTM Model 2')

    # Get predictions from both models
    predictions1 = lstm1.predict(testX)
    predictions2 = lstm2.predict(testX)

    # Combine the predictions using a weighted average
    ensemble_weight_1 = 0.6
    ensemble_weight_2 = 1.0 - ensemble_weight_1
    ensemble_predictions = ensemble_weight_1 * predictions1 + ensemble_weight_2 * predictions2

    # Evaluate the ensemble model’s performance
    mae = np.mean(np.abs(ensemble_predictions - testY), axis=0)
    print(f'Ensemble MAE: {mae}')

    # (Optional) Visualization code

    plt.figure(figsize=(12, 6))

    plt.plot(predictions1[:, 0], label='Humidity Predictions 1', linestyle ='-')
    plt.plot(predictions1[:, 1], label='Light Predictions 1', linestyle ='-')
    plt.plot(predictions1[:, 2], label='Temperature Predictions 1', linestyle ='-')
    plt.plot(predictions1[:, 3], label='Moisture Predictions 1', linestyle ='-')

    plt.plot(predictions2[:, 0], label='Humidity Predictions 2', linestyle ='-')
    plt.plot(predictions2[:, 1], label='Light Predictions 2', linestyle ='-')
    plt.plot(predictions2[:, 2], label='Temperature Predictions 2', linestyle ='-')
    plt.plot(predictions2[:, 3], label='Moisture Predictions 2', linestyle ='-')

    plt.plot(testY[:, 0], label='Humidity Real', alpha=0.7, linestyle='dashed')
    plt.plot(testY[:, 1], label='Light Real', alpha=0.7, linestyle='dashed')
    plt.plot(testY[:, 2], label='Temperature Real', alpha=0.7, linestyle='dashed')
    plt.plot(testY[:, 3], label='Moisture Real', alpha=0.7, linestyle='dashed')

    plt.xlabel('Time Steps')
    plt.ylabel('Sensor Values')

    #plt.legend()
    plt.show()

    return lstm1, lstm2


if __name__ == '__main__':

    # Fetch the service account key JSON file contents
    cred = credentials.Certificate('gunadarma-monitoring-firebase-adminsdk-t4zec-95d7f006d3.json')
    # Initialize the app with a service account, granting admin privileges
    firebase_admin.initialize_app(cred, {
        'databaseURL': "https://gunadarma-monitoring-default-rtdb.asia-southeast1.firebasedatabase.app"
    })

    humidity = db.reference('/live/Humidity').get()
    light = db.reference('/live/Luminosity').get()
    temp = db.reference('/live/Temperature').get()
    moisture = db.reference('/live/moisture').get()
    print("humidity : ", humidity)
    print("luminosity : ", light)
    print("temperature : ", temp)
    print("moisture : ", moisture)

    record_hum = db.reference('/previous_Humidity').get()
    humrecord_list = [i for i in record_hum.values()]

    record_light = db.reference('/previous_Luminosity').get()
    lightrecord_list = [i for i in record_light.values()]

    record_temp = db.reference('/previous_Temperature').get()
    temprecord_list = [i for i in record_temp.values()]

    record_mois = db.reference('/previous_moisture').get()
    moisrecord_list = [i for i in record_mois.values()]

    humrecord_list = np.array(humrecord_list)
    lightrecord_list = np.array(lightrecord_list)
    temprecord_list = np.array(temprecord_list)
    moisrecord_list = np.array(moisrecord_list)

    z_humidity = np.abs((humrecord_list - np.mean(humrecord_list)) / np.std(humrecord_list))
    z_light = np.abs((lightrecord_list - np.mean(lightrecord_list)) / np.std(lightrecord_list))
    z_temperature = np.abs((temprecord_list - np.mean(temprecord_list)) / np.std(temprecord_list))
    z_moisture = np.abs((moisrecord_list - np.mean(moisrecord_list)) / np.std(moisrecord_list))

    # Set a threshold for the z-score
    threshold = 3

    # Remove any data point that has a z-score greater than the threshold
    humrecord_list = humrecord_list[z_humidity < threshold]
    lightrecord_list = lightrecord_list[z_light < threshold]
    temprecord_list = temprecord_list[z_temperature < threshold]
    moisrecord_list = moisrecord_list[z_moisture < threshold]


    if input("plot les données ? ") == "o":

        plt.plot(humrecord_list, color='r', label='hum')
        plt.plot(lightrecord_list, color='g', label='light')
        plt.plot(temprecord_list, color='b', label='temp')
        plt.plot(moisrecord_list, color='y', label='mois')
        plt.legend()
        plt.show()



    print("humidity history : (size ", len(humrecord_list), ") ", humrecord_list)
    print("luminosity history : (size ", len(lightrecord_list), ") ", lightrecord_list)
    print("temperature history : (size ", len(temprecord_list), ") ", temprecord_list)
    print("moisture history : (size ", len(moisrecord_list), ") ", moisrecord_list)

    ensemble_models(humrecord_list, lightrecord_list, temprecord_list, moisrecord_list)


I wanted to combine 2 models to get a more performing system, but here I only have 2 models working independently. Can you help me combine the 2 models I have pls ?