Trigger-based binary sensor: how to intercept only needed events

I am doing first steps with a new format for template sensors - so far I have been using a legacy format.

There is a binary_sensor which should react on two events:

  • event_x: the sensor must be set to “ON”;
  • event_y: the sensor must be set to “OFF”.

Also:

  1. There are 11 of these sensors; each of them is related to some object (iPhone 5s, iPad 666S etc).
  2. Each event has an “entity_id” as a part of it’s data; this ID points to some object (mentioned above).
  3. Each sensor must react on an event with it’s object only.

With the legacy format I did this:

  1. Use 11 helpers “input_boolean” (for each object).
  2. Each “binary_sensor” = a corresponding “input_boolean”:
aaa_some_object_name:
  value_template: >
    {% set HELPER= "input_boolean.bbb_some_object_name" -%}
    {{ states(HELPER) }}
  1. Each helper is toggled by an automation (11 automations) triggered by those events:
    variables:
      OBJECT:  ### get a name from trigger.event.data.entity_id
      HELPER: >-
        {{ 'input_boolean.bbb_' + OBJECT}}

    trigger:
      - platform: event
        event_type: event_x   ### or event_y

    action:
      - service: homeassistant.turn_on    ## or turn_off
        data:
          entity_id: >-
            {{HELPER}}

It works fine.


Now I am trying to get rid of those automations & helpers (+ some educational purpose).

  - trigger:
      - platform: event
        event_type: event_x

      - platform: event
        event_type: event_y

    binary_sensor:
      - name: aaa_some_object_name
        state: >-
          {% set DEVICE_TRACKER = "device_tracker.zzz_some_object_name" -%}
          {%- if trigger.event.data.entity_id == DEVICE_TRACKER -%}
            {%- set EVENT_TYPE = trigger.event.event_type -%}
            {%- if EVENT_TYPE == "event_x" -%}
              {{ "on" }}
            {%- else -%}
              {{ "off" }}
            {%- endif -%}
          {%- else -%}
            {{ states("binary_sensor.aaa_some_object_name") }}
          {%- endif %}

This part:

            {%- if EVENT_TYPE == "event_x" -%}
              {{ "on" }}
            {%- else -%}
              {{ "off" }}
            {%- endif -%}

is to toggle the sensor dependingly on an event.

This part:

          {%- if trigger.event.data.entity_id == DEVICE_TRACKER -%}
            ...
            ## handle event
            ...
          {%- else -%}
            {{ states("binary_sensor.aaa_some_object_name") }}
          {%- endif %}

is to ignore the event for another sensor.
If I leave this section w/o assigning to the previous state - my sensor is toggled to OFF due to these rules.

I wonder am I doing it properly?
Or may be there is any other more elegant way?

I don’t use Event triggers very often, but wouldn’t it be possible to make your triggers more specific by using the event data variables and assign each trigger an ID? Then in the state template could just match the trigger.id to your desired state output.

Something like:

  - trigger:
      - platform: event
        event_type: event_x
        event_data:
          entity_id: device_tracker.zzz_some_object_name
        id: 'on'
      - platform: event
        event_type: event_y
        event_data:
          entity_id: device_tracker.zzz_some_object_name
        id: 'off'
    binary_sensor:
      - name: aaa_some_object_name
        state: "{{ trigger.id }}"

EDIT: Fixed missing quote marks mentioned below

2 Likes

This is genius)))
Went for testing

During HA startup got a warning.
Guess it should be:

        id: "on"