# (De)activate Telegram-notifications with Dashboard UI-switch

**URL:** <https://discourse.nodered.org/t/de-activate-telegram-notifications-with-dashboard-ui-switch/42859>\
**Category:** General\
**Created:** [19 March 2021 09:07 UTC](https://discourse.nodered.org/t/de-activate-telegram-notifications-with-dashboard-ui-switch/42859 "2021-03-19T09:07:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![molt](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@molt](https://discourse.nodered.org/u/molt)\
**Post date:** [19 March 2021 09:07 UTC](https://discourse.nodered.org/t/de-activate-telegram-notifications-with-dashboard-ui-switch/42859/1 "2021-03-19T09:07:30Z")

</div>

Hi,

what I am trying to do:  
I want to have a UI-Switch for activating or deactivating the sending of messages via Telegram.

Scenario:  
Switch is deactivated: Someone turns the light on or off = I dont receive a message  
Switch is activated: Someone turns the light on or off = I recieve a message via Telefgram with the message "Light is on" or "Light is off"

What I have done / what I have

- TelegramBot Bothfather
- Dashboard UI switch
- Philips Hue Light node

Debug:  
Philips Hue Light  
**On**

```auto
msg.payload : Object
object
on: true
brightness: 100
brightnessLevel: 254
reachable: true
updated: "2021-03-19T08:59:04+00:00"

```

Off

```auto
msg.payload : Object
object
on: false
brightness: 100
brightnessLevel: 254
reachable: true
updated: "2021-03-19T08:59:54+00:00"

```

**Function-Node**

```auto

if (msg.payload.on == true && msg.payload == on) {
msg.payload.chatId = private
msg.payload.type = "message"
msg.payload.content = "Light is on";
}
else if (msg.payload.on == false && msg.payload == on) {
msg.payload.chatId = private
msg.payload.type = "message"
msg.payload.content = "Light is off";
}
return msg;

```

**Debug of the function-Node**  
If I set UI-Switch to "on"

```auto
function : (error)
"ReferenceError: on is not defined (line 2, col 46)"

```

and "off"

```auto
function : (error)
"ReferenceError: on is not defined (line 7, col 52)"

```

---

<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:** [19 March 2021 09:21 UTC](https://discourse.nodered.org/t/de-activate-telegram-notifications-with-dashboard-ui-switch/42859/2 "2021-03-19T09:21:06Z")

</div>

> [@molt](#):
>
> ```auto
> function : (error)
> "ReferenceError: on is not defined (line 2, col 46)"
> 
> ```

change  
`if (msg.payload.on == true && msg.payload == on) {`

to  
`if (msg.payload.on == true) {`

> [@molt](#):
>
> ```auto
> function : (error)
> "ReferenceError: on is not defined (line 7, col 52)"
> 
> ```

change  
`else if (msg.payload.on == false && msg.payload == on) {`

to  
`else if (msg.payload.on == false) {`

* * *

Reason...

1. on is not a variable you have defined. you probably meant to test msg.payload oject contains a property "on" (but the first part of your `if` takes care of that any how)

---

<div class="post-metadata">

**Author:** ![molt](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@molt](https://discourse.nodered.org/u/molt)\
**Post date:** [19 March 2021 09:34 UTC](https://discourse.nodered.org/t/de-activate-telegram-notifications-with-dashboard-ui-switch/42859/3 "2021-03-19T09:34:56Z")

</div>

Hey Steve-Mcl,  
thanks for your fast response.

Now, the UI-Switch for activatic/deactivate messages in Telegram has no function.  
If the switch for sending messages is off I still receive the message whether the light is turned on or off.

---

<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:** [19 March 2021 09:41 UTC](https://discourse.nodered.org/t/de-activate-telegram-notifications-with-dashboard-ui-switch/42859/4 "2021-03-19T09:41:58Z")

</div>

I only addressed your errors.

The code and info you posted showed nothing "obvious" related to UI elements. You didnt even post a screenshot of your flow.

I do now suspect now the "on" part of your original `if` statements was meant to be along the lines of "if the UI switch is on" send a message? Unfortunately, due to lack of flow there is not really enough info to help.

As a suggestion - store the value of the UI switch in `flow` or `global` context, then in your function, grab that status from `flow` or `global` context

e.g...

```auto
var allowTelegram = flow.get("allowTelegram") || false;
if (allowTelegram == false) {
  return null; //dont send message to next node
}
  
if (msg.payload.on == true) {
  msg.payload.chatId = private
  msg.payload.type = "message"
  msg.payload.content = "Light is on";
} else {
  msg.payload.chatId = private
  msg.payload.type = "message"
  msg.payload.content = "Light is off";
}
return msg; //return msg to next node (telegram)

```

---

<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:** [19 March 2021 10:00 UTC](https://discourse.nodered.org/t/de-activate-telegram-notifications-with-dashboard-ui-switch/42859/5 "2021-03-19T10:00:34Z")

</div>

If you are trying to block the flow of a message under the control of a ui switch then an alternative is to use node-red-contrib-simple-gate. You can easily use the switch position to open and close the gate to allow the telegram messages through or to block them.

---

<div class="post-metadata">

**Author:** ![molt](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@molt](https://discourse.nodered.org/u/molt)\
**Post date:** [19 March 2021 10:18 UTC](https://discourse.nodered.org/t/de-activate-telegram-notifications-with-dashboard-ui-switch/42859/6 "2021-03-19T10:18:15Z")

</div>

Sorry my fault. I should have taken screenshots and described my flow to you in more detail.  
Anyway, thanks for your help!

---

<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:** [19 March 2021 11:11 UTC](https://discourse.nodered.org/t/de-activate-telegram-notifications-with-dashboard-ui-switch/42859/7 "2021-03-19T11:11:17Z")

</div>

No bother. Did you get it working? Perhaps for future readers with the same request, you could share your solution?

---

<div class="post-metadata">

**Author:** ![molt](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@molt](https://discourse.nodered.org/u/molt)\
**Post date:** [19 March 2021 11:47 UTC](https://discourse.nodered.org/t/de-activate-telegram-notifications-with-dashboard-ui-switch/42859/8 "2021-03-19T11:47:34Z")

</div>

Yep, its working.  
I have implemented it with the solution of @Colin  
Very simple, perfect for dummies like me.

```auto
[{"id":"89f2699d.8fe458","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"40d536c3.eee938","type":"function","z":"89f2699d.8fe458","name":"","func":"\nif (msg.payload == true) {\nmsg.payload.chatId = EnterYourChatIDhere\nmsg.payload.type = \"message\"\nmsg.payload.content = \"Light is on\";\n}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":300,"wires":[["b434154f.8e4088"]]},{"id":"cb8f7048.09b52","type":"telegram sender","z":"89f2699d.8fe458","name":"Jarvis_sender","bot":"","haserroroutput":false,"outputs":1,"x":926,"y":305,"wires":[[]]},{"id":"d81a56a6.a89118","type":"ui_switch","z":"89f2699d.8fe458","name":"open/close","label":"Light-notification","tooltip":"","group":"3a6fe673.03ea8a","order":1,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"control","style":"","onvalue":"open","onvalueType":"str","onicon":"","oncolor":"","offvalue":"close","offvalueType":"str","officon":"","offcolor":"","x":710,"y":420,"wires":[["b434154f.8e4088"]]},{"id":"b434154f.8e4088","type":"gate","z":"89f2699d.8fe458","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","persist":false,"x":730,"y":320,"wires":[["cb8f7048.09b52"]]},{"id":"2942131c.8f016c","type":"inject","z":"89f2699d.8fe458","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"str","x":310,"y":300,"wires":[["40d536c3.eee938"]]},{"id":"3a6fe673.03ea8a","type":"ui_group","name":"Telegram","tab":"83dbb273.054c7","order":1,"disp":true,"width":"6","collapse":false},{"id":"83dbb273.054c7","type":"ui_tab","name":"Notifications","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<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:** [2 April 2021 11:48 UTC](https://discourse.nodered.org/t/de-activate-telegram-notifications-with-dashboard-ui-switch/42859/9 "2021-04-02T11:48:21Z")

</div>

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