Fire a rule on Switch Press

Hi,
I have this item:

String BOSESOUNDDOCK_VOL "Cassa Bose Volume" <switch>

and this Sitemap:

Switch item=BOSESOUNDDOCK_VOL mappings=[VOLUP="Vol +", VOLDW="Vol -", MUTE="Mute"]

I need to fire a rule, check witch button is pressed and do something.
I tried with this rule:

rule "Cassa Bose Volume Handling"
when
	Item BOSESOUNDDOCK_VOL received command
then
	logInfo("CassaBOSE",BOSESOUNDDOCK_VOL.state.toString )
	//BOSESOUNDDOCK_VOL.postUpdate("nd")
end

I see in the logs, that sometime, is not sent the button i pressed, but the last one, and I have to press 2 time the command to send the one I want. It happen also enabling the postUpdate, that reset the status.
Is it possible to avoid this?

Many Thanks

Marco

Hi,
i solved using 3 different rules, one for each command of the switch:

Item BOSESOUNDDOCK_VOL received command VOLUP

I don’t know if this is the best way, but it seems to work.

Thanks

Marco

Why don’t you use

when
	Item BOSESOUNDDOCK_VOL changed
then

Hi @opus
my switch has 3 button, and I need to detect wich button is pressed of the switch and execute 3 different action

you could check the exact state of your switch between the “then” and “end” like:

 if (BOSESOUNDDOCK_VOL.state==VOLUP) {
            
          }   
  else if (BOSESOUNDDOCK_VOL.state==VOLDW) {
          }
  else if (BOSESOUNDDOCK_VOL.state==MUTE) {
          }

@opus
it is what I did, please read my first post, because I have an issue with this configuration.

Sorry, I missed that and I have no experience in that.

You are receiving a command in your rule, so to find out what the command was, you would instead use

logInfo("CassaBOSE",receivedCommand.toString)

Unless the default autoupdate is suppressed, at some point after the command was sent the state of the item would be updated. But when triggering a rule on received command, use the predefined receivedCommand to see what it was.

Hi @watou
Thanks for help, it works like a charm.

Kindly regards

Marco

1 Like