How to simplify automation, using trigger "state" for Binary Sensor

I m new, so dont be too hard on me (my first post even, trying to figure out formatting for code also). Lol. SHould be easy for you guys. Trying to figure out how can i simplify automation using state as trigger for binary sensor.

Here s what i m currently doing.

- id: '1556193269263'
  alias: Front Gate
  trigger:
  - entity_id: binary_sensor.door_window_sensor_158d0001f3a162
    platform: state
    to: 'on'
  condition: []
  action:
  - data:
      message: 'Notify: Gate opened'
    service: notify.tellme

The code works to notify me if it s opened. How can i modify is so that i can have another message to notify me when state is “to Off” instead of to On, in the single automation ID. The way i m doing now, i create another automation (with diff ID) to trigger when state is off. Wanna have it simplified. Thought is something like;

if state to on, message is xxxx.
if state to off, message is yyyy.

- id: '1556193269263'
  alias: Front Gate
  trigger:
    - entity_id: binary_sensor.door_window_sensor_158d0001f3a162
      platform: state
  action:
    - service: notify.tellme
      data_template:
        message: >
          {% if is_state('binary_sensor.door_window_sensor_158d0001f3a162', 'on') %}
            'Notify: Gate opened'
          {% else %}
            'Notify: Gate closed'
          {% endif %}
1 Like

Hi @tom_l,

Thanks! That works, although i have to further read to understand how the syntax works.
Except one small issue, i m getting the notification twice. Any idea why? The notify.tellme triggers twice, according to the log.

Update: well, sometimes, after 2,3- minutes, i got another notifications. more than 2x

The automation triggers on any change of state of the sensor (no state specified).

The action decides which message to send based on the current state of the sensor using a data_template.

Assuming you deleted the old automation my guess for the duplicate messages is a debouncing problem. You are actually receiving more than one state change when the window is opened or closed (e.g. sensor contacts don’t open cleanly form 1 to 0 or vise versa).

Try this and see if it helps:

- id: '1556193269263'
  alias: Front Gate
  trigger:
    - entity_id: binary_sensor.door_window_sensor_158d0001f3a162
      platform: state
      to: 'on'
      for:
        seconds: 2
    - entity_id: binary_sensor.door_window_sensor_158d0001f3a162
      platform: state
      to: 'off'
      for:
        seconds: 2
  action:
    - service: notify.tellme
      data_template:
        message: >
          {% if is_state('binary_sensor.door_window_sensor_158d0001f3a162', 'on') %}
            'Notify: Gate opened'
          {% else %}
            'Notify: Gate closed'
          {% endif %}
1 Like

I guess you are right. I tired the code with other binary sensor (of different brand/make), and it worked perfectly, no double notifications.

I will now try the one you suggested, which if i understand correctly, the state need to change for at least 2 seconds before it triggers the automation, so, “unclean” change will be ignored.

Thanks a bunch!

Update: The new code worked flawlessly with the first sensor.