Single automation for Telegram message if device is switched on/off?

Hi!

I want to receive notifications on Telegram if a device has been switched on/off.

Currently I am making 2 automations for every device

trigger:
  - platform: device
    type: turned_off
    device_id: 
    entity_id: switch.4x4_lights
    domain: switch
condition: []
action:
  - service: telegram_bot.send_message
    data:
      message: Lights for 4x4 have been switched off
mode: single

I have to repeat this automation for when it switches on, as the message changes.

Any easier/clutter free way of doing this instead as I have multiple switches going on/off throughout the day

trigger:
  - platform: state
    entity_id:
      - switch.4x4_lights
      - switch.abc
      - switch.def
      ...
action:
  - service: telegram_bot.send_message
    data:
      message: >
        {{ trigger.to_state.name }} have been switched {{ trigger.to_state.state }}

This kind of works but I am receiving multiple notifications repeatedly.

For eg : Switch got triggered to on at 9:00 am

9:00am : Fan have been switched on
9:15am: Fan have been switched on
9:22am: Fan have been switched on
9:37am: Fan have been switched on

Well then, those switch entities must be changing more than just their state (i.e., attributes must be changing.) So, try changing the trigger to:

  - platform: state
    entity_id:
      - switch.4x4_lights
      - switch.abc
      - switch.def
      ...
    to:
      - 'on'
      - 'off'
1 Like