Switching Tradfri Bulb on and setting brightness by rule

Hi everybody,

in advance it seems very simple but it is a problem for me!
I would like to switch a Tradfri bulb on and set brightness to a specific level.
I tried to fix the issue by a sleep thread, by return; . One switching process worked (On and Off). Switching on a second time it all ends up in a loop. It goes on and of until a rule changing.
Is there anybody with a solution?

Simple rule I tried:

rule "Bürolicht"
when
    Item Tradfri_Buero_Switch changed
then
        Tradfri_Buero_Dimmer.sendCommand(80)
end

If/else rule I tried:

rule "Bürolicht"
when
    Item Tradfri_Buero_Switch changed
then
    if(Tradfri_Buero_Switch.state == ON) {
        Tradfri_Buero_Dimmer.sendCommand(80)
    } else {
        if(Tradfri_Buero_Switch.state == OFF) {
            Tradfri_Buero_Dimmer.sendCommand(0) 
        }
    }
end

Thanks for you help!
BR a neewby

Controlled from where?
If it’s from the UI, it issues commands.
You’d have your rule triggered by received command and use the receivedCommand value to choose the actions.

Thanks for the quick support!
It is triggered from the UI. And as well later with e.g. Alexa.
Additionally I reduced the If/else phrase to simplyfy the rule.

rule "Bürolicht"
when
    Item Tradfri_Buero_Switch received command
then
    if(Tradfri_Buero_Switch.state == ON) {
        Tradfri_Buero_Dimmer.sendCommand(80)
    } else {
            Tradfri_Buero_Dimmer.sendCommand(0) 
    }
end

Careful with that. The rule will be triggered by the command, but the command has probably not yet got around to changing the target’s state. Your “if” is looking at the “old” state.
If you use the receivedCommand implicit variable instead of the Item.state it’ll work properly.

You may or may not still need some delay, depending on how your bulb copes with rapid commands.