How to plot a measurement value only where another measurement value is present

I’m visualizing temperature with the Graph panel.

However, i want to display whenever the machine is ON/OFF/IDLE. This is stored in another measurement.

My idea was to color the temperature graph based on the state of the machine. Another idea was to ONLY show the temperature when the machine was running in a certain state.

Is this something i can do in Grafana?

This is the query i use to get the temperatures.
SELECT "value" FROM "TTAA" WHERE ("tag" = 'MAC1') AND $timeFilter