{{trigger.entity_id}} not working as expected & underscore in telegram message

I am trying to get the entity_id which triggered an automation and send it with telegram.
With {{trigger.entity_id}} I get a telegram message but at the line where the entity_id should be there is just an empty line, no error entry in the log.
The message I get:

Kitchen LED Light
[emoji light_bulb] ON

2018-11-14 26:52:45

This is my automation:

- alias: notify_1_mqtt_lights_status
  initial_state: true
  hide_entity: false
  trigger:
    - platform: state
      entity_id: 
        - light.kitchen_node_1_led_light_1
        - switch.sonoff_basic_1_relay
  action:
    - service: notify.notify_1
      data_template:
        title: "{{ trigger.from_state.attributes.friendly_name }}"
        message: "
          {% if trigger.to_state.state == 'on' %}
            \uD83D\uDCA1 ON           
          {% elif trigger.to_state.state == 'off' %}   
            \u26AB OFF
          {% else %}          
            Error for light status.
          {% endif %}

          {{trigger.entity_id}}                    
                    
          {{now().strftime('%Y-%m-%d %H:%M:%S')}}"  

Can someone help?

I’m sure you will probably get the same results, but maybe try, to get a nicer output

{{ trigger.to_state.attributes.friendly_name }}

it’s possible that the entity_id isn’t populated when your trigger has multiple entities. Try grabbing it from the state object.

{{ trigger.to_state.entity_id }}
1 Like

Thank you.

{{ trigger.to_state.entity_id }} did thet trick.
I also tried different things:

message: "
  {% if trigger.to_state.state == 'on' %}
    \uD83D\uDCA1 ON           
  {% elif trigger.to_state.state == 'off' %}   
    \u26AB OFF
  {% else %}          
    Error for light status.
  {% endif %}
  
  1: {{ trigger.from_state }}
  
  
  2: {{ trigger.to_state }}
  
  
  3: {{ trigger.from_state.attributes.friendly_name }}
  
  
  4: {{ trigger.to_state.attributes.friendly_name }}
  
  
  5: {{ trigger.from_state.entity_id }}
  
  
  6: {{ trigger.to_state.entity_id }}
      
  
  {% if trigger.entity_id == 'light.kitchen_node_1_led_light_1' %}
  7: WORKING
  {% endif %}          
            
  {{now().strftime('%Y-%m-%d %H:%M:%S')}}"      

Which gives me this message:

Kitchen LED Light
💡 ON 
1: <state light.kitchennode1ledlight1=off; friendlyname=Kitchen LED Light, supportedfeatures=40 @ 2018-11-14T17:16:04.838961+01:00>

2: <state light.kitchennode1ledlight1=on; friendlyname=Kitchen LED Light, supportedfeatures=40 @ 2018-11-14T17:19:22.173917+01:00>

3: Kitchen LED Light

4: Kitchen LED Light

5: light.kitchennode1ledlight1

6: light.kitchennode1ledlight1

 7: WORKING 
2018-11-14 17:19:22

Interesting is that trigger.entity_id is working when used in an if but not when I want to send it as a message with {{trigger.entity_id}}

{{ trigger.to_state.entity_id }} removes the underscores from the entity_id, why is that?

1 Like

I’m not sure… that’s odd!

That is quite odd, and might be an issue. I have a similar issue where I want to parse the entity ID and pull a number from the name based on the underscores. This may be my issue.

I tried:

Works:

  {% if trigger.entity_id == 'light.kitchen_node_1_led_light_1' %}
  7: with underscore
  {% endif %}  

sends: 7: with underscore

Not working:

  {% if trigger.entity_id == 'light.kitchennode1ledlight1' %}
  7: without underscore
  {% endif %} 

sends an empty line

When I use trigger.from_state.entity_id instead of trigger.entity_id the same happens.

Telegram callback:
I am also using telegram callback, same thing here:
The callback is waiting to receive : /dafang_1_get_1_snapshot
in message {{trigger.event.data.data}} telegram sends /dafang1get1snapshot

Works:

   {% if trigger.event.data.data == '/dafang_1_get_1_snapshot' %}
   with underscore
   {% endif %} 

sends: with underscore

Not working:

  {% if trigger.event.data.data == '/dafang1get2snapshots' %}
  without underscore
  {% endif %}  

sends an empty line

In summary:

{{trigger.to_state.entity_id}} sends light.kitchennode1ledlight1,it removes the underscores when sent as a telegram message.

