Error in rules for Sonos volume

Hello,

I just upgrade to OH 2.2 Stable - Openhabian on RP3.

I have been using rules like this earlier, but it is working anymore:

rule "Sænk volume Sonos 1. sal ophold"

when   
    Item  Sonos1salOphold_VolNed changed from OFF to ON
then
	var Number v_adjustvolume = Sonos_1salophold_Volume.state as DecimalType
    while(Sonos1salOphold_VolNed.state == ON) {
		v_adjustvolume = v_adjustvolume - 2
		sendCommand(Sonos_1salophold_Volume, v_adjustvolume)
        Thread::sleep(250)
    }
end

Sonos1salOphold_VolNed is a “normal” switch in my setup.

It is controlling an Sonos Connect:AMP.
In the Sonos binding, volume is defined like:
volume Dimmer RW Set or get the master volume of the Zone Player

When pushing the switch, the log shows following:

16:58:10.354 [INFO ] [smarthome.event.ItemStateChangedEvent] - Sonos1salOphold_VolNed changed from OFF to ON
16:58:10.554 [INFO ] [smarthome.event.ItemStateChangedEvent] - Sonos1salOphold_VolNed changed from ON to OFF
16:58:19.818 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'Sænk volume Sonos 1. sal ophold': Could not cast UNDEF to org.eclipse.smarthome.core.library.types.DecimalType; line 46, column 30, length 44

It is referring to this:

var Number v_adjustvolume = Sonos_1salophold_Volume.state as DecimalType

Anyone who has an idea of how this can be solved?

Best regards,
Kristoffer

The error sounds like your “Sonos_1salophold_Volume” has the state “UNDEF” when this rule is performed.
That was probably during the first run. Try to set it to a value and use a persistence service that restores on startup.

1 Like

Makes sense.

Thanks for this.

Will use persistence for this element or add an if statement in the rule.