Telegram message with variables


I’m trying to send a message to telegram with a variable in the message:

    - service: notify.telegram
        title: "title"
        message: "device: {{ states.device_tracker.device1.state }}"

In the HA log I have:

2017-06-29 15:02:39 ERROR (Thread-8) [homeassistant.components.telegram_bot] Error sending message
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/telegram_bot/", line 414, in _send_msg
    out = func_send(*args_rep, **kwargs_rep)
  File "/home/homeassistant/.homeassistant/deps/telegram/", line 125, in decorator
    result = func(self, *args, **kwargs)
  File "/home/homeassistant/.homeassistant/deps/telegram/", line 158, in decorator
    return Bot._message_wrapper(self, url, data, *args, **kwargs)
  File "/home/homeassistant/.homeassistant/deps/telegram/", line 146, in _message_wrapper
    result =, data, timeout=kwargs.get('timeout'))
  File "/home/homeassistant/.homeassistant/deps/telegram/utils/", line 252, in post
  File "/home/homeassistant/.homeassistant/deps/telegram/utils/", line 194, in _request_wrapper
    raise BadRequest(message)
telegram.error.BadRequest: Can't parse entities in message text: can't find end of the entity starting at byte offset 24

When I do this I don’t have a problem and receive the message so it must be something in the variables:

    - service: notify.telegram
        title: "title"
        message: "device:"

Thank you


I’ve been having the exact same problem!

It only happens if that device is not home. If I’m home it gets the status and sends it to. I think it’s something to do with the underscore in ‘not_home’, I was going to try and get the template to change not_home to ‘away’ and see if that fixes it, but I haven’t had chance.

Let me know if you work it out.

I use “data” and not “data_template” and everything works fine as expected

    - service: notify.telegram
        message: "{% if is_state('device_tracker.oneplus_thomas', 'home') %} Thomas is at home.\n{% else %} Thomas is away.\n{% endif %}
                 {% if is_state('device_tracker.s4mini_maud', 'home') %} Maud is at home.\n\n{% else %} Maud is away.\n\n{% endif %}
                 Plafonnier is {{ states.light.plafonnier.state}} (/plafonnier)\n"

^ Interesting, I’ll try that first!

Here’s mine, like I say it works perfectly if somebody is home, as soon as we’re both out I get the same error as @EchoWisky

Link removed

Like @anon43302295 says, it probably is a result of the parse mode, which is trying to make italic text out of bla.foo_bar, where the enclosing _ is missing. The number of _'s always has to be even.
I worked around that problem by setting the notifiers parse_mode option to html. Have a look at the polling or webhook documentation (depending on which one you use) and set the parse_mode to html. That way not_home won’t be a problem anymore.

Thanks, that was it! If I take a closer look at the message I can see the italic font. After setting the parse_mode to html it’s gone.
Now the next problem is this:

- alias: 'change home notif'
  hide_entity: True
    - platform: state
        - device_tracker.t1_t1
        - device_tracker.t2_t2
    - platform: state
        - input_boolean.lin_home
        - input_boolean.sam_home
        - input_boolean.erik_home
        - input_boolean.lara_home
    - service: notify.telegram_erik
        title: "{{ trigger.to_state.attributes.friendly_name }}:"
        message: "Home: {{ trigger.to_state.state }}."
    - service: notify.pushbulet_erik
        title: "{{ trigger.to_state.attributes.friendly_name }}"
        message: "Home: {{ trigger.to_state.state }}" 

When the automation is executed I get the message in pushbullet with all the correct data in it.
Fot Telegram I have this in the log:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/helpers/", line 101, in async_render
    return self._compiled.render(kwargs).strip()
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/", line 385, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'trigger' is undefined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/", line 237, in _step
    result = next(coro)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/", line 1021, in _event_to_service_call
    yield from service_handler.func(service_call)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/notify/", line 125, in async_notify_message
    kwargs[ATTR_TITLE] = title.async_render()
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/helpers/", line 103, in async_render
    raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: 'trigger' is undefined

It looks like I can’t get a variable from the trigger part.


You are missing to: (and/or from:) from your triggers.

So I need that for Telegram but not for pushbullet?
Take a look at the code. If this automation is triggered I don’t have the message in Telegram but the Pushbullet works fine. So I don’t need the to/from.


Sorry, totally misread.

Is it because you’re not using data_template for the telegram one, but you are for the Pushbullet one?

Yep, that was it. I changed it because @lambtho sugested it in his first reply and forgot to put it back.

Thank you for the help!!


Sorry for that… It works for me though, Don’t know why…

No problem :wink:

Hi, I’m trying to use this to get the state of sensor.router_update sent in the push notification, this does not work:

- service: notify.telegram
    title: "Router New FW Available"
    message: "New firmware {{ states.sensor.router_update.state }} available for router"

This does work:

- service: notify.telegram
    title: "Router New FW Available"
    message: "New firmware available for router"

I have also tried changing “data” to “data_template” but didn’t work.

Can anyone help? Was planning on using lots of automation’s this way.


1 Like