Using a sensor as a trigger to toggle a light

So I have zigbee2mqtt setup with my Xiaomi wall switches which have 2 buttons (left and right)

I’ve got them integrated into home assistant and can see events coming in etc. but I’m struggling to specify the light i wanna toggle by reading the event data.

This is my event for a press on the left button:

{
  "type": "event",
  "id": 1,
  "event": {
    "time_fired": "2019-01-19T11:47:44.369961+00:00",
    "event_type": "state_changed",
    "data": {
      "new_state": {
        "last_updated": "2019-01-19T11:47:44.369783+00:00",
        "context": {
          "user_id": null,
          "id": "850d9dcf6a9b44b6870b04dcd92211a2"
        },
        "last_changed": "2019-01-19T11:47:44.369783+00:00",
        "entity_id": "sensor.office_switch",
        "attributes": {
          "battery": 100,
          "friendly_name": "office_switch",
          "voltage": 3035,
          "linkquality": 68,
          "icon": "mdi:toggle-switch"
        },
        "state": "left"
      },
      "entity_id": "sensor.office_switch",
      "old_state": {
        "last_updated": "2019-01-19T11:46:13.923169+00:00",
        "context": {
          "user_id": null,
          "id": "ff678a241f1e440188ffda613a3f9aee"
        },
        "last_changed": "2019-01-19T11:46:13.923169+00:00",
        "entity_id": "sensor.office_switch",
        "attributes": {
          "battery": 100,
          "friendly_name": "office_switch",
          "voltage": 3035,
          "linkquality": 99,
          "icon": "mdi:toggle-switch"
        },
        "state": "right"
      }
    },
    "context": {
      "user_id": null,
      "id": "850d9dcf6a9b44b6870b04dcd92211a2"
    },
    "origin": "LOCAL"
  }
}

Okay so I thought let’s use state_changed for a light toggle here is my automation:

- alias: "Bedroom Left Light Switch"     
  trigger:
      - platform: event
        event_type: state_changed
        event_data:
            entity_id: sensor.office_switch
            new_state:
                state: "left"
  action:
      - service: light.toggle
        data:
            entity_id: light.office_light

Now this automation doesn’t toggle the light at all. If i remove the new_state part I can toggle the light but the light gets toggled with both buttons, but I only want to toggle it when the state_changed event says something about state: “left”

What am I doing wrong?

Where exactly are you seeing that JSON that represents the event?

Is there an entity in your system named sensor.office_switch? And does it state change to ‘left’ when you click the left button?

My guess is that you should be using a state trigger instead of an event trigger. Something like:

- alias: Bedroom Left Light Switch
  trigger:
    platform: state
    entity_id: sensor.office_switch
    to: 'left'
  action:
    ...

That json is copied from Hass Configurator, in which you can observe all events going on.

The issue with a state trigger is that the light will only turn on when the state changes. So I have assuming the state is left, when I press the left button, state will still be left, i would have to press right first and the left. Now the light turns on.

I had this setup already going, but well for obvious reasons, that’s not what I want.

Ok, don’t know about Hass Configurator.

So, that’s a state_changed event that state triggers react to. Of course, like you say, if you use to: 'left', it will only fire when the “state string” (i.e., state.state) changes. Apparently when you click the button there will be a state_changed event, even if the “state string” doesn’t change (i.e., only an attribute changes.)

So, try this:

- alias: Bedroom Left Light Switch
  trigger:
    platform: state
    entity_id: sensor.office_switch
  condition:
    condition: template
    value_template: >
      {{ trigger.to_state is not none and
         trigger.to_state.state == 'left' }}
  action:
    ...

THANK YOU! This works!

I should have thought of that, I need to learn value_templates more

1 Like