Telegram Bot - Users left house with callback option to cancel away scripts if false trigger, HELP

Hi All,

I have an automation that sends users a Telegram notification if they left home within the last 30mins and the house is now empty.

  • My wife and I both leave at the same time and no one else is home, we both get a notification
  • I leave but my wife is still home no one gets the notification
  • I leave and I was the only one at home, only I get the notification

Here’s an example of one user leaving:

- alias: Telegram Bot - House is Empty (Alex)
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: group.household
    to: 'off'
    for:
      seconds: 1
  condition:
    condition: and
    conditions:
    # Check if a House has already shutdown or has been put into Away Mode Manually (could have been set via the user before they left home via a MQTT push button by front door or via Alexa/Siri)
    - condition: state
      entity_id: input_boolean.away_mode
      state: 'off'
    # Check if user has left the house recently (two people may have left at a similar time, this condition will allow the action to be sent to both users rather than just the last one to appear as not_home)
    - condition: state
      entity_id: input_boolean.alex_leaving
      state: 'on'
  action:
    - service: notify.telegram
      data:
        message: "Alex, You've left home and the house is empty. House will shutdown in 30mins."
        data:
          inline_keyboard:
            - "Cancel Shutdown I'm still at Home!:/home-alex"

The notifications have an inline_keyboard button (“Cancel Shutdown I’m still at Home!:/home-alex”) that allows them to cancel the script that shuts down the house and set themselves to “home” by pressing the button.

If the user hits the button from the app, the shutdown script will be stopped and the message in the app will be replaced with “shutdown cancelled” and the button will be removed, using the following automation:

- alias: 'Telegram bot that edits the keyboard'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: telegram_callback
    event_data:
      data: '/home-alex'
  action:
    - service: telegram_bot.answer_callback_query
      data_template:
        callback_query_id: '{{ trigger.event.data.id }}'
        message: 'Cancelling Shutdown'
    - service: telegram_bot.edit_message
      data_template:
        message_id: 'last'
        chat_id: '{{ trigger.event.data.user_id }}'
        message: "Shutdown Cancelled"

My shutdown process currently has delays in the script of 30mins to give users time to cancel or allow my device_tracker components to update users location should they have a “blip” - once the shutdown on the house is complete, input_boolean.away_mode is set to “ON”.

What I would like to be able to do is as soon as the input_boolean.away_mode is set to ON, to replace the Telegram message and button that gives users the option to cancel the script that shuts down the house… as it is too late, the house is already shut down.

I’m struggling to get Telegram Bot to replace the message and inline_keyboard by a state trigger, rather than a telegram_callback trigger, I’ve tried the below but can’t get anything to work?

- alias: 'Telegram bot that edits the keyboard 2'
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: input_boolean.away_mode
    to: 'on'
  action:
    - service: telegram_bot.edit_message
      data_template:
        message_id: 'last'
        chat_id: '{{ trigger.event.data.user_id }}'
        message: "House has shutdown"

Or is there anyway I could fire a “telegram_callback” from HASS?

Hopefully someone will find the above useful and hopefully someone will be able to help with my problem :slight_smile:

Many thanks

Hello,
I’m trying to figure out how to get the message_id of a sent message and avoid using message_id: ‘last’ as you use in your automation.

Have you found any way to do it ? (it would prevent errors if I receive consecutive message before trying to update the inline keyboard of the first one)

Anyway, I was reading your code and found the error
At the end of your action, you have chat_id: '{{ trigger.event.data.user_id }}'
The chat_id will not be set correctly as the trigger here is not from telegram but from the state platform