Telegram sends message without trigger + warning when restarting

Hi everyone,

when I restart HA I have only sometimes the problem that my automation gets triggered and sends me a telegram message when it shuts down and after it has started. The log is showing the following six entries:

2021-04-06 16:49:47 WARNING (MainThread) [homeassistant.components.automation.telegram] Telegram: Already running
2021-04-06 16:49:47 WARNING (MainThread) [homeassistant.components.automation.telegram] Telegram: Already running
2021-04-06 16:49:47 WARNING (MainThread) [homeassistant.components.automation.telegram] Telegram: Already running
2021-04-06 16:49:47 WARNING (MainThread) [homeassistant.components.automation.telegram] Telegram: Already running
2021-04-06 16:49:47 WARNING (MainThread) [homeassistant.components.automation.telegram] Telegram: Already running
2021-04-06 16:49:47 WARNING (MainThread) [homeassistant.components.automation.telegram] Telegram: Already running

My automation looks as follows:

- id: 'telegram'
  alias: Telegram
  description: ''
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.test_kontakt_contact
        - binary_sensor.haustur_contact
        - binary_sensor.hwr_tur_contact
        - binary_sensor.wz_tur_1_contact
        - binary_sensor.wz_tur_2_contact
        - binary_sensor.wz_tur_3_contact
        - binary_sensor.wz_tur_4_contact
      attribute: contact
  action:
    - service: notify.telegram_all
      data:
        message: "{{state_attr(trigger.entity_id, 'friendly_name')}} {% if states(trigger.entity_id) == 'on' %}offen{% else %}geschlossen{% endif %}"

In the logbook I can find this entry for the sensor which has been triggered:

Has been triggered by state of binary_sensor.haustur_contact

Do you see any problem here? Why does this contact sensor gets triggered and why it is only one sensor?

Best regards

When home assistant starts the binary sensor states will change from unknown to something.

Your trigger is set to trigger on any change of state, or attribute, for all the binary sensors as you have not specified a from or to state.

You should be able to prevent it occurring with this condition:

condition:
  condition: template
  value_template: " {{ trigger.from_state.state != 'unknown' }}"

Edit: actually that won’t work. I just realised you are triggering on an attribute. Try this instead:

  trigger:
    - platform: state
      entity_id:
        - binary_sensor.test_kontakt_contact
        - binary_sensor.haustur_contact
        - binary_sensor.hwr_tur_contact
        - binary_sensor.wz_tur_1_contact
        - binary_sensor.wz_tur_2_contact
        - binary_sensor.wz_tur_3_contact
        - binary_sensor.wz_tur_4_contact
      attribute: contact
      from:
        - 'on'
        - 'off'

Do not try to do this in the UI automation editor. It has a bug that will convert the from state list to:

from: 'on, off'

Which is not correct, it should be from: 'on', 'off'

1 Like

I added the from property but then telegram is not working anymore. The state gets changed in the logbook, but telegram does not gets triggered. I also tried adding the “to” property also with on and off, but it also did not worked.

Beside of that: Is there a way to see the change from unkown state into a defined state? The history tab does not show any change when I restart HA on the sensors.

It seems I had an issue with my configuration. Now it is working. Additionaly to the “from” property I added “to” to prevent messages when my Zigbee2Mqtt shuts down and all sensors become unavailable.

Here my working automation:

- id: telegram
  alias: Telegram door/window sensor
  description: 'Sends Telegram message when door/window sensors state changes'
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.test_kontakt_contact
    - binary_sensor.haustur_contact
    - binary_sensor.hwr_tur_contact
    - binary_sensor.wz_tur_1_contact
    - binary_sensor.wz_tur_2_contact
    - binary_sensor.wz_tur_3_contact
    - binary_sensor.wz_tur_4_contact
    from:
      - 'on'
      - 'off'
    to:
      - 'on'
      - 'off'
  action:
  - service: notify.telegram_all
    data:
      message: '{{state_attr(trigger.entity_id, "friendly_name")}} {% if states(trigger.entity_id)
        == "on" %}offen{% else %}geschlossen{% endif %}'
1 Like