[newbie] how to program this more elegantly

Dear all,
I wrote this code for a function, but I am aware this is not elegant.
I have two questions about it:

  1. Could you show me how to do this properly using a "case"-like structure
  2. How can I expand the function such that I also can choose between: twelve, thirteen, ...

Here is my code:

 if ((msg.payload.slots.state=="on") && (msg.payload.slots.pin=="twelve"))
  {
      msg.payload=1;
  } else {
      if ((msg.payload.slots.state=="off") && (msg.payload.slots.pin=="twelve"))
      {
        msg.payload=0;
      }
  }  

return msg;

kind regards,
hugo

You don't need a function for that... A switch node is your friend:

As you don't describe what you want to do with the various values of "pin" it is difficult to describe it further...

But, you could use a switch as well for the "pin" values.
GV

Hi Hugo, @greengolfer is correct in that you can achieve this with builtin switch and change nodes.

However, if this is an exercise in improving your JS skills, here is how I would suggest...

Firstly, at the end of the function you always return msg even if one of the logic paths fail. i.e. if msg.payload.slots.state had a value of null then the return msg would still occur & you would get a null payload - this might not be what you intended.

Regarding structure...

  1. The formatting is terrible. In small cases like this, that is not an issue, but when your functions grow, it will be hard to keep track of. - always format your code to a standard (I prefer this standard)

  2. there are 2 compares of msg.payload.slots.pin=="twelve" - that is unnecessary.

if (msg.payload.slots.pin == "twelve") {
  if (msg.payload.slots.state == "on")  {
    msg.payload = 1;
    return msg; //both state and pin match my criteria - send the msg to the next node.
  } else  if (msg.payload.slots.state == "off") {
    msg.payload = 0;
    return msg; //both state and pin match my criteria - send the msg to the next node.
  }
}  
return null;// either state or pin did not match my criteria - dont allow the flow to continue.

If you want to get smaller / cleverer (but less verbose / readable)...

const stateLookup = {"on":1, "off":0};
if (msg.payload.slots.pin == "twelve") {
  msg.payload = stateLookup[msg.payload.slots.state];
}  
if(msg.payload != null) return msg; 
1 Like

Thanks, greengolfer for answering. I admit I should have given more information.
As you can see in the flow below. I currently have the possibility to switch pins twelve (twaalf in dutch) and thirteen (dertien in dutch) on (aan) or off (uit). The goal is to adapt the flow elegantly such that it can control more pins in the future (in the flow below only pin 12 is controlled).

[{"id":"a220bb19.88e508","type":"tab","label":"gpio12and13","disabled":false,"info":""},{"id":"c5b27823.03f328","type":"inject","z":"a220bb19.88e508","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":170,"y":360,"wires":[["73f8397c.fb05b8"]]},{"id":"5da1627b.b88bbc","type":"inject","z":"a220bb19.88e508","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":170,"y":320,"wires":[["73f8397c.fb05b8"]]},{"id":"9bda50c0.aca068","type":"comment","z":"a220bb19.88e508","name":"Set pin 12 or 13 low or high using node-red in a container","info":"","x":340,"y":60,"wires":[]},{"id":"73f8397c.fb05b8","type":"pi-gpiod out","z":"a220bb19.88e508","name":"GPIO13 (33)","host":"pigpiod","port":8888,"pin":"13","set":"","level":"0","out":"out","sermin":"1000","sermax":"2000","x":610,"y":340,"wires":[]},{"id":"60d59edb.db7438","type":"inject","z":"a220bb19.88e508","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":170,"y":140,"wires":[["a53ea37b.20e768"]]},{"id":"62fd3afe.77a9ac","type":"inject","z":"a220bb19.88e508","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":170,"y":100,"wires":[["a53ea37b.20e768"]]},{"id":"a53ea37b.20e768","type":"pi-gpiod out","z":"a220bb19.88e508","name":"GPIO12 (32)","host":"pigpiod","port":8888,"pin":"12","set":"","level":"0","out":"out","sermin":"1000","sermax":"2000","x":610,"y":280,"wires":[]},{"id":"9f7a3c6e.e19ea8","type":"link in","z":"a220bb19.88e508","name":"gpio12and13","links":["b3728467.3ab1"],"x":170,"y":460,"wires":[["8acdc106.48aeb"]],"l":true},{"id":"8acdc106.48aeb","type":"function","z":"a220bb19.88e508","name":"on_or_off","func":"\n if ((msg.payload.slots.state==\"aan\") && (msg.payload.slots.pin==\"twaalf\"))\n {\n msg.payload=1;\n } else {\n if ((msg.payload.slots.state==\"uit\") && (msg.payload.slots.pin==\"twaalf\"))\n {\n msg.payload=0;\n }\n } \n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":520,"wires":[["a53ea37b.20e768"]]},{"id":"f883687.f470898","type":"comment","z":"a220bb19.88e508","name":"More GPIO pins could be added in the future","info":"","x":720,"y":200,"wires":[]}]

