Display Twitch stream only when live?

Looking to have my Twitch stream appear on my homepage but only when live. When I’m not live, I’d like for the group element that is holding the embed code to collapse and go back to the normal looking homepage.

Does anybody know if this is possible with either Twitch or YouTube?

Twitch provide REST API, you can use that.

To see channel is live or not, you can use-
Reference | Twitch Developers API and see the type.