Problem with including variable into string

Hello,

I need some help with a rule:

rule "send audio status zone 1"
when
    Item zone1_vol changed or
	Item zone1_bal changed or
	Item zone1_high changed or
	Item zone1_low changed or
	Item zone1_source changed or
	Item zone1_mute changed
then
	if(zone1_mute.state == ON){
	val String mute = "0"
	}
	if(zone1_mute.state == OFF){
	val String mute = "1"
	}
	
	Audio_status_send.sendCommand("status zone 1," + zone1_vol_calc.state.toString + "," + zone1_bal.state.toString + "," + zone1_high.state.toString + "," + zone1_low.state.toString + "," + zone1_source.state.toString + "," + mute + ";")
end

The item “Audio_status_send” is a String item that sends it state to MQTT.
This is not working because the variable “mute” in the “Audio_status_send.sendCommand” command. When i delete the variable “mute” in this command than it is working.

So 2 things could cause my problem:

  • variable is wrong declared
  • variable need some extra information in the command “Audio_status_send.sendCommand”

who can help me clear this out?

You declared the val inside he if ( inside { }, that way it isn’the known outside.
You use a val (value, cannot be changed).
Use a var declared above the if.

1 Like

thanks, I was forgotten that the val value was a fixed value. i thougth that is was no problem because is change only when the rule is activated and the variable was created inside the rule (and placed is on the wrong place)

The command is OK like i posted it?

I was referring to the error only.
Regarding the used “sendCommand”: It looks like the items which is receiving this command is used to be persisted only, in this case a “postUpdate” should be mor appropriate.
Why are persisting such a combination string instead of seperated values or strings for each item?

I use the sendcommand because the string has to pushed to the MQTT binding. I understood that postupdate was only updated the value and that you need to use sendcommand to push it to the binding for that particular item.

But for my question i want to know or the structure behind the command is OK, I mend this part:

("status zone 1," + zone1_vol_calc.state.toString + "," + zone1_bal.state.toString + "," + zone1_high.state.toString + "," + zone1_low.state.toString + "," + zone1_source.state.toString + "," + mute + ";")

is it OK to place the variable this way in the structure.

I want to make this combination beacause i need to send it that way to the MQTT binding.

Try to log the string and see what it puts out. That way you’ll see if there’s something funny in the string itself.

I allready did that (before the remarks of OPUS) and I get nothing.
I is like the rule is not fully executed. First I going to implement the remarks of OPUS and see what is happening.

Sorry, but I missed the point that you are sending it via this to MQTT.

May be you have a problem with your IF

change it to
zone1_mute.state.toString == “ON”

An other problem may be that the mute.state is not initialised an may have the value “null”

What happens if both if are not fullfilled? Then the “val String mute” is not existent. You should initialise it outside before the if.

This is the zone status for a multiroom audio system. The string is send with MQTT to a ESP-12e WiFi device that put this string directly on it’s serial port to the multiroom audio system.

So everytime that a parameter of a zone in openhab is changing a want to send the zone status to the multiroom audio device.

I’m not verry good at programming so maybe this is not the best way to implement this things but it is working (except for the mute variable, I have to covert the ON and OFF status of the switch item to a 0 and 1 value for the audio device.

I would try that also.

It is working now, the biggest problem was indeed the declaration of the variables inside { }!
thank you all!