Say you had a rough day with the kids and you were super stressed out. You want to find a way to calm down; so you try a number of things. Maybe a bath, glass of wine, TV. You find one that works for you and then that becomes your habit.
There are 4 elements to every habit - whether you find it effective or ineffective for you:
1. Cue - you feel overwhelmed after a hectic day and the kids are finally in bed
2. Craving - you crave something to take the edge off and calm you down
3. Response - you drink a glass of wine
4. Reward - your glass of wine satisfies your craving and you feel calm
You now associate the glass of wine with helping you calm down and relax after a difficult day.
Same works with coffee in the morning
1. You wake up
2. You want to feel awake and alert
3. You drink coffee
4. You satisfy your craving and coffee is no associated with waking up.
You can be free of your bad habits when you bring them into your consciousness! Decide WHO you want to be and then create habits around who you want to become.
Habits are about becoming SOMONE not having SOMETHING!
For more info on habits - how to start new ones, break old ones and become you really want to, I highly suggest Atomic Habits by James Clear.