# Is there a better way to do this?

**URL:** <https://discourse.nodered.org/t/is-there-a-better-way-to-do-this/43785>\
**Category:** General\
**Created:** [7 April 2021 01:31 UTC](https://discourse.nodered.org/t/is-there-a-better-way-to-do-this/43785 "2021-04-07T01:31:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcarreira](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jcarreira/32/39830_2.png) [@jcarreira](https://discourse.nodered.org/u/jcarreira)\
**Post date:** [7 April 2021 01:31 UTC](https://discourse.nodered.org/t/is-there-a-better-way-to-do-this/43785/1 "2021-04-07T01:31:03Z")

</div>

Maybe "better" isn't the right word... More native to NR, maybe? I'm trying to avoid dropping to code for everything, but this works...

```auto
[{"id":"449c809d.fec43","type":"hubitat hsm","z":"534682e6.98503c","name":"","server":"7b8c3c84.96b724","sendEvent":true,"x":290,"y":920,"wires":[["835c2b09.b329a8","ece7858a.4a4628"]]},{"id":"c78f8c2d.abc9a","type":"hubitat device","z":"534682e6.98503c","deviceLabel":"Garage Tablet","name":"","server":"7b8c3c84.96b724","deviceId":"176","attribute":"motion","sendEvent":true,"x":320,"y":1000,"wires":[["a07fe28e.a25e","9ec24317.a7929"]]},{"id":"835c2b09.b329a8","type":"function","z":"534682e6.98503c","name":"set global.hsm","func":"let value = msg.payload.value.toLowerCase();\nglobal.set('hsm', value);\nif (value.includes('intrusion')) {\n return msg;\n} else {\n return null;\n}","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is deployed.\nif (global.get('hsm') == undefined) {\n global.set('hsm','disarmed');\n}","finalize":"","x":560,"y":920,"wires":[["f3e945fc.a19668"]]},{"id":"70a03fe5.428bb","type":"function","z":"534682e6.98503c","name":"ping on startup","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is deployed.\nnode.send({payload: \"ping\"})","finalize":"","x":110,"y":960,"wires":[["449c809d.fec43","c78f8c2d.abc9a"]]},{"id":"f3e945fc.a19668","type":"function","z":"534682e6.98503c","name":"Pass if both set","func":"if (global.get('hsm').includes('intrusion') && flow.get('motionActive')) {\n return msg;\n} else {\n return null;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":780,"y":960,"wires":[["39094ece.947d22","300f9857.58a578"]]},{"id":"a07fe28e.a25e","type":"function","z":"534682e6.98503c","name":"set flow.motionActive","func":"let active = msg.payload.value == 'active';\nflow.set('motionActive',active);\nif (active) {\n return msg;\n} else {\n return null;\n}","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is deployed.\nif (flow.get('motionActive') == undefined) {\n flow.set('motionActive',false);\n}","finalize":"","x":540,"y":1000,"wires":[["f3e945fc.a19668"]]},{"id":"7b8c3c84.96b724","type":"hubitat config","name":"Hubitat","usetls":false,"host":"192.168.1.29","port":"80","appId":"270","nodeRedServer":"http://192.168.1.23","webhookPath":"/hubitat/webhook","autoRefresh":true,"useWebsocket":false,"colorEnabled":false,"color":"#ace043"}]

```

Basically I've got my Hubitat System Monitor and a tablet running Fully Kiosk Browser for my dashboard. When the HSM is in one of its "intrusion" modes (i.e. it was armed and a door was opened) AND the tablet senses motion, I want the tablet to capture an image using its camera and send it to me. I got the image capture and sending worked out earlier so I was working on coordinating these 2 states. There's no telling which event will come first, so I'm capturing them into context vars (global for HSM and flow for the motion activity).

So, is this a good way to accomplish this? Also, is my function to send a "ping" on startup a normal way to initialize values from devices so you don't have to wait for events from them?

Thanks

---

<div class="post-metadata">

**Author:** ![chrisn-au](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisn-au/32/25504_2.png) [@chrisn-au](https://discourse.nodered.org/u/chrisn-au)\
**Post date:** [7 April 2021 04:31 UTC](https://discourse.nodered.org/t/is-there-a-better-way-to-do-this/43785/2 "2021-04-07T04:31:29Z")

</div>

> [@jcarreira](#):
>
> `[`

From my perspective it looks like a sensible and node reddish way of doing it. I have similar flows. My only style thing (and I am no one to judge style) is to remove the logic of the internals of the intrusion string from down stream processing by adding another global (or possibly flow variable)

```auto
let value = msg.payload.value.toLowerCase();
global.set('hsm', value);

let intrusion = value.includes('intrusion')
global.set('hsmIntrusion', intrusion);
if (intrusion) return msg

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [7 April 2021 06:54 UTC](https://discourse.nodered.org/t/is-there-a-better-way-to-do-this/43785/3 "2021-04-07T06:54:50Z")

</div>

Just for info  
take a look here [https://reactor.toggledbits.com/mantisbt/](https://reactor.toggledbits.com/mantisbt/)  
if you can not get account post here [Multi-System Reactor | Smart Home - Community](https://smarthome.community/category/22/multi-system-reactor)  
This is a new multi system logic engine, it is very powerful and runs on any system that runs nodejs.

It is early alpha and so far systems that are integrated are  
vera/openluup  
hubitat  
home assistant

The idea is to bring all controllers and systems under one logic engine.  
more controllers and mqtt will also be integrated. It also has a built in dashboard

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 June 2021 06:55 UTC](https://discourse.nodered.org/t/is-there-a-better-way-to-do-this/43785/4 "2021-06-06T06:55:38Z")

</div>

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