HI,
Yes the command line is a bit more complicated because of the being a generic "catch all" scripte. It needs the ip adress of the led dimmer the 6 digits mac adress and the idx before the command begins.
e.g h801-leddimmer.sh
192.168.2.122 de979d 265 fun mixedcolortransistion.
or h801-leddimmer.sh
192.168.2.122 de979d 265 off
If you run the script if should state these examples as well.
Unfortunately I had to make the script even more complex due to it causing a loop when executed in domoitcz. When activated in Domoitcz the "status update" to domoticz re triggers the switch on again. this is very similar like this forum topic
viewtopic.php?f=11&t=2908. The only way to stop it I could find was setting a timer for for sending another update to domoticz. not ideal but it works.
In theory you can make the script a lot more simplistic if you like to have it have one script per led dimmer. all the input checks can then be removed as long as the IP, mac address and domoticz id is hardcoded.
Below I quickly removed checks within the Script and hardcode my h801 dimmer. but as said I rather have a bit more complex catch all script with the ip address etc in the (domoticz command line.
Code: Select all
#!/bin/bash
#########
#
# Bash remote script for controlling H801 WiFi led controller
# version 0.1 Evert
#
# 0.1 Simplistic script with hardcode leddimmer details based on h801-dimmer.sh script
#`
# dependency fping and netcat (nc)
# format hex string to led dimmer for color "fbeb RRGGBB WW(1) WW(2) MM(6) MM(5) MM(4) 00 " ( for multiple add the next 3 MAC otects MM(6) MM(5) MM(4) 00
# where "fbeb" is rgb color and "fbec" is fixed modes
# RR=Red, GG=Green, BB=Blue, WW(1)=White1, WW(2)=White2, MM(6)=Mac octet 6, MM(5)=Mac octet 5, MM(4)=Mac octet4
# e.g fbeb00000000005c979d00ad1a0200 or fbeb00000000005c979d00
# Input script example h801-leddimmer.sh 192.168.2.121 ad1a02 264 rgbww ff20000a00
#script:///home/pi/domoticz/scripts/bash/h801-leddimmer.sh 192.168.2.120 5c979d 263 comfort
#########
#
# Init
# Domoticz server
DOMO_SERVER="192.168.2.176"
IP="192.168.2.120"
MACTOTAL="5c979d"
DOMOIDX="263"
MODUS=$1
RGBWW=$2
SPECIALMODE=$2
#
## Functions
#
errormsg () {
loggerall "###### H801 WiFi controller script error message"
loggerall "# Script options are on, off, comfort, relax, reading, energize, concentrate, fun monochromelight, and rgbww RRGGBBW1W2"
loggerall "# When RRGGBBW1W2 is used RR is red in hex notation, GG is green, BB is Blue, W1 is white1 and W2 is white e.g rgbww 1213145000"
loggerall "# When fun is used options are fullcolor, monochromeshade, monochromefade, monochromelight, mixedcolorshade, mixedcolorfade,"
loggerall "# mixedcolorlight, redfade, greenfade, bluefade, monochrometransition, mixedcolortransistion, sevencolortransistion"
loggerall "# Input script example h801-leddimmer.sh 192.168.2.121 ad1a02 264 rgbww ff20000a00"
loggerall "#"
loggerall "# Input recieved was : $1 $2 $3 $4 $5"
loggerall "#"
loggerall "######"
}
loggerall () {
logger $1
echo $1
}
checkalive () {
loggerall "Checking host alive $1"
fping -c2 -t300 $IP 2>/dev/null 1>/dev/null #fping is not a standard packages and needs to be instaled
if [ "$?" = 0 ]
then
loggerall "Host $1 was found alive"
else
errormsg $1
loggerall "Host $1 was not found alive ( is fping installed)"
exit 1
fi
}
onofffunction () {
loggerall "Switching $1 $2"
#Set in rgb accept mode ( twice to make sure....)
#Set normalmode="fbec000100000079979d00" (example)
FULLRGBWW=fbec0001000000$MACTOTAL
COUNTER=0
while [ $COUNTER -lt 2 ]; do
echo -ne "$(echo $FULLRGBWW | sed -e 's/../\\x&/g')" | nc -u -w1 $1 30977
let COUNTER=COUNTER+1
done
#Set a color to let the power off/on work after the set mode
FULLRGBWW=fbebff20000a00$MACTOTAL
COUNTER=0
while [ $COUNTER -lt 2 ]; do
echo -ne "$(echo $FULLRGBWW | sed -e 's/../\\x&/g')" | nc -u -w1 $1 30977
let COUNTER=COUNTER+1
done
#The real on off command
COUNTER=0
while [ $COUNTER -lt 2 ]; do
if [ $2 = "off" ]; then FULLRGBWW=fbeb0000000000$MACTOTAL; echo -ne "$(echo $FULLRGBWW | sed -e 's/../\\x&/g')" | nc -u -w1 $1 30977; fi
if [ $2 = "on" ]; then FULLRGBWW=fbebff20000a00$MACTOTAL; echo -ne "$(echo $FULLRGBWW | sed -e 's/../\\x&/g')" | nc -u -w1 $1 30977; fi
let COUNTER=COUNTER+1
done
#Update domoticz
if [ $2 = "on" ] ; then updatedomoticz $IP 1 ; fi
if [ $2 = "off" ]; then updatedomoticz $IP 0 ; fi
}
rgbwwfunction () {
loggerall "Switching $1 in rgbww $2"
#Set in rgb accept mode
#Set normalmode="fbec001e00000079979d00"
FULLRGBWW=fbec0001000000$MACTOTAL
COUNTER=0
while [ $COUNTER -lt 2 ]; do
echo -ne "$(echo $FULLRGBWW | sed -e 's/../\\x&/g')" | nc -u -w1 $1 30977
let COUNTER=COUNTER+1
done
#Set the rgb value
FULLRGBWW=fbeb$2$MACTOTAL
COUNTER=0
while [ $COUNTER -lt 2 ]; do
echo -ne "$(echo $FULLRGBWW | sed -e 's/../\\x&/g')" | nc -u -w1 $1 30977
let COUNTER=COUNTER+1
done
updatedomoticz $IP 1
}
funfunction () {
loggerall "Switching $1 in fun $2"
#Set in in specific mode
SPECIALRGBWW=fbec$2$MACTOTAL
COUNTER=0
while [ $COUNTER -lt 2 ]; do
echo -ne "$(echo $SPECIALRGBWW | sed -e 's/../\\x&/g')" | nc -u -w1 $1 30977
let COUNTER=COUNTER+1
done
updatedomoticz $IP 1
}
updatedomoticz (){
#
# Check the difference in last update time stamp with current time stamp to see if there was a wrong trigger
#
TIME=`date -d"$(curl --request GET "http://$DOMO_SERVER/json.htm?type=devices&rid=$DOMOIDX" | grep "LastUpdate" | awk ' {print ($3=(substr($3,2,10))),($4=(substr($4,0,9)))}')" +%s`
CURRENT_TIME=`date +"%s"`
DIFFERENCE=$(($CURRENT_TIME - $TIME))
if (( "$DIFFERENCE" > 30))
then
loggerall "Updating $1 Domoticz status time differance bigger than 30 "
curl --request GET "http://$DOMO_SERVER/json.htm?type=command¶m=udevice&idx=$DOMOIDX&nvalue=$2"&
sleep 2
else
loggerall "NOT updated $1 Domoticz status due to difference not smaller than 30 "
sleep 2
fi
}
#######################
#
## Program
#
#######################
case "$MODUS:$SPECIALMODE" in
"-?":*) errormsg ;;
## begin list of commands.
"on":*) onofffunction $IP on ;;
"off":*) onofffunction $IP off ;;
"comfort":*) rgbwwfunction $IP ff20000a00 ;;
"relax":*) rgbwwfunction $IP c8fa002000 ;;
"reading":*) rgbwwfunction $IP f2f2f20000 ;;
"energize":*) rgbwwfunction $IP 00d0ff0000 ;;
"concentrate":*) rgbwwfunction $IP 00c5db0000 ;;
"rgbww":*) rgbwwfunction $IP $RGBWW ;;
"fun":"monochromeshade") funfunction $IP 0201000000 ;;
"fun":"monochromefade") funfunction $IP 0301000000 ;;
"fun":"monochromelight") funfunction $IP 0401000000 ;;
"fun":"mixedcolorshade") funfunction $IP 0501000000 ;;
"fun":"mixedcolorfade") funfunction $IP 0601000000 ;;
"fun":"mixedcolorlight") funfunction $IP 0701000000 ;;
"fun":"redfade") funfunction $IP 0801000000 ;;
"fun":"greenfade") funfunction $IP 0901000000 ;;
"fun":"bluefade") funfunction $IP 0a01000000 ;;
"fun":"monochrometransition") funfunction $IP 0b01000000 ;;
"fun":"mixedcolortransistion") funfunction $IP 0c01000000 ;;
"fun":"sevencolortransistion") funfunction $IP 0d01000000 ;;
*:*) errormsg
loggerall "Error: no valid input for mode recieved" ;;
esac
echo done
exit 0
orginial script with input checks and loop prevent timer.
Code: Select all
#!/bin/bash
#########
#
# Bash remote script for controlling H801 WiFi led controller
# version 0.4 Evert
#
# 0.4 added udatedomotiz funtion to prevent "flapping"/keep on swithing device
#`
# dependency fping and netcat (nc)
# format hex string to led dimmer for color "fbeb RRGGBB WW(1) WW(2) MM(6) MM(5) MM(4) 00 " ( for multiple add the next 3 MAC otects MM(6) MM(5) MM(4) 00
# where "fbeb" is rgb color and "fbec" is fixed modes
# RR=Red, GG=Green, BB=Blue, WW(1)=White1, WW(2)=White2, MM(6)=Mac octet 6, MM(5)=Mac octet 5, MM(4)=Mac octet4
# e.g fbeb00000000005c979d00ad1a0200 or fbeb00000000005c979d00
# Input script example h801-leddimmer.sh 192.168.2.121 ad1a02 264 rgbww ff20000a00
#########
#
# Init
# Domoticz server
DOMO_SERVER="192.168.2.176"
#
## Functions
#
errormsg () {
loggerall "###### H801 WiFi controller script error message"
loggerall "# Script options are on, off, comfort, relax, reading, energize, concentrate, fun monochromelight, and rgbww RRGGBBW1W2"
loggerall "# When RRGGBBW1W2 is used RR is red in hex notation, GG is green, BB is Blue, W1 is white1 and W2 is white e.g rgbww 1213145000"
loggerall "# When fun is used options are fullcolor, monochromeshade, monochromefade, monochromelight, mixedcolorshade, mixedcolorfade,"
loggerall "# mixedcolorlight, redfade, greenfade, bluefade, monochrometransition, mixedcolortransistion, sevencolortransistion"
loggerall "# Input script example h801-leddimmer.sh 192.168.2.121 ad1a02 264 rgbww ff20000a00"
loggerall "#"
loggerall "# Input recieved was : $1 $2 $3 $4 $5"
loggerall "#"
loggerall "######"
}
loggerall () {
logger $1
echo $1
}
checkalive () {
loggerall "Checking host alive $1"
fping -c2 -t300 $IP 2>/dev/null 1>/dev/null #fping is not a standard packages and needs to be instaled
if [ "$?" = 0 ]
then
loggerall "Host $1 was found alive"
else
errormsg $1
loggerall "Host $1 was not found alive ( is fping installed)"
exit 1
fi
}
onofffunction () {
loggerall "Switching $1 $2"
#Set in rgb accept mode ( twice to make sure....)
#Set normalmode="fbec000100000079979d00" (example)
FULLRGBWW=fbec0001000000$MACTOTAL
COUNTER=0
while [ $COUNTER -lt 2 ]; do
echo -ne "$(echo $FULLRGBWW | sed -e 's/../\\x&/g')" | nc -u -w1 $1 30977
let COUNTER=COUNTER+1
done
#Set a color to let the power off/on work after the set mode
FULLRGBWW=fbebff20000a00$MACTOTAL
COUNTER=0
while [ $COUNTER -lt 2 ]; do
echo -ne "$(echo $FULLRGBWW | sed -e 's/../\\x&/g')" | nc -u -w1 $1 30977
let COUNTER=COUNTER+1
done
#The real on off command
COUNTER=0
while [ $COUNTER -lt 2 ]; do
if [ $2 = "off" ]; then FULLRGBWW=fbeb0000000000$MACTOTAL; echo -ne "$(echo $FULLRGBWW | sed -e 's/../\\x&/g')" | nc -u -w1 $1 30977; fi
if [ $2 = "on" ]; then FULLRGBWW=fbebff20000a00$MACTOTAL; echo -ne "$(echo $FULLRGBWW | sed -e 's/../\\x&/g')" | nc -u -w1 $1 30977; fi
let COUNTER=COUNTER+1
done
#Update domoticz
if [ $2 = "on" ] ; then updatedomoticz $IP 1 ; fi
if [ $2 = "off" ]; then updatedomoticz $IP 0 ; fi
}
rgbwwfunction () {
loggerall "Switching $1 in rgbww $2"
#Set in rgb accept mode
#Set normalmode="fbec001e00000079979d00"
FULLRGBWW=fbec0001000000$MACTOTAL
COUNTER=0
while [ $COUNTER -lt 2 ]; do
echo -ne "$(echo $FULLRGBWW | sed -e 's/../\\x&/g')" | nc -u -w1 $1 30977
let COUNTER=COUNTER+1
done
#Set the rgb value
FULLRGBWW=fbeb$2$MACTOTAL
COUNTER=0
while [ $COUNTER -lt 2 ]; do
echo -ne "$(echo $FULLRGBWW | sed -e 's/../\\x&/g')" | nc -u -w1 $1 30977
let COUNTER=COUNTER+1
done
updatedomoticz $IP 1
}
funfunction () {
loggerall "Switching $1 in fun $2"
#Set in in specific mode
SPECIALRGBWW=fbec$2$MACTOTAL
COUNTER=0
while [ $COUNTER -lt 2 ]; do
echo -ne "$(echo $SPECIALRGBWW | sed -e 's/../\\x&/g')" | nc -u -w1 $1 30977
let COUNTER=COUNTER+1
done
updatedomoticz $IP 1
}
updatedomoticz (){
#
# Check the difference in last update time stamp with current time stamp to see if there was a wrong trigger
#
TIME=`date -d"$(curl --request GET "http://$DOMO_SERVER/json.htm?type=devices&rid=$DOMOIDX" | grep "LastUpdate" | awk ' {print ($3=(substr($3,2,10))),($4=(substr($4,0,9)))}')" +%s`
CURRENT_TIME=`date +"%s"`
DIFFERENCE=$(($CURRENT_TIME - $TIME))
if (( "$DIFFERENCE" > 30))
then
loggerall "Updating $1 Domoticz status time differance bigger than 30 "
curl --request GET "http://$DOMO_SERVER/json.htm?type=command¶m=udevice&idx=$DOMOIDX&nvalue=$2"&
sleep 2
else
loggerall "NOT updated $1 Domoticz status due to difference not smaller than 30 "
sleep 2
fi
}
#
## Check and process input
#
#Check if a minimum one input supplied
if [[ $# -lt 4 ]] #Check to see if at least one parameter is supplied
then
errormsg $1 $2 $3 $4 $5
loggerall "Error: A minimum of four inputs please"
exit 1
fi
#Check that first input is valid IPv4 IP address and alive
#Declare IP variable
if [[ "$(echo $1 | grep '[0-9]\{1,3\}[.][0-9]\{1,3\}[.][0-9]\{1,3\}[.][0-9]\{1,3\}')" ]]
then
loggerall "Ip address used '$1' "
IP=$1
checkalive $IP
else
errormsg $1 $2 $3 $4 $5
loggerall "Ip address not valid '$1'"
exit 1
fi
#Declare second input as led dimmer mac address id
if [[ ! $2 =~ ^-?[0-9a-fA-F]+$ ]] #Check to see if only numbers and characters a till f (hex) are supplied
then
errormsg $1 $2 $3 $4 $5
loggerall "Error: Mac address address in format Mac6 MAC5 MAC4 in hex"
exit 1
else
MACTOTAL=$2
declare -l MACTOTAL
MACTOTAL=$MACTOTAL"00" #adding the trailing 00 as well
fi
#Declare the third input as DOMOIDX
if [[ ! $3 =~ ^-?[0-9]+$ ]]
then
errormsg $1 $2 $3 $4 $5
loggerall "Error: Domoticz idx must be a number"
exit 1
else
DOMOIDX=$3
fi
#Declare fourth input as variable MODUS all lowercase
if [[ ! $4 =~ ^-?[a-zA-Z]+$ ]] #Check to see if only characters are supplied
then
errormsg $1 $2 $3 $4 $5
loggerall "Error: Mode input should only be characters"
exit 1
else
MODUS=$4
declare -l MODUS # Define lower case
MODUS=$MODUS
fi
#Make sure that if rgbww or fun is selected as modus a 5th input is there
if [[ $4 = "rgbww" && $5 = "" ]] || [[ $4 = "fun" && $5 = "" ]]
then
errormsg $1 $2 $3 $4 $5
loggerall "Error: If rgbww or fun is the input a second input is required"
exit 1
fi
#Declare fith input as RGBWW if fourth input is rgbww
if [[ $MODUS = "rgbww" && ! $5 = "" ]]
then
if [[ ! $5 =~ ^-?[0-9a-fA-F]+$ ]] #Check to see if only numbers and characters a till f are supplied
then
errormsg $1 $2 $3 $4 $5
loggerall "Error: rgb w1 w2 value can only be hex numbers"
exit 1
else
if [[ ! ${#5} = 10 ]] # Check that input string is 10 long
then
errormsg $1 $2 $3 $4 $5
loggerall "Error: rgb w1 w2 must be 10 hex numbers"
exit 1
else
RGBWW=$5
declare -l RGBWW
RGBWW=$RGBWW
fi
fi
fi
#Declare fith input as SPECIALMODE if fourth input is fun
if [[ $MODUS = "fun" && ! $5 = "" ]]
then
if [[ ! $5 =~ ^-?[a-zA-Z]+$ ]] #Check to see if characters (a till z) are supplied
then
errormsg $1 $2 $3 $4 $5
loggerall "Error: With the argument fun there must be a specialmode defined"
exit 1
else
SPECIALMODE=$5
declare -l SPECIALMODE
SPECIALMODE=$SPECIALMODE
fi
fi
#######################
#
## Program
#
#######################
case "$MODUS:$SPECIALMODE" in
"-?":*) errormsg ;;
## begin list of commands.
"on":*) onofffunction $IP on ;;
"off":*) onofffunction $IP off ;;
"comfort":*) rgbwwfunction $IP ff20000a00 ;;
"relax":*) rgbwwfunction $IP c8fa002000 ;;
"reading":*) rgbwwfunction $IP f2f2f20000 ;;
"energize":*) rgbwwfunction $IP 00d0ff0000 ;;
"concentrate":*) rgbwwfunction $IP 00c5db0000 ;;
"rgbww":*) rgbwwfunction $IP $RGBWW ;;
"fun":"monochromeshade") funfunction $IP 0201000000 ;;
"fun":"monochromefade") funfunction $IP 0301000000 ;;
"fun":"monochromelight") funfunction $IP 0401000000 ;;
"fun":"mixedcolorshade") funfunction $IP 0501000000 ;;
"fun":"mixedcolorfade") funfunction $IP 0601000000 ;;
"fun":"mixedcolorlight") funfunction $IP 0701000000 ;;
"fun":"redfade") funfunction $IP 0801000000 ;;
"fun":"greenfade") funfunction $IP 0901000000 ;;
"fun":"bluefade") funfunction $IP 0a01000000 ;;
"fun":"monochrometransition") funfunction $IP 0b01000000 ;;
"fun":"mixedcolortransistion") funfunction $IP 0c01000000 ;;
"fun":"sevencolortransistion") funfunction $IP 0d01000000 ;;
*:*) errormsg
loggerall "Error: no valid input for mode recieved" ;;
esac
echo done
exit 0