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

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

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

Off

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

Function-Node


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"

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

and "off"

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

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

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

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)
1 Like

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.

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...

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)

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.

1 Like

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

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

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

[{"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}]

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