Function question

Hi,

i try to add a function in my flow.

i have 2 inputs:
playstate of my sonos speaker = msg.payload.play
powerstate of my kenwood amp = msg.payload.kenwood

I want toggle my Amp, if the playstate and powerstate are not identical.
The Flow change the inputs of the nodes to "true" and "false" for the dashboard switches.
After i changed it in numbers 1,0 for the function.

For toggle my Amp i only have one trigger with a http request to an arduino IR Remote.

Second:
if i pause or stop the Sonos Playstate, the amp should not switch immediately

May you help me?

Thanks


My function is:

var m_out=[];
var play=msg.payload.play;
var kenwood=msg.payload.kenwood;

if (play !== kenwood){
    m_out = {payload:1}
} else null;

return msg;

Flow:

[{"id":"7c9b27df.0bc7d8","type":"tab","label":"Kenwood","disabled":false,"info":""},{"id":"5df92dda.dfb934","type":"http request","z":"7c9b27df.0bc7d8","name":"","method":"GET","ret":"txt","paytoqs":false,"url":"http://10.2.0.65/ir?code=501397830","tls":"","persist":false,"proxy":"","authType":"","x":1190,"y":300,"wires":[[]]},{"id":"367ccdfb.1ecbd2","type":"mqtt in","z":"7c9b27df.0bc7d8","name":"","topic":"shellies/shelly-wohnzimmer-kenwood/relay/0/power","qos":"0","datatype":"auto","broker":"8aa7c4c7.b6d308","x":250,"y":300,"wires":[["26017241.60409e"]]},{"id":"26017241.60409e","type":"switch","z":"7c9b27df.0bc7d8","name":">= 10 <= 10","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"10","vt":"str"},{"t":"lte","v":"10","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":570,"y":300,"wires":[["60a41f75.b66858"],["fdf91316.f38878"]]},{"id":"60a41f75.b66858","type":"change","z":"7c9b27df.0bc7d8","name":"An","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"kenwood","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":280,"wires":[["145376e3.77b791","dd123a68.ad29e"]]},{"id":"fdf91316.f38878","type":"change","z":"7c9b27df.0bc7d8","name":"Aus","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"kenwood","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":320,"wires":[["145376e3.77b791","dd123a68.ad29e"]]},{"id":"f6ba6b85.8e56c","type":"comment","z":"7c9b27df.0bc7d8","name":"Schalter Kenwood Anlage","info":"","x":150,"y":40,"wires":[]},{"id":"ebbbb832.a8e74","type":"change","z":"7c9b27df.0bc7d8","name":"AN","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":100,"wires":[["5a81caf4.f5dbf4"]]},{"id":"7d3519c8.f761c8","type":"switch","z":"7c9b27df.0bc7d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"playing","vt":"str"},{"t":"eq","v":"paused","vt":"str"},{"t":"eq","v":"stopped","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":370,"y":120,"wires":[["ebbbb832.a8e74"],["bbd4a08f.eda1a"],["bbd4a08f.eda1a"]]},{"id":"bbd4a08f.eda1a","type":"change","z":"7c9b27df.0bc7d8","name":"AUS","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":140,"wires":[["5a81caf4.f5dbf4"]]},{"id":"5a81caf4.f5dbf4","type":"ui_switch","z":"7c9b27df.0bc7d8","name":"","label":"Playstate","tooltip":"","group":"87b21765.57daa8","order":12,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":740,"y":120,"wires":[["319eb34a.a9f5dc"]]},{"id":"adce622b.eefea","type":"link in","z":"7c9b27df.0bc7d8","name":"Sonos Playstate Wohnzimmer","links":["8026b813.baa0b"],"x":235,"y":120,"wires":[["7d3519c8.f761c8"]]},{"id":"d5746361.9c5248","type":"debug","z":"7c9b27df.0bc7d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1650,"y":180,"wires":[]},{"id":"145376e3.77b791","type":"ui_switch","z":"7c9b27df.0bc7d8","name":"","label":"Kenwood Anlage","tooltip":"","group":"87b21765.57daa8","order":11,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":950,"y":300,"wires":[["5df92dda.dfb934"]]},{"id":"319eb34a.a9f5dc","type":"change","z":"7c9b27df.0bc7d8","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.play","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":960,"y":120,"wires":[["186cecfe.5ba32b"]]},{"id":"dd123a68.ad29e","type":"change","z":"7c9b27df.0bc7d8","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.kenwood","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":960,"y":240,"wires":[["186cecfe.5ba32b"]]},{"id":"186cecfe.5ba32b","type":"change","z":"7c9b27df.0bc7d8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"switch","tot":"str"},{"t":"change","p":"payload.play","pt":"msg","from":"true","fromt":"bool","to":"1","tot":"num"},{"t":"change","p":"payload.play","pt":"msg","from":"false","fromt":"bool","to":"0","tot":"num"},{"t":"change","p":"payload.kenwood","pt":"msg","from":"true","fromt":"bool","to":"1","tot":"num"},{"t":"change","p":"payload.kenwood","pt":"msg","from":"false","fromt":"bool","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1220,"y":180,"wires":[["78496e03.fe4d18"]]},{"id":"78496e03.fe4d18","type":"function","z":"7c9b27df.0bc7d8","name":"","func":"var m_out=[];\nvar play=msg.payload.play;\nvar kenwood=msg.payload.kenwood;\n\nif (play !== kenwood){\n    m_out = {payload:1}\n} else null;\n\nreturn msg;","outputs":1,"noerr":0,"x":1430,"y":180,"wires":[["d5746361.9c5248"]]},{"id":"8aa7c4c7.b6d308","type":"mqtt-broker","z":"","name":"Mosquitto","broker":"10.0.0.171","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"87b21765.57daa8","type":"ui_group","z":"","name":"Main","tab":"7c6fa6e7.e5b808","order":1,"disp":true,"width":8,"collapse":false},{"id":"7c6fa6e7.e5b808","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Messages never arrive at the same time so you need to either store the values or use a join node.

See this article in the cookbook for an example of how to join messages into one object.

Thank you.

now the Inputs come at the same time.

I stuck in the function:
var play=msg.payload.play;
var kenwood=msg.payload.kenwood;

if (play !== kenwood){
???????
} else null;

return msg;

yeah - ok - so what are you trying to do?

if you want to halt the flow of a msg, you can return null...

var play=msg.payload.play;
var kenwood=msg.payload.kenwood;

if (play !== kenwood){
  //what do you want to happen here?
  msg.payload = 1;   //e.g. set msg.payload?
} else {
  //return null to stop the msg travelling to next node
  return null;
}

return msg; //send msg to next node by returning the msg.

play can be: true/false
kenwood can be true/false
(i can change it in numbers if easier)

if play and kenwood are both true, do nothing
if play and kenwood are both false, do nothing
if play true and kenwood false, trigger next steps
if play false and kenwood true, trigger next steps

next step is a single node with http request to toggle the kenwood amp power (on and off)
the variable "kenwood" is a shelly plug s with power Measurement over mqtt. unter 10 watt is off/false, over 10 watt is on/true

var play=msg.payload.play;
var kenwood=msg.payload.kenwood;

// if play and kenwood are both true, do nothing
if (play == true && kenwood == true) return null; //do nothing

// if play and kenwood are both false, do nothing
if (play == false && kenwood == false) return null; //do nothing

// if play true and kenwood false, trigger next steps
// if play false and kenwood true, trigger next steps
return msg; //send msg to next node by returning the msg.
1 Like

Or

if (play == kenwood) msg = null
return msg
1 Like

It works now!
Thank you @Steve-Mcl and @Colin for the second option :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.