Function o some node

Hi all!

I'm working with some devices obtaining one object for many of my Sonoff devices, using the api and getting the PowerStatus key. This i do for knowing which device is ON or OFF. So, if a device is ON i need to turn off that device, if OFF is ok and do not do nothing.

The main goal will be to JOIN someway the deviceName with the PowerStatus so node red can decide which device turn off.

How can i get the deviceName and the powerStatus for each device?

Thanks a lot!!

Marty,

Hi - I have ~ 15 SONOFF devices in my house and they work really well.

A question is there a reason you are the REST interface rather than using MQTT to control your devices. My experience is that The MQTT process works really well for Sonoff however just a thought.

To answer your question the way I would do it, would be to create a simple function node that refactors your input to be an array of devices with the state. Once you have done that you could split the array and switch on those that are in the "on" status

[{"id":"2c4556f.22e3caa","type":"change","z":"e550d168.41007","name":"Set up data","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"deviceName\":[\"switch_1\",\"switch_2\"],\"powerStatus\":[0,1]}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":200,"wires":[["baafdd91.341578"]]},{"id":"fcc2f4bb.256678","type":"debug","z":"e550d168.41007","name":"Do nothing","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1010,"y":280,"wires":[]},{"id":"bbcd73d1.1d2b78","type":"inject","z":"e550d168.41007","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":200,"wires":[["2c4556f.22e3caa"]]},{"id":"baafdd91.341578","type":"function","z":"e550d168.41007","name":"Process input","func":"let input = msg.payload\n\nlet devices = input.deviceName\nlet status = input.powerStatus\n\nlet states = []\n\nlet deviceStates = []\n\nfor (let i = 0; i < devices.length; i++){\n    let device = {\n        deviceName :devices[i],\n        powerStatus : status[i]\n    }\n    deviceStates.push(device)\n}\n\nmsg.payload = deviceStates;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":200,"wires":[["2e937e60.50ccb2"]]},{"id":"2e937e60.50ccb2","type":"split","z":"e550d168.41007","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":700,"y":200,"wires":[["bc89affc.fded9"]]},{"id":"bc89affc.fded9","type":"switch","z":"e550d168.41007","name":"","property":"payload.powerStatus","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":850,"y":200,"wires":[["2088c894.99ead"],["fcc2f4bb.256678"]]},{"id":"2088c894.99ead","type":"debug","z":"e550d168.41007","name":"Turn it off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1020,"y":160,"wires":[]}]

I hope this helps

Hi! Thanks for your reply. First off i use the API in order to practice some JS API data and how to control it writing some functions or so. Yes, i could also use MQTT and i use Mosquitto for another features and also Sonoff.

So, coming back to my question, i could figured it out with some nodes.

Now i have the result that i need.

But...there is always a BUT jajajaja....

How can i turn off the turned on Sonoff using the collected data?.

Thanks again!
Regards

Good, to see you have got to the next step

If you are already using the API to gt the status then to use it to turn off the sonoff will be straight forward. You use the SONOFF API For Developer API to turn it off. Have a good and come up with a flow. If you need any assistance let me know

Cool, yes im already using the Tasmota Api, not Sonoff original software because i've flashed it to do some actions and that works ok.

The thing is within my node, how can i turn off the sonnof that is turned on.

I think i need to follow these steps within a function maybe:

  1. Scan all sonoff devices - done
  2. Verify which devices are turned on - done
  3. Save the turned on devices IP within an array or so?
  4. Turn off the devices that are turned on using the previously array

What d y think?

Regards!

Great,

you are very nearly there. The flow which I originally sent does that.

The process input node , refactors the data into a different format (really your step 3) but not filtered by turned on.

It then splits that array (the split node) and the switch node then checks if it is on. You would only process those. ?

Try my flow with your Tasmota call which scans for the devices as input

Hi!, yes i think i'm almost there. So the thing is that i need to turn on the Sonoff that is turned on like the picture below.

I can filter the turned on Sonoff but how can i process the turn off flow?.

This is the complete flow.

[{"id":"eff5f66.e40db08","type":"function","z":"371b5d1c.9c46c2","name":"hosts","func":"const tasmotas = [\"192.168.1.40\",\"192.168.1.180\"]\nconst status= \"/cm?cmnd=Status\"\n\nfor(let x=0;x<tasmotas.length;x++){\n node.send({url:\"http://\"+tasmotas[x]+status})\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":360,"wires":[["ef211c5a.425a"]]},{"id":"ef211c5a.425a","type":"http request","z":"371b5d1c.9c46c2","name":"","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":390,"y":400,"wires":[["e83f5874.d8d9e8"]]},{"id":"e83f5874.d8d9e8","type":"batch","z":"371b5d1c.9c46c2","name":"","mode":"count","count":"2","overlap":0,"interval":10,"allowEmptySequence":false,"topics":[],"x":370,"y":460,"wires":[["12e13e59.da9d42"]]},{"id":"c6ead7b5.54b4e8","type":"inject","z":"371b5d1c.9c46c2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":360,"wires":[["eff5f66.e40db08"]]},{"id":"12e13e59.da9d42","type":"change","z":"371b5d1c.9c46c2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.Status{\t   \"deviceName\":**.DeviceName,\t   \"powerStatus\":**.Power \t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":500,"wires":[["179329f9.5d00a6","45bffc79.042974"]]},{"id":"45bffc79.042974","type":"debug","z":"371b5d1c.9c46c2","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":560,"wires":[]},{"id":"179329f9.5d00a6","type":"join","z":"371b5d1c.9c46c2","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"payload","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":370,"y":540,"wires":[["a6a76f4e.2a9b"]]},{"id":"a6a76f4e.2a9b","type":"switch","z":"371b5d1c.9c46c2","name":"filter turned on devices","property":"payload.powerStatus","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":420,"y":580,"wires":[["737c53f.e87afac"]]},{"id":"737c53f.e87afac","type":"debug","z":"371b5d1c.9c46c2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":600,"wires":[]}]

I think i should reuse the sonoff IPs from the Hosts node?

Thanks!

Yep, almost there. If you save the hosts data (I assume a mapping of deviceName to ipAddress) to a variable at the start of your flow you should be able to retrieve it at the end of the flow where you would call the tasmota API

Ok, i've already resolved adding a Switch node and selecting the right tasmota whether is on or off.

Regards!

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