Trigger TelegramBot with args

Hello,
I have recently started with HA and I am configuring a telegram bot, to facilitate the management of the system from outside the home.

The first thing I did was to set up some sonoffs for the lights that already work for me, but when I try to integrate them with the bot they do not work for me.

I’m trying to make the command by step parameters allow me to turn the lights on or off independently.

This is the code that I created, when I check it from HA it tells me that it is correct in structure, but I can not get it to work.

He sends me the light message on but does not turn on the corresponding light.

alias: 'telegram bot'
#hide_entity: true
trigger:
  - platform: event
    event_type: telegram_command
    event_data:
      command: '/lamp_on'
  - platform: event
    event_type: motion_detected
action:
  - service: switch.turn_on
    data_template:
      entity_id: >
           {% if trigger.event.data["args"] == 1 %}
             switch.luz_entrada
           {% endif %} 
  - service: telegram_bot.send_message
    data_template:
      message: "Luz encendida {{ trigger.event.data['args']  }}"

Thank you very much for the help.

1 Like

can you use </> around your code please. It’s current state is unreadable.

1 Like
data_template:
  entity_id: >
       {% if trigger.event.data["args"] == 1 %}
         switch.luz_entrada
       {% endif %} 

This is most likely your culprit. What happens if you don’t have an entity_id? Hass is going to error. You should check the logs.

Other than that, you may want to change:

  - service: telegram_bot.send_message
    data_template:
      message: "Luz encendida {{ trigger.event.data['args']  }}"

to

  - service: telegram_bot.send_message
    data_template:
      message: Luz encendida {{ trigger.event.data['args']  }}

You could also just change action to handle this without templates:

 action:
   - condition: template
     value_template: "{{ trigger.event.data['args'] == 1 }}"
   - service: switch.turn_on
     entity_id: switch.luz_entrada
   - service: telegram_bot.send_message
     data:
       message: Luz encendida 1

Everything after the condition will not execute if the value template is not met. That way you wont run into unforeseen issues when you don’t provide an entity_id.

Hello,
First, thank you for your help.

As you say you had the following error in the log:

Log Details (ERROR)
Mon Sep 18 2017 12:14:54 GMT + 0200 (CEST)

Invalid service data for switch.turn_on: Entity ID is an invalid entity id for dictionary value @ data ['entity_id']. Got ''

I have tried the code that you have modified me and the condition never enters.

If I remove the condition and the rest works correctly and I see that the testo that gives me the telegram bot is the following

Luz encendida '1'

With the quotes

I tried to escape but I get an error in script

where did you get this?

trigger.event.data["args"]

I just copied you. I have no idea if that is correct. What event data are you trying to get? Do you have an example event data that you could share?

Hello,
I saw it in the HA telegram bot documentation.

This in the example of

An example to show the use of event_data in action:

- alias: 'Kitchen Telegram Speak'
  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: '/speak'
  action:
    - service: notify.kitchen_echo
      data_template:
        message: >
          Message from {{ trigger.event.data["from_first"] }}. {% for state in trigger.event.data["args"] %} {{ state }} {% endfor %}

You need to find out what you get when you call args. In the python world, that will return a list of values without keywords. So if the data passed back inside trigger.event.data is {“x”:“a”, “y”,“b”}, then when you request args you should get [“a”,“b”]. You’ll never get 1. The condition you have is saying trigger.event.data[“args”] == 1, based on that information the condition will never be true.

So your first order of business is to find out what you actually get from ‘args’. Once you figure that out, you need to figure out what attribute you want to write your condition based off of.

In almost all cases, I could see you avoiding ‘args’ all together. Instead your condition will be based on a keyword inside your data dictionary. Using the example I listed above, the condition would look like:

if trigger.event.data[‘x’] == ‘a’.

So ask yourself, what am I trying to achieve? What data do I want to use to conditionally message myself?

If I get all the data of:

trigger.event.data

I get the following:

Light on {'userid': XXXXX, 'fromfirst': 'Goldrak', 'chatid': XXXXX, 'command': '/ lampon', 'args': '1'}

Therefore, when doing equality, you should not have the ‘’

when I print only the args

