# Execute script when changing topic"s value

**URL:** <https://discourse.nodered.org/t/execute-script-when-changing-topic-s-value/10798>\
**Category:** General\
**Created:** [3 May 2019 21:17 UTC](https://discourse.nodered.org/t/execute-script-when-changing-topic-s-value/10798 "2019-05-03T21:17:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![atux\_null](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@atux\_null](https://discourse.nodered.org/u/atux_null)\
**Post date:** [3 May 2019 21:17 UTC](https://discourse.nodered.org/t/execute-script-when-changing-topic-s-value/10798/1 "2019-05-03T21:17:52Z")

</div>

Hi. Start doing my baby steps with node red and i am looking on how to achieve a task. My setup has a raspberry that runs mqtt broker and node-red. On mqtt there are some topics and each topic has values either 0 or 1 and some other topics have open or closed values. I would like topic home/alarm/door when value is 0 to execute the script /home/pi/scripts/doors\_zero.sh and when 1 the doots\_one.sh on the same folder

While on topic office/door when value is closed the execute /home/pi/scripts/office\_closed.sh and when value is open execute the office\_open.sh

I would any help available since i am newbie with node -red , please

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [3 May 2019 21:32 UTC](https://discourse.nodered.org/t/execute-script-when-changing-topic-s-value/10798/2 "2019-05-03T21:32:29Z")

</div>

so look at the MQTT input node to subscribe to your topic. You can then use a switch node to direct a flow based on the value you get from your MQTT topic.

Take a look at the exec node to be able to execute script.

Add debug nodes along the flow so you can see and hopefully understand what is happening

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 May 2019 09:27 UTC](https://discourse.nodered.org/t/execute-script-when-changing-topic-s-value/10798/3 "2019-05-04T09:27:33Z")

</div>

The more node-red way would be to replace your scripts with node-red replacements of course. Get it working with the scripts first though since you (presumably) know that they work.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 May 2019 23:23 UTC](https://discourse.nodered.org/t/execute-script-when-changing-topic-s-value/10798/4 "2019-05-04T23:23:46Z")

</div>

I know people dont want to hear this (re-do your scripts in node-red) however @Colin is on the money here. You will not only learn (node-red) but will get a more stable solution launching scripts is unnecessary overhead and flakey at best IMO)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 May 2019 07:44 UTC](https://discourse.nodered.org/t/execute-script-when-changing-topic-s-value/10798/5 "2019-05-05T07:44:49Z")

</div>

I am not sure I would go that far. There is no reason why a script should be flaky any more than a node-red implementation.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [5 May 2019 16:13 UTC](https://discourse.nodered.org/t/execute-script-when-changing-topic-s-value/10798/6 "2019-05-05T16:13:06Z")

</div>

I'm referring more to the reliability of getting responses back. The problems people have reading stdout etc.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 May 2019 16:30 UTC](https://discourse.nodered.org/t/execute-script-when-changing-topic-s-value/10798/7 "2019-05-05T16:30:07Z")

</div>

It can be fiddly getting it to work I agree, but once going it should be fine. I assumed by flaky that you meant unreliable.
