Switch voltage output with Zener diodes in series
Here's a circuit.







simulate this circuit &ndash; Schematic created using CircuitLab

When the signal is 5V the output will be 12VDC. Otherwise it will be 24VDC.

NOTES:


Vin must be at least 30V and RZ should be calculated according to minimum input voltage, \$V_{i-min}\$: \$RZ[k\Omega]=(V_{i-min}-24)/8\$. Its power rating should be calculated from maximum input voltage, \$V_{i-max}\$.
Q1 may need a heatsink. Because, if the input voltage is 30V and the output voltage is 12V, total power consumption of Q1 will be about 0.75W at a load current of 40mA. So a power transistor in TO-220 package (e.g. BD243) and a proper heatsink should be used.