Creating a switch to turn rules on and off


(Ryan S) #1

Hi there, I have successfully setup this rule to work however I’m having a problem with the state change, most specifically on my mobile. I created a switch to control the rule below but the problem I’m having is that when I turn the rule switch on with my mobile phone it does not get picked up on the desktop. How can I have state changes reflected with some code? Below the rule code is the items code. The bedroom lights switch successfully updates on the desktop when switched on the mobile and vice versa. I’m assuming this is fine because of the MQTT message sent but how do I have this same functionality with a “floating” switch?

//Rule

rule “turn burner on for work”

when

Time cron “0 5 6 ? * MON-FRI *”

then

if (Burner_Wakeup_Switch.state==ON)
{sendCommand(Stove_Burner,ON)}

end

//Items

Switch Bedroom_Lights “Bedroom Lights” (LR,gLight)
{mqtt=">[broker:cmnd/bedroomlights/power:command:*:default],
<[broker:stat/bedroomlights/POWER:state:default]"}

Switch Burner_Wakeup_Switch “Toggle Burner on Wakeup”


(Rich Koshak) #2

First, How to use code fences.

What do you mean by this statement. Are you saying that when you change the switch on your phone the Rule desn;t get the state or that the sitemap or HABPanel display on the desktop doesn’t show the new state?

State changes are state changes. Either they happen or they do not happen. So is Burner_Wakeup_Switch not changing state (you can see that in events.log) or is the UI just not showing that the state has changed?


(Ryan S) #3

Thanks for the prompt reply Rich, you are really helping everyone out on here. I’ve read through countless topics and they all seem to have multiple replies from you.So the Burner_Wakeup_Switch is changing state when I check the log, however if I check the sitemap on my mobile device, it’s status still shows as off.


(Ryan S) #4

I’ve tried this code you posted in another topic but I can’t seem to get it to work.


rule "status received update"
when
    Item Burner_Wakeup_Switch received update
then
   Burner_Wakeup_Switch.postUpdate(if(Burner_Wakeup_Switch.state.toString == "1") ON else OFF)
end


(Lucky) #5

What you’re missing is basic coding experience. Here’s what u should be doing…

rule "status received update"
when 
    Item Burner_Wakeup_Switch received update 
then 
    Burner_Wakeup_Switch.postUpdate( Burner_Wakeup_Switch.state.toString == "1" ? ON : OFF)
end 

But your logic doesn’t make sense to me. Is this an Item switch already? Basically, your rule says to update the item with its own state when it receives an update.


(Rich Koshak) #6

Then the problem has nothing to do with the Rule. There are known bugs in some browsers where it doesn’t always update the sitemap immediately with changes to the Items.

Actually, Ryan has the correct syntax for the Rules DSL. The trinary operator is indeed:

if(condition) trueValue else falseValue

https://www.eclipse.org/xtend/documentation/203_xtend_expressions.html#if-expression

Why they changed I can’t say, perhaps they liked it better that way.

But, like Lucky says, the Rule makes no sense.

Walk through the Rule step by step.

  1. Burner_Wakeup_Switch receives an update. Since it is a Switch Item that update can only be ON, OFF, or NULL
  2. We check to see if Burner_Wakeup_Switch.state == “1”. Since “1” is not a possible state for a Switch this will always be false so the trinary will always return OFF
  3. We postUpdate OFF to Burner_Wakeup_Switch which takes us back to 1. Loop forever.

What are you trying to accomplish?