Formulas:
1. Run the LED at about half it's max current rating: If you need more brightness get a high brightness LED. I expect this to be 12mA.
2. Resistors get hot, so make sure the power dissipation is no more than 1/2 the resistor's rating for 120 volts (170vpeak).
3. For 120VAC, use a diode connected in anti-parallel across the LED. THis reduces the RMS voltage to 60 volts, but the peak remains 170V.
Assume the LED forward voltage is 2 volts or read the spec sheet for 1/2 max current.
Now the formulas:
for 24 volts.
The resistor must be sized to limit the current to 12mA rms, 20mA max., where 20 mA is assumed to be the max. operating LED current rating.
R =V/I gives the resistor ohms. V is V24-Vf or V60-Vf, I is 12mA
P = (V)(I) using the above values.
Example: Vf = 2V, I = 12mA, Operating voltage = 24VDC
R = (24-2)V/12mA = 1.83k ohms (use the nearest standard value)
P = (24-2)V * 12mA = 0.264 watts. (Use a 1/2watt in open air or 1watt if the space is confined.)
Example: Vf = 2V, I = 12mA, Operating Voltage = 120VAC.
R = V/I, V= Vrms - Vf = 60-2v = 58V.
R = 58/12mA = 4.83k ohms.
P = 58 * 12mA = .7Watts ( use 2 watts or more)
Vpeak = 170 - 2 = 168V peak
The current must not exceed the absolute maximum device ratings:
Imax = Vpeak / R = (168) / 4.83k =34.7mA.
If this is too close to your device rating, go back and choose the resistor so that the peak current is no more than 80percent of the absolute rating.
Example: Device abs. max rating is 40mA.
.8 x 40mA = 32mA
R = 168/32mA = 5.25k ohms.
Inew = 58/5.25k = 11.05 mA
Pnew = 58 * 11.05 mA = .64 watts
The resistor voltage will be 24V - the forward voltage. V=VfR = V/I