Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

Kan iemand me helpen met een Wake-up light achtig schript?

Pagina: 1
Acties:

  • timmiej93
  • Registratie: Oktober 2010
  • Laatst online: 29-10 07:16
Hoi mensen,

Zoals de titel al zegt, ben ik aan het proberen een "wake-up light achtig" script te maken, om te gebruiken in Domoticz (aansturing LED-strip).
Ik heb wat dingen geprobeerd in lua scripts, omdat ik daar enige ervaring in heb, maar ik kwam al snel er achter dat lua geen goede taal is om in Domoticz iets te doen waarbij je moet wachten, aangezien je dan verder niets meer kan doen.

Wat ik probeer te doen in z'n totaal: Op basis van het eerstvolgende alarm (via Tasker op m'n telefoon, geen onderdeel van de vraag) mijn LED-strip langzaam laten oplichten.

Het probleem ligt dus vooral bij het langzaam laten oplichten van de LED-strip.

Basis code idee:
code:
1
2
3
4
5
6
if (alarm over x minuten) {
   for (i, <20, ++) {
      brightness +1
      wait(tijdsduur)
   }
}


Zoiets heb ik dus in m'n hoofd, en dan het liefst een mogelijkheid om bij het uitschakelen van het alarm de sterkte van de lampen in één keer op 20 te zetten, of op z'n minst de mogelijkheid om het script te onderbreken (niet zo handig als ik na 5 minuten opsta en de lampen nog een half uur steeds feller gaan branden).

Als iemand me de goede richting op kan sturen zou dat al enorm gewaardeerd worden!

Tim

  • timmiej93
  • Registratie: Oktober 2010
  • Laatst online: 29-10 07:16
Natuurlijk, nadat je zo'n vraag stelt vind je zelf vanalles. Het is me dus inmiddels gelukt om dit helemaal klaar te krijgen, dus hierbij:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash
#
#   WakeUpLight script
#
#   This script is designed to controll one single light source, but can easilly be extended to controll more.
#   The script has some settings that need to be set to work properly.
#
#
#   Workings:
#     1.  Uses the dimmer idx 'Didx' to make sure all lights are off, then waits for 5 seconds (set time, to be sure everything is off).
#     2.  Sets the 'InitLevel' brightness (usually 1%), and then waits for the duration of 'InitSleep' (used to start slowly).
#     3.  Adds the 'StepLevel' to the brightness level, then waits for the duration of 'StepSleep' .
#     4.  Repeats for the amount of times set by 'StepsNumber'.






#  -------------------------------------------------------------------------------------------------------------  #
#  -------------------------------------------------------------------------------------------------------------  #
#
#  Base settings
#
#  -------------------------------------------------------------------------------------------------------------  #
#  -------------------------------------------------------------------------------------------------------------  #


    #RPi Settings
        username=<username>     #Username, only if required for logging in to Domoticz
        password=<password>     #Password, same as above
        Address=127.0.0.1       #RPi IP address. If running from the same RPi, IP is 127.0.0.1
        Port=8080               #Port Number. If not changed, is 8080

    #Domoticz Settings
        #Dimmer idx number
            Didx=13     #Dimmer

        #Light idx number
            #idx=16     #Red
            #idx=17     #Green
            #idx=18     #Blue
            idx=19      #White

    #Timing Settings
        #Manual Settings
            MInitLevel=1            #Start brightness [%]
            MInitSleep=60           #Time between first lighting up and changing to the next value up [sec]

            MStepLevel=1            #Amount brightness increases with every step [%]
            MStepSleep=30           #Time between each step (except for time between first turning on and next step) [sec]

            MStepsNumber=20         #Amount of steps / brightnesschanges [#]

        #Automatic Settings
            #Set values for 'TotalDuration', 'InitDuration', 'InitBrightness' and 'TargetPercent' to enable
            #   automatic settings, based on the total time it should take for the WakeUpLight to go to its full
            #   brightness. If you fail to set a value for one of the variables, this function will not work,
            #   and the script will revert to the 'Manual Settings'.
            #
            #Step time calculation will be rounded down to whole seconds. So i.e. 20/11 (would be 1.81) will
            #   become 1.
            #
            #Step level for automatic settings is always 1.

            ATotalDuration=         #Total time from switching on to full brightness [min]
            ATargetPercent=         #Percentage of brightness the script should end at [%]

            AInitLevel=             #Initial brightness level, which stays on for 'AInitSleep' [%]
            AInitSleep=             #Time initial brightness stays on before switching to time between steps [sec]



#  -------------------------------------------------------------------------------------------------------------  #
#  -------------------------------------------------------------------------------------------------------------  #
#
#  Start of script, don't alter without experience!
#
#  -------------------------------------------------------------------------------------------------------------  #
#  -------------------------------------------------------------------------------------------------------------  #


#Save PID
echo $$ > /home/pi/domoticz/scripts/WuL.pid


#Dummy's
DummyTest=False


#WakeUp Procedure
#Initialization, writing to log
    echo "WuL ---- WakeUp light script started"
    echo "WuL ---- Initializing, turning everything off"


