Page 1 of 1

Toggle device?

Posted: Thursday 03 November 2016 12:30
by theolsen
Hi all,

I'm pulling out my remaining hair with what I think should be a simple problem to solve.

I am using presence detection on my phone from a script running on my Asus router.
The script updates one of two virtual devices on either 2.4 or 5.0 Ghz network so that I can tell that my phone is in the house and which network it is connected to.
That works fine.
I have a further dummy device that is updated if either of the 2.4 or 5.0 dummies are on.

The problem is I want the dummy device to only turn on if it was previously off, for example right now if my phone switches from 2.4 to 5.0 the dummy device will be switched on again even though I am already home - I want this to only happen if it is already off...otherwise do not update.

I hope I have explained well.

Thanks in advance.

Re: Toggle device?

Posted: Thursday 03 November 2016 13:58
by Egregius
Use the current state of the switch in a if block of your script.

Re: Toggle device?

Posted: Thursday 03 November 2016 14:05
by theolsen
Thanks Egregius, I was hoping to cheat it in Blockly but you are right..lua is the answer!

Re: Toggle device?

Posted: Thursday 03 November 2016 14:13
by Egregius
Or any other language... ;)

Re: Toggle device?

Posted: Thursday 03 November 2016 16:23
by bloody2k
Hi

Would it be possible for you to share the script you use at your router?

Thanks

Re: Toggle device?

Posted: Thursday 03 November 2016 16:42
by pj-r
theolsen wrote:The problem is I want the dummy device to only turn on if it was previously off, for example right now if my phone switches from 2.4 to 5.0 the dummy device will be switched on again even though I am already home - I want this to only happen if it is already off...otherwise do not update.
Whats the problem with blocky?

if dummy <> on and (dummy24 = on OR dummy5 = on) then
set dummy = on
else if dummy = on and dummy24 <> on AND dummy5 <> on
set dummy = off
end if

Re: Toggle device?

Posted: Thursday 03 November 2016 18:07
by theolsen
bloody2k wrote:Hi

Would it be possible for you to share the script you use at your router?

Thanks
Of course, I cobbled it together from http://forum.micasaverde.com/index.php?topic=24084.0

I am sure it is far from efficient but it works for me and doesn't miss a beat.


Code: Select all

#!/bin/sh                                                                                                                                                              
WATCHDOG_SLEEP_SEC=10                                                                                                                                                  
MAC_ADDRESS_1=“nn:nn:nn:nn:nn:nn” # Ken                                                                                                                               
c1_last_state="0"                                                                                                                                                      
c3_last_state="0"                                                                                                                                                      
MAC_ADDRESS_2=“nn:nn:nn:nn:nn:nn” # Barbie                                                                                                                               
c2_last_state="0"                                                                                                                                                      
c4_last_state="0"                                                                                                                                                      
VERAURL1="http://192.168.1.65:8080/json.htm?type=command&param=switchlight&idx=26&switchcmd="                                                                          
VERAURL2="http://192.168.1.65:8080/json.htm?type=command&param=switchlight&idx=27&switchcmd="                                                                          
VERAURL3="http://192.168.1.65:8080/json.htm?type=command&param=switchlight&idx=29&switchcmd="                                                                          
VERAURL4="http://192.168.1.65:8080/json.htm?type=command&param=switchlight&idx=28&switchcmd="                                                                          
                                                                                                                                                                       
x=0                                                                                                                                                                    
y=0                                                                                                                                                                    
offcount1=0                                                                                                                                                            
offcount2=0                                                                                                                                                            
offcount3=0                                                                                                                                                            
offcount4=0                                                                                                                                                            
                                                                                                                                                                       
