There’s nothing wrong with the way you’re using the retained int. The problem is your test code. When I try it, I only get 0 once, then it never publishes again. If I put in a delay of 5000 instead of 1000, then it works as expected. Also, your second publish statement will never run. When you put the device into deep sleep, it resets when it starts back up (that is, it runs setup() again, it doesn’t go to the next line after the sleep command).