Then your condition should be:

   - condition: template
     value_template: "{{ trigger.event.data['args'] == '1' }}"

Only if you are trying to condition off the fact that args is equal to 1.

Testing the line you tell me does not work for me, so I decided to try it in position 0 in case I avoided them.

being that way:

    - condition: template
      value_template: "{{trigger.event.data ['args'] [0] == '1'}}"

This way if it works for me, so try to put other conditions, as you told me, to turn on the rest of my lights.

But the second option does not work for me.

alias: 'telegram bot'
#hide_entity: true
trigger:
  - platform: event
    event_type: telegram_command
    event_data:
      command: '/lamp_on'
  - platform: event
    event_type: motion_detected
action:
 - condition: template
   value_template: "{{ trigger.event.data['args'][0] == '1' }}"
 - service: switch.turn_on
   entity_id: switch.luz_entrada
 - service: telegram_bot.send_message
   data_template:
     message: "Luz encendida {{ trigger.event.data['args'][0]  }}"
 - condition: template
   value_template: "{{ trigger.event.data['args'][0] == '2' }}"
 - service: telegram_bot.send_message
   data_template:
     message: "Luz encendida {{ trigger.event.data['args'][0]  }}"

Thanks for all.

I can’t follow what you are trying to do. All the code you are posting shows me that you are over complicating this. To me based on your if statements, you don’t need a condition at all because nothing is changing between each condition. If that truly is the case, this should work:

action:
 - service: switch.turn_on
   entity_id: switch.luz_entrada
 - service: telegram_bot.send_message
   data_template:
     message: "Luz encendida {{ trigger.event.data['args'] }}"

In regards to using [0], that shouldn’t make a difference because both return the same value.

Calling a postion a larger string makes sense, but in your case it doesn’t. Example my string is “MYSTRING”. I want the first postion, “MYSTRING”[0] would return “M”. You are calling trigger.event.data[‘args’], which will return a string according to you:

meaning that the respose for calling [‘args’] would be ‘1’. and ‘1’[0] will return the first position in the string ‘1’, which is ‘1’.

So My question is, Are you just trying to change the message based on the event data?

My intention is to be able to turn on the different lights of my house by passing a number to the command.

The example that I put does not have another light because I still do not have it installed and I was trying to get a text back.

If I do not put the [0] as I said before, the condition always returns false.

2018-04-02 19:13:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event telegram_command[L]: user_id=xxxxxx, from_first=Goldrak, chat_id=xxxxx, command=/lamp_on, args=['1']>
2018-04-02 19:13:15 INFO (MainThread) [homeassistant.components.automation] Executing telegram bot
2018-04-02 19:13:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: name=telegram bot, message=has been triggered, domain=automation, entity_id=automation.telegram_bot>
2018-04-02 19:13:15 INFO (MainThread) [homeassistant.helpers.script] Script telegram bot: Running script
2018-04-02 19:13:15 INFO (MainThread) [homeassistant.helpers.script] Script telegram bot: Test condition template: False
2018-04-02 19:13:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.telegram_bot, old_state=<state automation.telegram_bot=on; last_triggered=None, friendly_name=telegram bot @ 2018-04-02T19:13:05.709375+02:00>, new_state=<state automation.telegram_bot=on; last_triggered=2018-04-02T19:13:15.538562+02:00, friendly_name=telegram bot @ 2018-04-02T19:13:05.709375+02:00>>

well that makes sense because your log says:

args=['1']

you said:

[‘1’] does not equal ‘1’

brackets around an item indicates a list. Your result is a list of strings, so you would need to use:

trigger.event.data['args'][0]

Also, changing which light to turn on based on event data would look like this:

action:
  - service: switch.turn_on
    data_template:
      entity_id:>
        {% if trigger.event.data['args'][0] == '1' %}
          switch.luz_entrada
        {% elif trigger.event.data['args'][0] == '2' %}
          switch.luz_2
        {% elif trigger.event.data['args'][0] == '3' %}
          switch.luz_3
        {% else %}
          switch.luz_4
        {% endif %}
  - service: telegram_bot.send_message
    data_template:
      message: "Luz encendida {{ trigger.event.data['args'][0] }}"
1 Like

Thanks for all, now work perfect.

1 Like