Integrating Aqara Hub and Text Sensor Control

Hi everyone, I wanted to share my recent experience integrating the Aqara M2 hub with Home Assistant and setting up a relay control based on a text sensor. It’s been a bit of a journey, but I’ve learned a lot along the way!

First, I encountered an issue when trying to add the Aqara M2 hub to Home Assistant. The error message indicated a failure to discover the gateway. After some research, I realized that specifying the IP address of my Home Assistant instance as the interface helped resolve the problem. It’s a small tweak but made a big difference!

Next, I focused on setting up a relay to turn on and off based on the state of a text sensor from a media player. My goal was to have the relay off when the media player is idle or off and on when it’s playing. Initially, my code wasn’t working as expected. After reviewing the logs, I noticed that the sensor was correctly reporting its state, but the automation wasn’t triggering the relay as intended.

I decided to simplify the logic. Instead of using complex conditions, I streamlined the if-else statements to directly check the sensor’s state. This approach worked like a charm! Here’s the revised code snippet:

  • platform: text_sensor
    name: “Chromecast Power”
    entity_id: media_player.kelder_chromecast
    on_value: “idle”
    off_value: “off”

  • automation:
    alias: “Relay Control”
    trigger:
    platform: state
    entity_id: text_sensor.chromecast_power
    condition:
    condition: state
    entity_id: text_sensor.chromecast_power
    state: “idle”
    action:

    • service: switch.turn_off
      target:
      entity_id: switch.relay

    condition:
    condition: state
    entity_id: text_sensor.chromecast_power
    state: “playing”
    action:

    • service: switch.turn_on
      target:
      entity_id: switch.relay

This setup ensures that the relay behaves exactly as intended, responding to the media player’s state changes. It’s satisfying to see everything work seamlessly now!

I’d love to hear from others who have successfully integrated Aqara hubs or set up similar automations. Any tips or experiences to share would be greatly appreciated! :raised_hands: