Telegram notifications with conditions based on arguments

I’m playing a bit with telegram notifications, and my aim is to prepare some automations that will trigger some scripts, based on the agument given. all looks kinda easy, but I’m hitting a wall now, probably just because additional ‘’ which I can’t remove…

this is my automation:

alias: test_automation
trigger:
  platform: event
  event_type: telegram_command
  event_data:
    command: '/hello'
action:
  - service: script.test_notify
    data_template:
      var_text: >
        {% if is_state('{{ trigger.event.data.args }}', "keyword") %}
          you've entered the keyword!
        {% else %}
          {{ trigger.event.data.args }}
        {% endif %}

my test_notify script is as follows:

sequence:
- service: notify.my_telegram_bot
  data_template:
    message: "{{ var_text }}"

now, whatever I enter after /hello my bot send a message like this:

'<text string>'

so even if I enter /hello keyword [while the “keyword” should match the if condition] it still runs the “else” condition and outputs

'keyword'

instead of you’ve entered the keyword!

what I did wrong here?

Try

{% if is_state(trigger.event.data.args, “keyword”) %}

instead of

{% if is_state(‘{{ trigger.event.data.args }}’, “keyword”) %}

didn’t work at all :frowning:

[I mean: automation isn’t even started]

Sorry my mistake. Should be

{% if is_state(‘trigger.event.data.args’, “keyword”) %}

the same result like with my version - outputting ‘keyword’ [with ‘’] and not respecting the condition :frowning:
it is impossible that it is impossible… I;m out of ideas now…

Will try and test later as I use telegram as well although in node-red mostly.

I wonder if it’s the is_state that is bugging it out.

Can you try

{% if trigger.event.data.args == ‘keyword’ %}

More reading. If that doesn’t work I think args is returned as a list so maybe

{% if trigger.event.data.args[0] == ‘keyword’ %}

that does the trick!

oh boy, I’ve tried the [0] parameter, but just with earlier iterations - so THANK YOU VERY MUCH! that was really helpful!

is this config still valid? I can’t get var_text to work on

yeah, more-or-less the same config is still in use - below are my actual copy-n-pasted fragments that work for sure:

action part of one of the automations:

action:
  - service: script.notify_via_telegram
    data:
      var_text: "this is a message"

and the script itself:

notify_via_telegram:
  sequence:
  - service: notify.telegrambot_to_me
    data_template:
      message: "{{ var_text }}"

configuration.yaml part:

notify:
  - platform: telegram
    name: telegrambot_to_me
    chat_id: !secret my_tg_id

check if you have “data” or “data_template” when setting the var_text variable. it depends on if you use templates or not, but maybe somehow you did forget to enter this correctly?

I got it working follow your latest config, maybe its something wrong in the config here

trigger:
  platform: event
  event_type: telegram_command
  event_data:
    command: '/hello'
action:
  - service: script.test_notify
    data_template:
      var_text: >
        {% if trigger.event.data.args[0] == ‘keyword’ %}
          you've entered the keyword!
        {% else %}
          {{ trigger.event.data.args }}
        {% endif %}

I still get this error

Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected char ‘‘’ at 36) for dictionary value @ data[‘action’][0][‘data_template’][‘var_text’]. Got None. (See /config/configuration.yaml, line 153). Please check the docs at Automation - Home Assistant

I think this is the cause. change the quotes to " " or ’ ’ [you can see the difference in the code view in your post - you used the ones that has different opening and closing characters, just use the simple ones]

post scriptum: there’s also a possibility, that “you’ve” creates errors - if my previous advice didn’t helped, try to enclose whole template in " "

thanks, I’ve done with “keyword”. appreciate
what have you accomplished with this idea? mind to share abit of your work?

I’m planning to make some cleanings so my config could see the public light, but TBH I’m not sure if it will ever be ready… that’s why easier is for me to just cut out a bit… must try to make something with the pieces tho.

anyway.

I’ve got some automatic notifications and some /keyword triggered responses on Telegram.
most important [for me] automatic ones:

  1. first of all - it notifies me when RPi/Hassio is up after a reboot [so I know if reboot went ok or if there’s something wrong]; it notifies me when my second RPi [with MySQL server & MQTT broker] is up after a reboot or it’s gone for longer than 5 minutes [so I know if my system is functioning correctly].
  2. it also notifies me if I’ll keep my terrace doors open for too long in wintertime - system shuts down the heater [to preserve energy and not to run constant fight with cold] and screams via Google Home Minis + on Telegram, that I need to close the door so Skynet could bring back the heat :wink:
  3. when the home is empty, my Xiaomi MiJia Robot starts cleaning at 1p.m., Telegram notifies me on every problem that occurs and also after successfull cleaning I got the stats & report [time, battery, total area, brush/sensor status - do I need to replace/clean them etc.]
  4. reports me when my wife, and my wife when I - go out of work and gives ETA to home considering current traffic.

I have set the “importance” level for every notification, and then via dashboard me & my wife can set what importance levels do we need to be notified when away/at home - so for example I can mute the less important ones when I’m at home, or mute everything etc.

the triggered ones are mostly the report ones - so:
image

  1. I have the “main” keyword /report - when given alone it shows me a Telegram-message-buttons-menu with options, I can tap/click them or I can give commands like /report lights etc. the screenshot is in my native language - but it’s just to get you know what I’m talking about :slight_smile: these reports give me status about which lamp is turned on, what scene is on, gives me info about vacuum [similar like one after the cleaning], it can show me current snapshot from one of my 3 cameras [living room, door, garage], and my last project - it shows me current watermeter readouts.
  2. map locations - /where name shows map & location of user name
  3. /weather [tomorrow|week] gives current or when based weather forecast
  4. /script name gives me possibility to test scripts that I’m working on
  5. /vacuum [clean|usage|start|stop|dock] are for vacuum control

aaand few more, but you should get the point :slight_smile: [the commands above are translated to english for the sake of forum, but I have it in my native language of course].

most of them are well… just for fun, so I can show my friends that “I’m talking with my home via telegram” :wink: and they’re not used so often, but it is a cool space to excercise yaml scripting :slight_smile:

@pejotigrek i’m interested on your telegram menü :slight_smile:

@stephanschleichstr13 tell me what do you want to know :slight_smile: I build it with inline_keyboard argument - this is the part of yaml which does it:

  - service: telegram_bot.send_message
    data:
      target: "{{ target }}"
      message: "\U0001F4C3 Tematy raportów *do wyboru*:"
      inline_keyboard:
        - "\U0001f6cb  Salon:/raport_salon,\U0001f4a1  Oświetlenie:/raport_lampy,\U0001f52a  Kuchnia:/raport_kuchnia,\U0001f333  Ogród:/raport_ogrod"
        - "\U0001f6aa  Kamera wejście:/kamera_wejscie,\U0001f698  Kamera garaż:/kamera_garaz"
        - "\U0001f46e  Kamera salon:/kamera_salon,\U0001f333  Kamera ogród:/kamera_ogród"
        - "\U0001f4c8  Sprzątanie:/rosita_sprzatanie,\U0001f4c9  Zużycie Rosity:/rosita_zuzycie"
        - "\U0001F6B0  Stan liczników i bieżące zużycie wody:/raport_woda"

target is just a telegram id of user that was asking bot for report. the rest is using this schema: “EMOTICON LABEL:/command”. each line in yaml is a new button line in telegram chat, and if I want a couple of buttons in line I just put them after a comma.

ah okay, in your picture it looks like keyboard and not inline_keyboard :slight_smile: