The best thing you can do is to stop thinking that “motivation” is going to be what gets you results. Trying to sustain going to the gym or changing your diet with motivation is like trying to warm your house with a firecracker. It’ll give you a short burst of energy and then quickly fizzle into nothing. Motivation is something you use to push through that last rep you’re struggling with, not for sticking with a new practice. Thinking of this in terms of motivation is why you have failed in the past.
The problem you’re trying to solve is your consistency and your discipline. Motivation isn’t going to keep you consistent or disciplined. They’re on entirely opposite ends of the spectrum. Instead, create a habit. Do you fail to fill up the gas tank in your car because you don’t have the motivation? No. You fill it up because you know it has to be done. Don’t have your level of motivation or desire be what determines whether or not you go to the gym or stick with your diet. Instead, exercise discipline so you can stay consistent. See /r/getdisciplined for more discussion in this area.
It is also important to keep in mind the very old adage of “Fall down seven times, get up eight” and not allow a single failure to permanently knock you off course. Just as you weren’t an immediate expert in walking as a toddler, it may take many false starts and brief stints before you’re able to learn the skill of sticking to it. Treat every time you fall off the wagon as an opportunity to learn what derails you and how to avoid it.