Then perhaps...

const stateLookup = {"on":1, "off":0};
const allowedPins = ["twelve","thirteen"];
if ( allowedPins.includes(msg.payload.slots.pin) ) {
  msg.payload = stateLookup[msg.payload.slots.state];
}  
if(msg.payload != null) return msg; //only send msg to next node if payload is "something" 
2 Likes

This kind of coding is way above my pay grade :rofl:
Nice though. I have learnt something !!

1 Like

Just to prove @greengolfer point, it is doable by built in nodes (however, I find 1 function more concise than these three nodes) - the choice is yours....

Flow version...

[{"id":"de756e4a.331f7","type":"switch","z":"c3af41e4.dd1e9","name":"pin == twelve or thirteen?","property":"payload.slots.pin","propertyType":"msg","rules":[{"t":"eq","v":"twelve","vt":"str"},{"t":"eq","v":"thirteen","vt":"str"},{"t":"eq","v":"","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":430,"y":360,"wires":[["8aa8e71f.cc4b38"],["8aa8e71f.cc4b38"],[]]},{"id":"8aa8e71f.cc4b38","type":"change","z":"c3af41e4.dd1e9","name":"SET  on-->1   off-->0    other-->null","rules":[{"t":"set","p":"payload","pt":"msg","to":"(msg.payload.slots.state = \"on\" ? 1 : (msg.payload.slots.state = \"off\" ? 0 : null))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":360,"wires":[["878a9bba.20c268"]]},{"id":"878a9bba.20c268","type":"switch","z":"c3af41e4.dd1e9","name":"payload not null?","property":"payload","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":450,"y":420,"wires":[["59bfa586.cf740c"]]},{"id":"76cd6c22.199264","type":"inject","z":"c3af41e4.dd1e9","name":"twelve on","topic":"","payload":"{\"slots\":{\"pin\":\"twelve\",\"state\":\"on\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":300,"wires":[["de756e4a.331f7"]]},{"id":"59bfa586.cf740c","type":"debug","z":"c3af41e4.dd1e9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":420,"wires":[]},{"id":"a56be6ca.07e228","type":"inject","z":"c3af41e4.dd1e9","name":"twelve off","topic":"","payload":"{\"slots\":{\"pin\":\"twelve\",\"state\":\"off\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":340,"wires":[["de756e4a.331f7"]]},{"id":"a2439395.f3c12","type":"inject","z":"c3af41e4.dd1e9","name":"thirteen on","topic":"","payload":"{\"slots\":{\"pin\":\"thirteen\",\"state\":\"on\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":400,"wires":[["de756e4a.331f7"]]},{"id":"80706485.376ad8","type":"inject","z":"c3af41e4.dd1e9","name":"thirteen off","topic":"","payload":"{\"slots\":{\"pin\":\"thirteen\",\"state\":\"off\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":440,"wires":[["de756e4a.331f7"]]},{"id":"a06943bc.7c3ba","type":"inject","z":"c3af41e4.dd1e9","name":"thirteen _bad_","topic":"","payload":"{\"slots\":{\"pin\":\"thirteen\",\"state\":\"_bad_\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":540,"wires":[["de756e4a.331f7"]]},{"id":"9174e232.43738","type":"inject","z":"c3af41e4.dd1e9","name":"nineteen on","topic":"","payload":"{\"slots\":{\"pin\":\"nineteen\",\"state\":\"on\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":580,"wires":[["de756e4a.331f7"]]},{"id":"17a10d29.d8d2f3","type":"comment","z":"c3af41e4.dd1e9","name":"OK tests","info":"","x":160,"y":260,"wires":[]},{"id":"2fe60e68.7e03a2","type":"comment","z":"c3af41e4.dd1e9","name":"BAD tests","info":"","x":160,"y":500,"wires":[]}]

Pros

  • visually simple
  • no function node

Downsides

  • JSONata in change node not so easy on the eyes
  • hard coded design time solution (couldnt update allowable pins from a dashboard or databaes for example)

The function version...

[{"id":"90f20706.f28aa8","type":"inject","z":"c3af41e4.dd1e9","name":"twelve on","topic":"","payload":"{\"slots\":{\"pin\":\"twelve\",\"state\":\"on\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":720,"wires":[["b0594b91.92d008"]]},{"id":"e8498189.482ac","type":"inject","z":"c3af41e4.dd1e9","name":"twelve off","topic":"","payload":"{\"slots\":{\"pin\":\"twelve\",\"state\":\"off\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":760,"wires":[["b0594b91.92d008"]]},{"id":"fd1b0ba0.817f88","type":"inject","z":"c3af41e4.dd1e9","name":"thirteen on","topic":"","payload":"{\"slots\":{\"pin\":\"thirteen\",\"state\":\"on\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":820,"wires":[["b0594b91.92d008"]]},{"id":"ad473970.7e2c58","type":"inject","z":"c3af41e4.dd1e9","name":"thirteen off","topic":"","payload":"{\"slots\":{\"pin\":\"thirteen\",\"state\":\"off\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":860,"wires":[["b0594b91.92d008"]]},{"id":"4d30bc3b.8c1d14","type":"inject","z":"c3af41e4.dd1e9","name":"thirteen _bad_","topic":"","payload":"{\"slots\":{\"pin\":\"thirteen\",\"state\":\"_bad_\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":960,"wires":[["b0594b91.92d008"]]},{"id":"4a35a555.3a6abc","type":"inject","z":"c3af41e4.dd1e9","name":"nineteen on","topic":"","payload":"{\"slots\":{\"pin\":\"nineteen\",\"state\":\"on\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":1000,"wires":[["b0594b91.92d008"]]},{"id":"8a54e71b.19a6d8","type":"comment","z":"c3af41e4.dd1e9","name":"OK tests","info":"","x":160,"y":680,"wires":[]},{"id":"f2ac0465.a7a068","type":"comment","z":"c3af41e4.dd1e9","name":"BAD tests","info":"","x":160,"y":920,"wires":[]},{"id":"b0594b91.92d008","type":"function","z":"c3af41e4.dd1e9","name":"state==on => 1, state==off => 0  (allowed pins only)","func":"\nconst stateLookup = {\"on\":1, \"off\":0};\nconst allowedPins = flow.get(\"allowedPins\") || [];\nif ( allowedPins.includes(msg.payload.slots.pin) ) {\n    msg.payload = stateLookup[msg.payload.slots.state];\n    if(msg.payload != null) return msg; //only send msg to next node if payload is \"something\" \n}  ","outputs":1,"noerr":0,"x":530,"y":780,"wires":[["36651d47.524902"]]},{"id":"36651d47.524902","type":"debug","z":"c3af41e4.dd1e9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":840,"wires":[]},{"id":"5d67f7c7.1b5d48","type":"inject","z":"c3af41e4.dd1e9","name":"Initialise","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":400,"y":700,"wires":[["5d58dbe7.885cd4"]]},{"id":"5d58dbe7.885cd4","type":"change","z":"c3af41e4.dd1e9","name":"Setup allowed pins","rules":[{"t":"set","p":"allowedPins","pt":"flow","to":"[\"twelve\",\"thirteen\"]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":700,"wires":[[]]}]

Pros

  • easier to maintain (IMO)
  • runtime automatable i.e. from a dashboard / database (as the "allowable pins" are stored in context, you could change these at runtime)

Downsides

  • function node not to everyones liking
1 Like

Dear greengolfer,

Could you send me a flow with that node only. I can't figure out how to get "···Property" below Name.
I see "Rules" instead.

kind regards,
hugo

lol.

Actually, there was a slight bug.

this is the better version of that

const stateLookup = {"on":1, "off":0};
const allowedPins = ["twelve","thirteen"];
if ( allowedPins.includes(msg.payload.slots.pin) ) {
  msg.payload = stateLookup[msg.payload.slots.state];
  if(msg.payload != null) return msg; //only send msg to next node if payload is "something"
}  

basically, if you sent msg.payload.slots.pin "nineteen" for example, it allowed the function to output the original msg unchanged. By moving the last line into the if() it is contained.

1 Like

Dear Steve-Mcl,
Thanks a lot for all this really interesting teachings. Unfortunately I still didn't communicate as clear as I should have in my question.
What I really want is that depending on the pin number which is coming through, a 0 or a 1 should be sent to a gpiod-output. Checking whether the pin is available is only a safety measure. So I thought a switch node might be useful (or a function with more than 1 output, 2 if only pin 12 and 13 are used, but in the future there will be more) . I have added a flow which shows the idea using a switch (note however the switch is not configured correctly, I don't know how).

[{"id":"a220bb19.88e508","type":"tab","label":"gpio12and13","disabled":false,"info":""},{"id":"c5b27823.03f328","type":"inject","z":"a220bb19.88e508","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":170,"y":360,"wires":[["73f8397c.fb05b8"]]},{"id":"5da1627b.b88bbc","type":"inject","z":"a220bb19.88e508","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":170,"y":320,"wires":[["73f8397c.fb05b8"]]},{"id":"9bda50c0.aca068","type":"comment","z":"a220bb19.88e508","name":"Set pin 12 or 13 low or high using node-red in a container","info":"","x":290,"y":40,"wires":[]},{"id":"73f8397c.fb05b8","type":"pi-gpiod out","z":"a220bb19.88e508","name":"GPIO13 (33)","host":"pigpiod","port":8888,"pin":"13","set":"","level":"0","out":"out","sermin":"1000","sermax":"2000","x":1010,"y":340,"wires":[]},{"id":"60d59edb.db7438","type":"inject","z":"a220bb19.88e508","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":170,"y":200,"wires":[["a53ea37b.20e768"]]},{"id":"62fd3afe.77a9ac","type":"inject","z":"a220bb19.88e508","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":170,"y":160,"wires":[["a53ea37b.20e768"]]},{"id":"a53ea37b.20e768","type":"pi-gpiod out","z":"a220bb19.88e508","name":"GPIO12 (32)","host":"pigpiod","port":8888,"pin":"12","set":"","level":"0","out":"out","sermin":"1000","sermax":"2000","x":1010,"y":260,"wires":[]},{"id":"9f7a3c6e.e19ea8","type":"link in","z":"a220bb19.88e508","name":"gpio12and13","links":["b3728467.3ab1"],"x":170,"y":460,"wires":[["1574e835.ba1d28"]],"l":true},{"id":"f883687.f470898","type":"comment","z":"a220bb19.88e508","name":"More GPIO pins could be added in the future","info":"","x":890,"y":160,"wires":[]},{"id":"1574e835.ba1d28","type":"switch","z":"a220bb19.88e508","name":"ChoosePin","property":"payload.slots.pin","propertyType":"msg","rules":[{"t":"eq","v":"12","vt":"str"},{"t":"eq","v":"13","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":650,"y":560,"wires":[["87bb608e.e73638"],["91a7e273.2f6968"]]},{"id":"87bb608e.e73638","type":"change","z":"a220bb19.88e508","name":"send 0 or 1 to pin 12","rules":[],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":520,"wires":[["a53ea37b.20e768"]],"info":"property"},{"id":"91a7e273.2f6968","type":"change","z":"a220bb19.88e508","name":"send 0 or 1 to pin 13","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":580,"wires":[["73f8397c.fb05b8"]]},{"id":"37eea9d4.d7dd6e","type":"comment","z":"a220bb19.88e508","name":"The switches below are for testing","info":"","x":250,"y":100,"wires":[]}]

Here is an example of the json-formatted code which enters the flow:

{
    "entities": [
        {
            "end": 11,
            "entity": "pin",
            "raw_end": 11,
            "raw_start": 4,
            "raw_value": "dertien",
            "start": 4,
            "value": "dertien",
            "value_details": {
                "kind": "Unknown",
                "value": "dertien"
            }
        },
        {
            "end": 15,
            "entity": "state",
            "raw_end": 15,
            "raw_start": 12,
            "raw_value": "uit",
            "start": 12,
            "value": "uit",
            "value_details": {
                "kind": "Unknown",
                "value": "uit"
            }
        }
    ],
    "intent": {
        "confidence": 0.6666666666666667,
        "name": "gpio12and13"
    },
    "raw_text": "zet pin dertien uit",
    "raw_tokens": [
        "zet",
        "pin",
        "dertien",
        "uit"
    ],
    "recognize_seconds": 0.16175900300004287,
    "slots": {
        "pin": "dertien",
        "state": "uit"
    },
    "speech_confidence": 1,
    "text": "zet dertien uit",
    "tokens": [
        "zet",
        "dertien",
        "uit"
    ],
    "wakeword_id": null
}

Try this...

[{"id":"918eee3e.d731c","type":"comment","z":"eec6199a.e09fb8","name":"Set pin 12 or 13 low or high using node-red in a container","info":"","x":290,"y":100,"wires":[]},{"id":"5a0b30ab.cb57c","type":"link in","z":"eec6199a.e09fb8","name":"gpio12and13","links":["b3728467.3ab1","7a401449.e0613c","1d070139.b9491f"],"x":150,"y":140,"wires":[["e2a0d79.2366f28"]],"l":true},{"id":"e839ac74.072b8","type":"comment","z":"eec6199a.e09fb8","name":"More GPIO pins could be added in the future","info":"","x":710,"y":100,"wires":[]},{"id":"e2a0d79.2366f28","type":"switch","z":"eec6199a.e09fb8","name":"ChoosePin","property":"payload.slots.pin","propertyType":"msg","rules":[{"t":"eq","v":"dertien","vt":"str"},{"t":"eq","v":"twaalf","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":140,"wires":[["f6b8e34b.a1b02"],["6c4d2f6c.a711d"]]},{"id":"bad57987.18bb68","type":"comment","z":"eec6199a.e09fb8","name":"The switches below are for testing","info":"","x":220,"y":220,"wires":[]},{"id":"9c1a5e3b.c9103","type":"inject","z":"eec6199a.e09fb8","name":"twaalf uit","topic":"","payload":"{\"entities\":[{\"end\":11,\"entity\":\"pin\",\"raw_end\":11,\"raw_start\":4,\"raw_value\":\"dertien\",\"start\":4,\"value\":\"dertien\",\"value_details\":{\"kind\":\"Unknown\",\"value\":\"dertien\"}},{\"end\":15,\"entity\":\"state\",\"raw_end\":15,\"raw_start\":12,\"raw_value\":\"uit\",\"start\":12,\"value\":\"uit\",\"value_details\":{\"kind\":\"Unknown\",\"value\":\"uit\"}}],\"intent\":{\"confidence\":0.6666666666666667,\"name\":\"gpio12and13\"},\"raw_text\":\"zet pin dertien uit\",\"raw_tokens\":[\"zet\",\"pin\",\"dertien\",\"uit\"],\"recognize_seconds\":0.16175900300004287,\"slots\":{\"pin\":\"twaalf\",\"state\":\"uit\"},\"speech_confidence\":1,\"text\":\"zet dertien uit\",\"tokens\":[\"zet\",\"dertien\",\"uit\"],\"wakeword_id\":null}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":260,"wires":[["7a401449.e0613c"]]},{"id":"7a401449.e0613c","type":"link out","z":"eec6199a.e09fb8","name":"","links":["5a0b30ab.cb57c"],"x":315,"y":260,"wires":[]},{"id":"88807acb.d52bd8","type":"debug","z":"eec6199a.e09fb8","name":"to gpio12","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":820,"y":140,"wires":[]},{"id":"ef3e044a.a3b698","type":"debug","z":"eec6199a.e09fb8","name":"to gpio13","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":820,"y":180,"wires":[]},{"id":"3c70687d.e647d8","type":"inject","z":"eec6199a.e09fb8","name":"twaalf aan","topic":"","payload":"{\"entities\":[{\"end\":11,\"entity\":\"pin\",\"raw_end\":11,\"raw_start\":4,\"raw_value\":\"dertien\",\"start\":4,\"value\":\"dertien\",\"value_details\":{\"kind\":\"Unknown\",\"value\":\"dertien\"}},{\"end\":15,\"entity\":\"state\",\"raw_end\":15,\"raw_start\":12,\"raw_value\":\"uit\",\"start\":12,\"value\":\"uit\",\"value_details\":{\"kind\":\"Unknown\",\"value\":\"uit\"}}],\"intent\":{\"confidence\":0.6666666666666667,\"name\":\"gpio12and13\"},\"raw_text\":\"zet pin dertien uit\",\"raw_tokens\":[\"zet\",\"pin\",\"dertien\",\"uit\"],\"recognize_seconds\":0.16175900300004287,\"slots\":{\"pin\":\"twaalf\",\"state\":\"aan\"},\"speech_confidence\":1,\"text\":\"zet dertien uit\",\"tokens\":[\"zet\",\"dertien\",\"uit\"],\"wakeword_id\":null}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":300,"wires":[["7a401449.e0613c"]]},{"id":"3b5eb17.7da884e","type":"inject","z":"eec6199a.e09fb8","name":"dertien uit","topic":"","payload":"{\"entities\":[{\"end\":11,\"entity\":\"pin\",\"raw_end\":11,\"raw_start\":4,\"raw_value\":\"dertien\",\"start\":4,\"value\":\"dertien\",\"value_details\":{\"kind\":\"Unknown\",\"value\":\"dertien\"}},{\"end\":15,\"entity\":\"state\",\"raw_end\":15,\"raw_start\":12,\"raw_value\":\"uit\",\"start\":12,\"value\":\"uit\",\"value_details\":{\"kind\":\"Unknown\",\"value\":\"uit\"}}],\"intent\":{\"confidence\":0.6666666666666667,\"name\":\"gpio12and13\"},\"raw_text\":\"zet pin dertien uit\",\"raw_tokens\":[\"zet\",\"pin\",\"dertien\",\"uit\"],\"recognize_seconds\":0.16175900300004287,\"slots\":{\"pin\":\"dertien\",\"state\":\"uit\"},\"speech_confidence\":1,\"text\":\"zet dertien uit\",\"tokens\":[\"zet\",\"dertien\",\"uit\"],\"wakeword_id\":null}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":360,"wires":[["7a401449.e0613c"]]},{"id":"af4d581b.bb0588","type":"inject","z":"eec6199a.e09fb8","name":"dertien aan","topic":"","payload":"{\"entities\":[{\"end\":11,\"entity\":\"pin\",\"raw_end\":11,\"raw_start\":4,\"raw_value\":\"dertien\",\"start\":4,\"value\":\"dertien\",\"value_details\":{\"kind\":\"Unknown\",\"value\":\"dertien\"}},{\"end\":15,\"entity\":\"state\",\"raw_end\":15,\"raw_start\":12,\"raw_value\":\"uit\",\"start\":12,\"value\":\"uit\",\"value_details\":{\"kind\":\"Unknown\",\"value\":\"uit\"}}],\"intent\":{\"confidence\":0.6666666666666667,\"name\":\"gpio12and13\"},\"raw_text\":\"zet pin dertien uit\",\"raw_tokens\":[\"zet\",\"pin\",\"dertien\",\"uit\"],\"recognize_seconds\":0.16175900300004287,\"slots\":{\"pin\":\"dertien\",\"state\":\"aan\"},\"speech_confidence\":1,\"text\":\"zet dertien uit\",\"tokens\":[\"zet\",\"dertien\",\"uit\"],\"wakeword_id\":null}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":400,"wires":[["7a401449.e0613c"]]},{"id":"f6b8e34b.a1b02","type":"function","z":"eec6199a.e09fb8","name":"convert uit/aan to 0/1","func":"const stateLookup = {\"aan\":1, \"uit\":0};\nmsg.payload = stateLookup[msg.payload.slots.state];\nreturn msg;","outputs":1,"noerr":0,"x":640,"y":140,"wires":[["88807acb.d52bd8"]]},{"id":"6c4d2f6c.a711d","type":"function","z":"eec6199a.e09fb8","name":"convert uit/aan to 0/1","func":"const stateLookup = {\"aan\":1, \"uit\":0};\nmsg.payload = stateLookup[msg.payload.slots.state];\nreturn msg;","outputs":1,"noerr":0,"x":640,"y":180,"wires":[["ef3e044a.a3b698"]]}]
1 Like

Thanks, works nicely and I learned a lot.

kind regards,
Hugo

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