#Check if Manual, Automatic or Test mode
    if [ -n "$1" ] && [ "$1" == "-test" ]
    then
    #Running in test mode
        echo "WuL ---- Running in test mode"
        InitLevel=1
        InitSleep=3
        StepLevel=1
        StepSleep=1
        StepsNumber=10
        DummyTest=True
    else
        if [ -n "$ATotalDuration" ] && [ -n "$ATargetPercent" ] && [ -n "$AInitLevel" ] && [ -n "$AInitSleep" ]
        then
        #Non-null, so Automatic settings set and will be used
            echo 'WuL ---- Automatic Settings enabled'
            InitLevel=$AInitLevel
            InitSleep=$AInitSleep
            StepLevel=1
            StepSleep=$(((ATotalDuration*60)/ATargetPercent))
            StepsNumber=$ATargetPercent

            if [ "$StepSleep" -lt 1 ]
            then
                StepSleep=1
            fi
        else
        #Null, so Manual settings will be used
            echo 'WuL ---- Manual Settings enabled'
            InitLevel=$MInitLevel
            InitSleep=$MInitSleep
            StepLevel=$MStepLevel
            StepSleep=$MStepSleep
            StepsNumber=$MStepsNumber
        fi
    fi
    echo "WuL ---- WakeUp light script running"



#Calculation for cancelation procedure
    if [ -n "$ATotalDuration" ] && [ -n "$ATargetPercent" ] && [ -n "$AInitLevel" ] && [ -n "$AInitSleep" ]
    then
    #Non-null, so Automatic settings set and will be used, so 'ATargetPercent' will be used.
        CancelLevel=$ATargetPercent
    else
    #Null, so Manual settings will be used, so 'CancelLevel' should be 'MInitLevel'+('MStepLevel'*'MStepsNumber')
        CancelLevel=$(((MInitLevel+(MStepLevel*MStepsNumber))))
    fi
    if [ "$DummyTest"=="True" ]
    then
        CancelLevel=21
    fi

#Cancelation procedure initialization
#  --------------------------------------------------------------------------------------------------------------
#  !!!!!  #
#  DO NOT TOUCH, MAY RUIN YOUR MACHINE #
#  !!!!!  #
#  --------------------------------------------------------------------------------------------------------------

trap '{
    echo "";
    echo "WuL ---- Script terminated manually. Setting light to finishing value immediately";
    curl -s "$username:$password@$Address:$Port/json.htm?type=command&param=switchlight&idx=$idx&switchcmd=Set%20Level&level=$CancelLevel" > /dev/null
#   curl -s "$username:$password@$Address:$Port/json.htm?type=command&param=switchlight&idx=16&switchcmd=Set%20Level&level=100" > /dev/null
    echo "";
    rm -f /home/pi/domoticz/scripts/WuL.pid;
    exit 1;
}' INT

#  --------------------------------------------------------------------------------------------------------------
#  End cancelation procedure
#  --------------------------------------------------------------------------------------------------------------



KillFunction () {
    echo "WuL ---- Switch turned off, ending script"
    kill -INT $$
}

CheckState () { #Arguments: CheckState <SwitchName to check>
    SwitchName=$1
    Output=$(curl -s -i -H "Accept: application/json" "$username:$password@$Address:$Port/json.htm?type=scenes" | egrep -w -A10 "$SwitchName" | grep "Status")
    if [[ "$Output" == *'"Off"'* ]]
    then
        echo "off"
    fi
    if [[ "$Output" == *'"On"'* ]]
    then
        echo "on"
    fi
}

SleepFunction () {  #Arguments: SleepFunction <SwitchName to check>
    SwitchName=$2
    if [ "$1" == "InitSleep" ]
    then
        SleepTime=$InitSleep
    elif [ "$1" == "StepSleep" ]
    then
        SleepTime=$StepSleep
    fi

    i=1
    while [ $i -lt $SleepTime ]
    do
        if [ "$SleepTime" -le 5 ]
        then
            sleep $SleepTime
            i=$((i+SleepTime))
        elif [ "$SleepTime" -gt 5 ]
        then
            if [[ $(CheckState $SwitchName) == "on" ]]
            then
                if [ $((SleepTime-i)) -lt 5 ]
                then
                    sleep $((SleepTime-i))
                    i=$((i+SleepTime))
                else
                    sleep 5
                    i=$((i+5))
                fi
            elif [[ $(CheckState $SwitchName) == "off" ]]
            then
                KillFunction
            fi
        fi
    i=$((i+1))
    done
}



#Run
    for i in `seq 1 $StepsNumber`; do
        SwitchName=Test
        if [[ $(CheckState $SwitchName) == "off" ]]
        then
            KillFunction
        elif [[ $(CheckState $SwitchName) == "on" ]]
        then
            InitLevelAdj=$(($InitLevel+1))
            StepLevelMultiplied=$((($StepLevel*$i)+1))
            if [ $StepLevelMultiplied -gt 100 ]
            then
                StepLevelMultiplied=100
                echo "WuL ---- StepLevelMultiplied above 100, setting to 100"
            fi

            if [ $i -eq 1 ]
            then
                curl -s "$username:$password@$Address:$Port/json.htm?type=command&param=switchlight&idx=$Didx&switchcmd=Off" > /dev/null
                sleep 5
                curl -s "$username:$password@$Address:$Port/json.htm?type=command&param=switchlight&idx=$idx&switchcmd=Set%20Level&level=$InitLevelAdj" > /dev/null
                SleepFunction InitSleep $InitSleep
            elif [ $i -gt 1 ]
            then
                curl -s "$username:$password@$Address:$Port/json.htm?type=command&param=switchlight&idx=$idx&switchcmd=Set%20Level&level=$StepLevelMultiplied" > /dev/null
                SleepFunction StepSleep $StepSleep
            else
                echo "WuL ---- i ($i) out of range"
            fi
        else
            echo "Stuff is broken, switch is neither on or off"
        fi
    done

rm -f /home/pi/domoticz/scripts/WuL.pid;
echo "WuL ---- WakeUp light script finished succesfully"