[SOLVED] Switch for turning on/off Telegram notifications

Hi,
This is my first message but I’ve been reading the community for a long time ago and I would like to say that I’m learning a lot and I appreciate all the help that the members of the community provide:)
If I’m writing now is because I created a rules file with notifications of Telegram (all are running perfect) but I would like to know if there is a possibility to deactivate or activate them when I want. I await your response and thank you very much for everything. By the way, as I said before, it’s the first time I write and if I haven’t done it well, please say it to get it right next time.

  • Platform information:

    • Hardware: Raspberry PI 3B
    • OS: openHABian v1.4.1
    • Java Runtime Environment: openjdk version “1.8.0_152”
      OpenJDK Runtime Environment (Zulu Embedded 8.25.0.76-linux-aarch32hf) (build 1.8 .0_152-b76)
      OpenJDK Client VM (Zulu Embedded 8.25.0.76-linux-aarch32hf) (build 25.152-b76, m ixed mode, Evaluation)
      openHAB version: openHAB 2
  • Issue of the topic: Switch for turning on/off Telegram notifications

  • Please post configurations (if applicable):

    • Items configuration related to the issue
      // Activación-desactivación avisos apertura móvil
      Switch Notificaciones_telegram “Estado[%s]” (Home, Escenas)
    • Sitemap configuration related to the issue
      Group item=Escenas {
      Frame label=“Notificaciones Telegram”
      Switch item=Notificaciones_telegram
      }
    • Rules code related to the issue
      /* Mensaje de texto */
      rule “Puerta almacén - Apertura”
      when
      Item PuertaAlmacN_isOpen changed
      then
      if(PuertaAlmacN_isOpen.state == OPEN)
      {
      logInfo(“notifications”, “Sending notification via Telegram.”)
      sendTelegram(“Juanma”,
      “Juanma, ¡la puerta del almacén se ha ABIERTO!”)
      }
      if(PuertaAlmacN_isOpen.state == CLOSED)
      {
      logInfo(“notifications”, “Sending notification via Telegram.”)
      sendTelegram(“Juanma”,
      “Juanma, ¡la puerta del almacén se ha CERRADO!”)
      }
      end
    • Services configuration related to the issue
  • If logs where generated please post these here using code fences:

Simply use another Item (Switch, to enable or disable notifications):

/* Mensaje de texto */
rule "Puerta almacén - Apertura"
when
    Item PuertaAlmacN_isOpen changed
then
    if(Notification.state == ON) {
        logInfo("notifications", "Sending notification via Telegram.")
        var String myNote
        if(PuertaAlmacN_isOpen.state == OPEN)
            myNote = "ABIERTO!"
        if(PuertaAlmacN_isOpen.state == CLOSED) 
            myNote = "CERRADO!"
        sendTelegram("Juanma","Juanma, ¡la puerta del almacén se ha " + myNote)
    }
end
3 Likes

Thank you very much! I will try tomorrow at first hour and I will write the result. As I write first, really, thank you very much because I was totally lost!:slight_smile

Hi,

It’s working like a charm. Thank you very much! I have another doubt. I have a Xiaomi Aqara Wall Switch with two channels for opening and closing a skylight window. The problem is that it doesn’t turn off by itself and I would like to know if it’s possible to setup a timer in a rule for each time that it’s on. Something like turn on and after 7 seconds turn off. Thank you very much!:slight_smile:

Sure.

var Timer tAutoOff = null

rule "auto off"
when
    Item SkyLightOpen received command or
    Item SkyLightClose received command
then
    tAutoOff?.cancel
    if(receivedCommand == ON)
        tAutoOff = createTimer(now.plusSeconds(10), [|
            if(SkyLightOpen.state == ON) SkyLightOpen.sendCommand(OFF)
            if(SkyLightClose.state == ON) SkyLightClose.sendCommand(OFF)
            tAutoOff = null
        ])
end

As an alternative, use the expire binding.
You will have to bind an expire config to the two switches in question. after that, expire will send a OFF command after the last ON command:

Switch SkyLightOpen  "open Skylight Window"  {channel="mihome:gateway...", expire="10s,command=OFF"}
Switch SkyLightClose "close Skylight Window" {channel="mihome:gateway...", expire="10s,command=OFF"}

You have to install the expire binding first :slight_smile: but then you don’t need a rule for that.