Closed Raynman77 closed 3 years ago
Looking at the serial monitor, I noticed that the fan only turns off when it's value is 0. If the last value was above 0 and the off command is issued, the fan doesn't turn off. It's like my conditional statement is being ignored when checking the state whether I set the condition to be LOW or even false.
When you send an "Off" command via Alexa (ie: "Turn light off"), the light's value is preserved, so that next time a light is turned on it comes back to the same brightness. I would suggest using ((state == "OFF") || (value==0))
When you send an "Off" command via Alexa (ie: "Turn light off"), the light's value is preserved, so that next time a light is turned on it comes back to the same brightness. I would suggest using
((state == "OFF") || (value==0))
Yes, I see it is preserving the value. However when I issue the command above in my 1st post to this issue, I have the condition wrote to turn the fan off if the fan is the device name and the state is false and it is not turning the fan off. I thought that if the state received from the call back is false then that should satisfy my condition above since the state is a bool value.
Also, you suggested using ((state == "OFF") || (value == 0))
But that won't work here because:
error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if (strcmp(device_name,"Office Fan") == 0 && ((state == "OFF") || (value == 0))) mySwitch.send(3238165, 24); // fan off
This conditional statement above is within the fauxmo.onSetState()
callback function.
My apologies - state
is a boolean, so try:
if (strcmp(device_name,"Office Fan") == 0 && (state || (value == 0))) mySwitch.send(3238165, 24); // fan off
My apologies -
state
is a boolean, so try:if (strcmp(device_name,"Office Fan") == 0 && (state || (value == 0))) mySwitch.send(3238165, 24); // fan off
I tried all kind of different combinations to get my conditional statement to execute properly. From setting a bool type global variable with the state's value to even adding code to the main loop to execute based on my conditions and still no dice. What finally did work for me was when I decided to create a function for fan operation and then created a switch statement in the "onSetState" function to run my function if state is true otherwise send the off command.
`void fanOps() { // low speed operation if((fan_val > 0) && (fan_val <= 85)) { mySwitch.send(3238277, 24); }
// medium speed operation
if((fan_val > 85) && (fan_val <= 170)) {
mySwitch.send(3238213, 24);
}
// high speed operation
if((fan_val > 170) && (fan_val <= 255)) {
mySwitch.send(3238341, 24);
}
}`
and then..... `fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) { Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
if (strcmp(device_name,"Office Fan Light") == 0) mySwitch.send(3238357, 24); // light control
if (strcmp(device_name,"Office Fan") == 0) fan_val = value;
if (strcmp(device_name,"Office Fan") == 0) state ? fanOps() : mySwitch.send(3238165, 24);
});`
Sorry, github mangled this code. Anyway this worked beautifully. The "value" was able to be stored to a global variable but the "state" seemed like it was not able to be stored, only read immediately upon the "onSetState" callback function.
I'm having a little trouble wrapping my head around why I can only turn off this virtual device once. After that, Alexa accepts the device command "off" and sets the state accordingly, but my code is not turning it off. Here is a snippet of the code. The //fan off line is where the problem occurs. Any ideas, or anything wrong with this?
fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) { if (strcmp(device_name,"office fan light") == 0) mySwitch.send(3238357, 24); // light control if (strcmp(device_name,"office fan") == 0 && (value > 0 && value <= 85)) mySwitch.send(3238277, 24); // low speed if (strcmp(device_name,"office fan") == 0 && (value > 85 && value <= 170)) mySwitch.send(3238213, 24); // medium speed if (strcmp(device_name,"office fan") == 0 && (value > 170 && value <= 254)) mySwitch.send(3238341, 24); // high speed if (strcmp(device_name,"office fan") == 0 && state == LOW) mySwitch.send(3238165, 24); // fan off if (strcmp(device_name,"office fan") == 0 && state) mySwitch.send(3238341, 24); // fan on (high) Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value); });
I would have inserted all of this as code but github keeps mangling it...sorry.