Conversation with Telegram Bot: "turn [Friendly Name] [on/off]"

Here is how I text my Telegram Bot using the built in conversation component

First, create a group and put all devices that you want Telegram to control in it. Example…

controllable_devices:
  name: Controllable Devices
  entities:
    - fan.bedroom_fan
    - fan.ceiling_fan
    - fan.fan
    - fan.play_room_fan
    - switch.dining_fan
    - switch.dining_light
    - switch.down_lights
    - switch.entrance_light

then, add this to your automation…

- alias: 'Conversation'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: telegram_text
  condition:
    condition: template
    value_template: >-
      {% set device = trigger.event.data.text|lower|replace('turn on ','')|replace('turn off ','') %}
      {% for entity in states.group.controllable_devices.attributes.entity_id if states[entity.split('.')[0]][entity.split('.')[1]].name|lower == device -%}
      true
      {%- else -%}
      false
      {%- endfor -%}
  action:
    - service: conversation.process
      data_template:
        text: '{{ trigger.event.data.text|lower }}'
    - service: telegram_bot.send_message
      data_template:
        message: >-
          {% set device = trigger.event.data.text|lower|replace('turn on ','')|replace('turn off ','') %}
          OK, {{ trigger.event.data.text|lower|replace('turn','turning') }}.
        title: ''
        target: '{{ trigger.event.data.user_id }}'


- alias: 'Conversation Device Not Found'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: telegram_text
  condition:
    condition: template
    value_template: >-
      {% set device = trigger.event.data.text|lower|replace('turn on ','')|replace('turn off ','') %}
      {% for entity in states.group.controllable_devices.attributes.entity_id if states[entity.split('.')[0]][entity.split('.')[1]].name|lower == device -%}
      false
      {%- else -%}
      true
      {%- endfor -%}
  action:
    service: telegram_bot.send_message
    data_template:
      message: >-
        {% set device = trigger.event.data.text|lower|replace('turn on ','')|replace('turn off ','') %}
        I'm sorry. I cannot find the device named {{device}} in the house.
      title: ''
      target: '{{ trigger.event.data.user_id }}'

with this automation, I can send text message to my HA via Telegram using the same command for the conversation component such as “turn [on/off] [Friendly Name]”.

UPDATE 1: Limit the devices that can be controlled by using group. Then added the ability to response back the result of the request whether it is successful or not.

8 Likes

I also wish the conversation component can support more features such as…

  • “What is the status of [friendly name]” and then response back with the result.
  • “Switch [on/off] [friendly name]”

when was conversation added? I was looking for a way to have the telegram bot turn off specific entity using a template. What does your conversation config look like?

I didn’t add anything. just…

conversation:
  intents:
    ShoppingListAddItem:
      - Add {item} to my shopping list

…exactly the same with the example

Great! I was using Telegram Bot for controlling several devices but didn’t know about the conversation component so I created an automation for every command. Now this has much more use :slight_smile:

I have made some changes to this. Please see UPDATE 1 in the first post.

Is it possible to translate the “turn on” and “turn off” command?

Regarding to your conversation with telegram bot to turn on/off device

Do I have to put anything in the intent_script.yaml and conversation.yaml ?
As I didn’t use telegram bot. Can it trigger and service from something else to be able to turn on/off device?

Currently, I am able to use conversation and intent_script to turn on/off device, but I have to add in each of the devices in both conversation and intent_script. Is there anyway to make it shorter or more simpler like what you did?

example:

- alias: 'Conversation'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: telegram_text  <== can it be conversation or ?

  action:
    - service: conversation.process
      data_template:
        text: '{{ trigger.event.data.text|lower }}'
    - service: telegram_bot.send_message <=== can it be something?

Thank you very much. I have create telegram bot polly and your conversation with telegram bot work so nice.

Thanks! Works perfect! :smiley:

@masterkenobi I have followed your instructions and have added the group and the automation but when I turn on say my heater from Telegram it says its turning the heater on but nothing happens and it keeps on sending me the message that it is turning the heater on.

Do I have set up any script? because I only have conversation: in my configuration.yaml

Thanks.

there is a bug on Telegram webhooks in 0.65. Try changing to polling for now.

I am using polling never been on webhooks.

@masterkenobi

Still same issue with 0.67.1 on polling.

I get the “turning on …” message but nothing turns on.

I get an error saying “WARNING (MainThread) [homeassistant.core] Unable to find service sensor/turn_on”

Hi everybody,

@masterkenobi what have you wrote in the conversation component ?

thank you !

the conversation component from home assistant already support…

  1. add item to shopping list
  2. turn on/off devices

So for this share project you don’t need any setting in conversation and intentscript. You just need to copy the above share for.

  1. group.conrollable_devices <== under this group, put entity_id you want to turn on/off
  2. automation.coversation <== copy to your automation

I am trying to use your project but i have a doubt.
Must i insert friendly name inside the controllable devices group or not?
If not in conversation i must put the device name inserted into the group or only the friendly_name?

You need the friendly name for each entity_id under group.controllable_devices, this is easier for you to text with telegram bot.

For group.controllable_devices, There is a name given: Controllable Devices.

The automation turn on/off devices inside group.controllable_devices only.

1 Like

It doesnt work for me.

Can you share your code please for turn on/off.

Thanks.

It works for me, but not for lights !!
What do use as devices ?