Hi everyone,

Made a little script to dim my Zigbee lights gradually. You could do the same thing just with rules, but I wanted to do it this way. :) When the dim level gets changed by an other actor during the sequence, then the sequence stops.

# requires three parameters,
#1: name of the lamp/group,
#2: desired dimlevel,
#3: time in between dim steps

#get initial dimlevel
initial_dimlevel=$(curl\
        -X GET \
        --user "###:###" \
        http://192.168.1.10/api/variables/"${1}.dimlevel" 2> /dev/null | grep -o -E "value.{4,6}" | tr -dc '0-9')
echo initial dimlevel: $initial_dimlevel

#check arguments
if [ $# -lt 3 ]
then
        >&2 echo "error: no arguments, 3 arguments are needed: name of light, desired dim vallue, time in between steps"
        exit 1
fi

if [[ "$initial_dimlevel$2$3" =~ [A-Za-z] ]]
then
        >&2 echo "error: arguments 2,3 can only contain numbers"
        exit 1
fi


let difference_dimlevels=$initial_dimlevel-$2
if echo "$difference_dimlevels" | grep -q "-"
then
        positive_or_negative="+"
else
        positive_or_negative="-"
fi

difference_dimlevels=${difference_dimlevels//-} #removes the minus

current_dimlevel=1
dimvalue=1

for i in $( seq 1 $difference_dimlevels )
do
        if [[ $current_dimlevel = $dimvalue ]]
        then
                let dimvalue=$initial_dimlevel$positive_or_negative$i
                curl \
                        -X POST \
                        --header "Content-Type:application/json" \
                        --data '{"actionString": "dim '"$1"' to '"$dimvalue"'%"}' \
                        --user "###:###" \
                        http://192.168.1.10/api/execute-action &> /dev/null
                        sleep $3
                        current_dimlevel=$(curl\
                                                -X GET \
                                                --user "###:###" \
                                                http://192.168.1.10/api/variables/"${1}.dimlevel" 2> /dev/null | grep -o -E "value.{4,6}" | tr -dc '0-9')
        else
                >&2 echo "error: dim vallue has been changed by someone else, so this dim sequence has been aborted. current-dimlevel: $current_dimlevel dimvalue: $dimvalue"
                exit
        fi
done

Executed by the name of the script followed by device id of the light, the desired level of dim and the time between each step in the dim sequence:

${HOME}/bin/gradual_light_control.sh "raspbee_d6013c2ebb72a5c3def6a3e5e327cf73" 0 3 2> /dev/null