How to do this in a single automation?

I have ISY994 which generates an event isy994_control with either DON or DOF data. I want to write a single automation to turn a switch on or off depending on DON or DOF. How would I do it? Here are the DON and DOF event data and desired action.

{
    "event_type": "isy994_control",
    "data": {
        "entity_id": "sensor.kitchen_1",
        "control": "DON",
        "value": 0,
        "formatted": 0,
        "uom": "",
        "precision": "0"
    },
    "origin": "LOCAL",
    "time_fired": "2020-10-20T18:12:16.906668+00:00",
    "context": {
        "id": "c95ada1212ff11ebabeab5c3cca007cb",
        "parent_id": null,
        "user_id": null
    }
}
{
    "event_type": "isy994_control",
    "data": {
        "entity_id": "sensor.kitchen_1",
        "control": "DOF",
        "value": 0,
        "formatted": 0,
        "uom": "",
        "precision": "0"
    },
    "origin": "LOCAL",
    "time_fired": "2020-10-20T18:12:18.423294+00:00",
    "context": {
        "id": "ca4244d412ff11eb86bcff47ca60ab3b",
        "parent_id": null,
        "user_id": null
    }
}

Action pseudo-code:

action:
  entity_id: switch.under_cabinet_lights
  service_template: >
    {% if trigger.data.control == 'DON' %}
      switch.turn_on            
    {% elif trigger.data.control == 'DOF' %}
      switch.turn_off 
    {% endif %}

Thanks,
Arun

I do that all over the place. Here’s a simple example:

- alias: CR Lights On
  initial_state: true
  trigger:
    - platform: event
      event_type: isy994_control
      event_data:
        entity_id: light.cr_light
  condition: "{{ trigger.event.data.control in ('DON', 'DOF') }}"
  action:
    - entity_id: group.cr
      service: homeassistant.turn_{{ 'on' if trigger.event.data.control == 'DON' else 'off' }}

I think I would sometimes get other events, hence the condition.

Your pseudocode would almost work as-is, albeit a bit more wordy - you’re just missing “event” in the trigger variable.

Why not in the action just use the homeassistant.toggle service call?

Because that does not meet the stated requirement of having DON switch the light on an DOF switch it off. These things can get out of sync.

Very correct. The DON and DOF are generated by a mini remote switch and it blinks green on ON and red on OFF. If I used toggle, then things will get out of sync.

Thank you so much for the solution. I really wrote the pseudo code without any knowledge. Is there any documentation on what attributes of any object I can use? I am have read so much but this part still eludes me. This kind of code imparts true power to the Home Assistant.

If you go to the Developer Tools / States page you can see all of your entities, their states and attributes.

Many configuration / automation examples at

The main variables are described here, with the link to the state object telling you what you can access from there for state triggers. For events, you start with trigger.event.data and then follow the event structure to the member you want, as you did.