while sleep $WATCHDOG_SLEEP_SEC                                                                                                                                        
do                                                                                                                                                                     
        if [ "$x" == 180 ]; then                                                                                                                                       
                                                                                  
                x=0                                                                                                                                                    
                c1_last_state="0"                                                                                                                                      
                c2_last_state="0"                                                                                                                                      
                c3_last_state="0"                                                                                                                                      
                c4_last_state="0"                                                                                                                                      
                                                                                                                                                                       
        fi                                                                                                                                                             
        x=$(( $x + 1 ))                                                                                                                                                
                                                                                                                                                                       
        c1_new_state=`wl -i eth2 assoclist | grep $MAC_ADDRESS_1`                                                                                                      
        if [ "$c1_new_state" == "$c1_last_state" ] ; then                                                                                                              
                sleep 0                                                                                                                                                
        else                                                                                                                                                           
                if [ "$c1_new_state" == "assoclist $MAC_ADDRESS_1" ]; then                                                                                             
                        c1_last_state="$c1_new_state"                                                                                                                  
                         wget -qs "http://192.168.1.65:8080/json.htm?type=command&param=switchlight&idx=27&switchcmd=On"                                               
                        offcount1=0                                                                                                                                    
                else                                                                                                                                                   
                        offcount1=$(( $offcount1 + 1 ))                                                                                                                
                fi                                                                                                                                                     
        fi                                                                                                                                                             
                                                                                                                                                                       
        c2_new_state=`wl -i eth2 assoclist | grep $MAC_ADDRESS_2`                                                                                                      
        if [ "$c2_new_state" == "$c2_last_state" ] ; then 
              sleep 0                                                                                                                                                
        else                                                                                                                                                           
                if [ "$c2_new_state" == "assoclist $MAC_ADDRESS_2" ]; then                                                                                             
                        c2_last_state="$c2_new_state"                                                                                                                  
                        wget -qs "http://192.168.1.65:8080/json.htm?type=command&param=switchlight&idx=26&switchcmd=On"                                                
                        offcount2=0                                                                                                                                    
                else                                                                                                                                                   
                        offcount2=$(( $offcount2 + 1 ))                                                                                                                
                fi                                                                                                                                                     
        fi                                                                                                                                                             
                                                                                                                                                                       
        c3_new_state=`wl -i eth1 assoclist | grep $MAC_ADDRESS_1`                                                                                                      
        if [ "$c3_new_state" == "$c3_last_state" ] ; then                                                                                                              
                sleep 0                                                                                                                                                
        else                                                                                                                                                           
                if [ "$c3_new_state" == "assoclist $MAC_ADDRESS_1" ]; then                                                                                             
                        c3_last_state="$c3_new_state"                                                                                                                  
                        wget -qs "http://192.168.1.65:8080/json.htm?type=command&param=switchlight&idx=28&switchcmd=On"                                                
                        offcount3=0                                                                                                                                    
                else                                                                                                                                                   
                        offcount3=$(( $offcount3 + 1 ))                                                                                                                
                fi                                                                                                                                                     
        fi                                                                                                                                                             
                                                                                                                                                                       
        c4_new_state=`wl -i eth1 assoclist | grep $MAC_ADDRESS_2`                                                                                                      
        if [ "$c4_new_state" == "$c4_last_state" ] ; then                                                                                                              
                sleep 0                                                                                                                                                
        else                                                                                                                                                           
                if [ "$c4_new_state" == "assoclist $MAC_ADDRESS_2" ]; then                                                                                             
                        c4_last_state="$c4_new_state"                                                                                                                  
                        wget -qs "http://192.168.1.65:8080/json.htm?type=command&param=switchlight&idx=29&switchcmd=On"                                                
                        offcount4=0                                                                                                                                    
                else                                                                                                                                                   
                        offcount4=$(( $offcount4 + 1 ))                                                                                                                
                fi                                                                                                                                                     
        fi                                                                                                                                                             
                                                                                                                                                                       
                                                                                                                                                                       
                                                                                                                                                                       
                                                                                                                                                                       
                                                                                                                                                                       
                                                                                                                                                                       
                                                                                                                                                                       
                                                                                                                                                                       
        if [ $offcount1 -lt 6 ]; then                                                                                                                                  
                # give not responding devices 1 minute to respond                                                                                                      
                sleep 0                                                                                                                                                
        else                     

 c1_last_state="$c1_new_state"                                                                                                                          
                wget -qs "http://192.168.1.65:8080/json.htm?type=command&param=switchlight&idx=27&switchcmd=Off"                                                       
                                offcount1=0                                                                                                                            
        fi                                                                                                                                                             
                                                                                                                                                                       
        if [ $offcount2 -lt 6 ]; then                                                                                                                                  
                                                                                                                     
                sleep 0                                                                                                                                                
        else                                                                                                                                                           
                c2_last_state="$c2_new_state"                                                                                                                          
                wget -qs "http://192.168.1.65:8080/json.htm?type=command&param=switchlight&idx=26&switchcmd=Off"                                                       
                offcount2=0                                                                                                                                            
        fi                                                                                                                                                             
                                                                                                                                                                       
          if [ $offcount3 -lt 6 ]; then                                                                                                                                
                                                                                                                      
                sleep 0                                                                                                                                                
        else                                                                                                                                                           
                c3_last_state="$c3_new_state"                                                                                                                          
                wget -qs "http://192.168.1.65:8080/json.htm?type=command&param=switchlight&idx=28&switchcmd=Off"                                                       
                offcount3=0                                                                                                                                            
        fi                                                                                                                                                             
                                                                                                                                                                       
          if [ $offcount4 -lt 6 ]; then                                                                                                                                
                                                                                                                    
                sleep 0                                                                                                                                                
        else                                                                                                                                                           
                c4_last_state="$c4_new_state"                                                                                                                          
                wget -qs "http://192.168.1.65:8080/json.htm?type=command&param=switchlight&idx=29&switchcmd=Off"                                                       
                offcount4=0                                                                                                                                            
        fi                                                                                                                                                             
                                                                                                                                                                       
done        

Re: Toggle device?

Posted: Thursday 03 November 2016 18:12
by theolsen

Whats the problem with blocky?

if dummy <> on and (dummy24 = on OR dummy5 = on) then
set dummy = on
else if dummy = on and dummy24 <> on AND dummy5 <> on
set dummy = off
end if
Good call, I really don't know why I wasn't thinking like that :)


Thanks