Variables in Actions (Telegram messages)

I have an automation that tells me when the fridge is open - with a temperature sensor inside the fridge transmitting by 433 MHz.
The automation works fine and sends a Telegram message.

Now I want to change it so the message includes the actual temperature from the sensor.
Below is as far as I can get.

- id: 4b75316ea5bc479fbebd4ca86b12e9f1
  alias: Fridge over temperature alarm
  trigger:
  - above: '2'
    entity_id: sensor.fridge_temp
    platform: numeric_state
  action:
  - service: telegram_bot.send_message
    data_template:
      title: '*HA Alert!*'
      target: '142XXXXXX'
      message: Fridge was opened.
  - service: telegram_bot.send_message
    data_template:
      title: '*HA Alert!*'
      target: '142XXXXXX'
      message: 'Fridge was opened. Temperature = {{ sensor.fridge_temp }}°C'
  - service: telegram_bot.send_message
    data_template:
      title: '*HA Alert!*'
      target: '142XXXXXX'
      message: 'Fridge was opened. Temperature = °C'

Only 2 of the 3 messages are sent - the 1st & 3rd.
The first is my original message.
The 2nd is the problem message - I am thinking the variable is the problem.
The 3rd message is sent ok - it is just a test message to ensure the automation isn’t getting stuck and also to confirm that there are no other characters causing the problem.

Can variable values not be included in automation actions (messages)?
Or do I have something else wrong?

your template isn’t complete.

try this:

message: 'Fridge was opened. Temperature = {{ states.sensor.fridge_temp.state }}°C'

2 Likes

Thanks very much!
I would have not have figured it out - I looked at a few different examples in the community postings that don’t include that ‘states.’ at the beginning. Included it and ‘.state’ at the end and it is working perfectly now.

Ah, that’s very helpful! Thank you.
How about adding the triggerID to the message (after setting this optional triggerID in the automation trigger section)?

using the example above it would be something like this:

message: 'Fridge was opened. Temperature = {{ states.sensor.fridge_temp.state }}°C. The trigger id was {{trigger.id}}'

Thank you.
trigger.id and id only do not work

I’m confused…

are you saying that {{trigger.id}} didn’t work?

If so then maybe posting what you are trying to do with code would help to give context.

I am trying to send the trigger ID with the telegram message:

alias: "Sicherheit: Bewegung vorm Haus detektiert (Überwachungsmodus)"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.haustur_motion
    from: "off"
    to: "on"
    id: Ring
  - platform: state
    entity_id:
      - binary_sensor.pir_sensor_with_relay_home_security_motion_detection
    from: "off"
    to: "on"
    id: GartenhausPIR
condition:
  - condition: or
    conditions:
      - condition: state
        entity_id: group.persons
        state: not_home
      - condition: state
        entity_id: input_boolean.nightmode
        state: "on"
      - condition: state
        entity_id: input_boolean.bewegungs_meldung_aussen
        state: "on"
action:
  - service: notify.mobile_app_iphone
    data:
      message: Bewegung im Garten vorne detektiert!
      title: Sicherheit
  - service: telegram_bot.send_photo
    data:
      url: http://192.168.68.74/cgi-bin/snapshot.cgi?1
      username: admin
      password: xxx
      caption: Bewegung im Garten vorne durch {{id}} detektiert.
mode: single

I tried:
{{id}}
{{trigger.id}}
In your example the are quotes, but the oroginal code in my case did not have quotes.
Adding quotes does not change anything though.

2 months…? it must not be very pressing. :grinning_face_with_smiling_eyes:

try this:

  - service: telegram_bot.send_photo
    data:
      url: http://192.168.68.74/cgi-bin/snapshot.cgi?1
      username: admin
      password: xxx
      caption: 'Bewegung im Garten vorne durch {{trigger.id}} detektiert.'

or if that doesn’t work then try this:

  - service: telegram_bot.send_photo
    data:
      url: http://192.168.68.74/cgi-bin/snapshot.cgi?1
      username: admin
      password: xxx
      caption: Bewegung im Garten vorne durch '{{trigger.id}}' detektiert.

and another few months (to much to fix :wink: and yes, it’s not pressing but still bothers me - sorry :blush:

Both of your suggestions don’t work.
i get:

Error running action
Error rendering data template: UndefinedError: 'trigger' is undefined

If you run it manually then the trigger will be undefined.

if it runs automatically then you shouldn’t get that error since the automation trigger variable is created.

1 Like

Sure - that totally makes sense!
Thank you for opening my eyes! :slight_smile:

Just for the record - this works!
Thank you!

1 Like