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

I have got the following under controllable devices:

 controllable_devices:
  name: Controllable Devices
  entities:
   - light.bedroom_lights
   - light.gateway_light_34ce00908ea4
   - switch.kitchen_light
   - switch.heater
   - switch.tv
   - switch.water

and none of them work!

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

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

Even if you use the friendly_name ?

where is this friendly_name? I thought I am using the friendly_name.

For example for the switch.water the friendly name is “water” so when I use water nothing happens.

you can find Friendly_name here:

yep I am using the correct friendly name.

but still no joy.

I’d bet that this is not the reason but just in case: do you know that the indentation you put under controllable_devices is not correct? (YAML is a bit picky about this).

what do you mean buddy?

I checked it and its fine.

I mean that the spaces in what you have under controllable_devices
hasscomm01
could be the reason; this is what masterkenobi shared in his first post:
hasscomm02
but maybe what you have pasted here is not exactly the same you have in your config file and the issue is somewhere else.

nope thats not the issue.

I get this error saying “WARNING (MainThread) [homeassistant.core] Unable to find service sensor/turn_on” when I send a message on telegram to “turn on water”

Do you get similar issues?

Forget about telegram for a moment.

Have you enabled conversation component in the configuration?

Are you able to control the devices using your voice via the conversation component?

Actually, I am no longer using conversation.process service to control the devices via Telegram bot. I have changed it to homeassistant.turn_on (or off). It processes my request faster using this way. Here is the automation I am using…

automation:
  - alias: 'Control Devices'
    initial_state: 'on'
    trigger:
      platform: event
      event_type: telegram_text
    condition:
      condition: template
      value_template: >-
        {%- set text = trigger.event.data.text|lower -%}
        {%- set text = text.split(' ') -%}
        {%- if (text[0] == 'turn' or text[0] == 'switch') and (text[1] == 'on' or text[1] == 'off') -%}
        true
        {%- endif -%}
    action:
      - service: telegram_bot.send_message
        data_template:
          message: >-
            {%- set device = trigger.event.data.text|lower|replace('turn on ','')|replace('turn off ','')|replace('switch on ','')|replace('switch off ','') -%}
            {%- for entity in states.group.controllable_devices.attributes.entity_id if states[entity.split('.')[0]][entity.split('.')[1]].name|lower == device -%}
            OK, {{ trigger.event.data.text|lower|replace('turn','turning')|replace('switch','switching') }}.
            {%- else -%}
            I'm sorry. I cannot find the device named {{device}} in the house.
            {%- endfor -%}
          title: ''
          target: '{{ trigger.event.data.chat_id }}'
      - condition: template
        value_template: >-
          {%- set device = trigger.event.data.text|lower|replace('turn on ','')|replace('turn off ','')|replace('switch on ','')|replace('switch 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 -%}
      - service_template: >-
          {%- set text = trigger.event.data.text|lower -%}
          {%- set text = text.split(' ') -%}
          {%- set onoff = text[1] -%}
          homeassistant.turn_{{onoff}}
        data_template:
          entity_id: >-
            {%- set device = trigger.event.data.text|lower|replace('turn on ','')|replace('turn off ','')|replace('switch on ','')|replace('switch off ','') -%}
            {%- for entity in states.group.controllable_devices.attributes.entity_id if states[entity.split('.')[0]][entity.split('.')[1]].name|lower == device -%}
            {{ entity }}
            {%- endfor -%}

You still need the controllable_devices group.

3 Likes

I am able to control my devices via voice using the conversation component.

Your latest code works for me.

Thanks alot.

Both automation from the first one and also the latest one work perfect. The latest one works faster ^^

With this automation can we disable the old ones?
And more, may i use italian language in conversation with telegram bot?

you can use either old automation or new automation. Both are usable.

for the language, if you would see in the automation that catch the word 'turn on, turn off, switch on, switch off. You can try to your own language.

1 Like

I am using 0.77.3 version and with your automation i always get the error

"Error rendering template: UndefinedError: 'trigger' is undefined"

Any chance to have it again working properly?

Good stuff. Just tried it and it works.