trigger.entity_id is working when used in an if but not when I want to send it as a telegram message with {{trigger.entity_id}}

entity_id is always sent without an underscore, but when you want to use it in an if statement you have to use underscore in order to get a true if statement.
___Working: {% if trigger.entity_id == 'light.kitchen_node_1_led_light_1' %}
Not working: {% if trigger.entity_id == 'light.kitchennode1ledlight1' %}

___Working: {% if trigger.from_state.entity_id == 'light.kitchen_node_1_led_light_1' %}
Not working: {% if trigger.from_state.entity_id == 'light.kitchennode1ledlight1' %}

___Working: {% if trigger.event.data.data == '/dafang_1_get_1_snapshot' %}
Not working: {% if trigger.event.data.data == '/dafang1get2snapshots' %}

I opened an Issue on github.

Sounds like a bug to me. Lets wait and see how they respond.

I wanted to let you know that I used your example, my triggers were input_select.word_word_word_number, and the results from

  5: {{ trigger.from_state.entity_id }}
  
  
  6: {{ trigger.to_state.entity_id }}

did include underscores. I am on 81.6

Thank you for trying.
I am on 80.2
I should update and try it again.

I’m having a weird issue with entity ID though, even though it seems like it should work. Rather than retype, I’ll just link my post here in case anyone has any ideas or recommendations. At this point I’m about to give up and just do a long if elif statement, which would be a bit annoying.

Thanks in advance for any help

Justed updatet to 0.82.0

  5: {{ trigger.from_state.entity_id }}


  6: {{ trigger.to_state.entity_id }}

and still no underscores in the message for the entity_id

After that I tried it with an input_select and input_number and still no underscores.

Just for giggles add this to your output:

8: {{ "_" }}

9: _

And post your results.

I removed the timestamp from the message.

- not working
Error:
Error sending message: Can't parse entities: can't find end of the entity starting at byte offset 27. Args: (373476846, 'Kitchen LED Light\n⚫ OFF _'), kwargs: {'parse_mode': 'Markdown', 'disable_notification': False, 'disable_web_page_preview': None, 'reply_to_message_id': None, 'reply_markup': None, 'timeout': None}b

byte offset 27 is the underscore.

{{ '_' }} not working
No double quotes because it use them around the whole message.
Error:
Can't parse entities: can't find end of the entity starting at byte offset 27. Args: (373476846, 'Kitchen LED Light\n⚫ OFF _'), kwargs: {'parse_mode': 'Markdown', 'disable_notification': False, 'disable_web_page_preview': None, 'reply_to_message_id': None, 'reply_markup': None, 'timeout': None}

It might be a problem with the telegram component or telegram itself.

Try parse_mode html for your telegram component. You’re currently using markdown as your parse mode and it doesn’t like uneven numbers of underscores.

2 Likes

So I’m guessing underscores mean something in markdown for telegram.

@naitsimp, that’s why your entity id’s are getting changed, and its on telegrams side. So the underscores are actually there, it’s just that markdown is suppressing them.

EDIT: FYI I debugged into telegram and it does not doctor your messages. It just passes them through. So this is 100% on the telegram side.

1 Like

Ok. I understand. Thank you.

So underscores won’t work at all in the message when using markdown.
An exception is when you want italic text. _text_

Can you show an example of how the automation should look using parse_mode? I haven’t seen that used before. See my automation below. Whenever someone isn’t home, their state is returned as not_home and it causes the automation to error out.

- id: telegramstatusinquiry
  alias: Telegram - Status inquiry
  trigger:
  - event_data:
      command: /status
    event_type: telegram_command
    platform: event
  condition: []
  action:
  - service: telegram_bot.send_message
    data_template:
      message: |
        {%- set person1 = states.person.person1 -%}
        {%- set person2 = states.person.person2-%}
        {%- set lock = states.lock.schlage_be468_touchscreen_deadbolt_locked -%}
        {%- set thermostat = states.sensor.thermostat_temperature -%}
        {%- set garage = states.cover.garage -%}
        {{ person1.name }}: {{ person1.state }}
        {{ person2.name }}: {{ person2.state }}
        {{ lock.name }}: {{ lock.state }}
        {{ thermostat.name }}: {{ thermostat.state }}
        {{ garage.name }}: {{ garage.state }}

You don’t put it in your automation, you put it in the telegram component.

telegram_bot:
  - platform: polling
    api_key: YOUR_API_KEY
    parse_mode: html
    allowed_chat_ids:
      - 12345
      - 67890
1 Like

Perfect. Thank you

1 Like