Hello. I'm quite new to NodeRed and to programming in general. I've done a few automations in my home using ESP8266+Blynk, but I'm trying to convert my Arduino devices to different apps like Homekit, Google Home or Home Assistant. I'm currently trying to control an LED lights state and intensity using Homebridge. Here is my flow:
I blurred out those nodes as they're just for experimenting. So here is my problem: I am able to control the intensity of the LED using this arduino callback function:
void callback(char* topic, byte* payload, unsigned int length) {
for (int i = 0; i < length; i++) {
payload[length] = '\0';
int pwmVal = atoi((char *)payload);
Serial.println(pwmVal);
if (strcmp(topic, "test/message") == 0){
int y=map(pwmVal, 0, 100, 0, 1024);
analogWrite(led, y);
if((char)payload[0]=='1'){
digitalWrite(led, HIGH);
}
else if((char)payload[0]=='0'){
digitalWrite(led, LOW);
}
}
}
}
And my 2 NodeRed functions look like this:
DimmerFunc should control intensity slider in Homekit app, and PowerFunc should only control the button(On,Off). Ok, so the button works, I am able to control the On/Off state of the LED with 1/0. However, when I'm using the slider, after each value sent to the MQTT topic a "1" is also received, something like this:
0 //Off
1 //On, next I'll adjust the slider
50
1
46
1
41
1 //And so on
So the light always stays on without changing its intensity. I'm assuming the 2 functions are somehow conflicting with each other. I don't understand how though. How could I bind them together and use both the slider and the button to control the LED? I'll also like to remember the last slider value, so when push the "On" button, the LED gets the intensity it last had. Any help is greatly apreciated!
Dragos