OK,
I could do with some help here. I connected some nodes to show whether:
a. the sound system is on or off (switch)
b. what the volume of the sound system is. (gauge)
I also created some buttons which set the volume to a certain level.
This is what it looks like:
At this point, it might be worth mentioning that I haven’t connected this to Homey yet. I first wanted to get the nodes connected properly.
For me, when the volume is zero, the system is off. I managed to get Node Red to automatically set the sound system (switch) to on/off when I select a certain level of volume or when using the button to set a preset volume. I also managed to set the volume (gauge) to zero when I turn off the sound system and I managed to set the volume (gauge) to the correct volume when I use a button to select a preset volume level. Still with me?
But now I want Node Red to remember the last volume level (example 8%) when I turn it off. When turning it on again, it should set it to the last known level again.
My current flow:
It shows the 4 buttons on the left, the volume-gauge called Sonos and the on/off switch (also called Sonos), which is preceded by a function whichs sets the input to either true or false (volume 0 > false, volume != 0 > true).
After the Sonos switch, you see a switch (the upper yellow one) which only sends the message when its false. That sets the gauge to 0 when the system is turned off using the switch.
Remember volume level
I made two switches, one after the gauge and one after the switch. The one after the gauge only sends the message when it’s not zero. This to prevent that the variable which needs to remember the volume level goes to zero when the system is turned off.
This switch then goes to the function below, which receives the message and sets a variable (var = volume) to this payload. The function only executes this part when the payload is not true. I tried to Javascript this:
if(msg.payload != true){ var volume = msg.payload; }
The other switch sends the message true to the function, in order to prevent that the on/off switch triggers the function when it’s turned off. So the function only receives a number somewhere in the range of 1-100 and the payload true. Now I want the function to send to value of the variable (volume) to the gauge. I tried to Javascript my way trough this again:
if (msg.payload = true){ msg.payload = volume; return msg; }
But I can’t get it right. I think it’s somewhere in the last piece of the function. I tried debugging, but I couldn’t figure it out. can anyone assist me?