Matrix integration not receiving commands

I have HA running via Docker and I’m using the Matrix integration to connect to my Matrix homeserver for notifications and receiving commands.

Here is my matrix setup in configuration.yaml:

matrix:
  homeserver: https://<domain>
  username: "@homeassistant:<domain>"
  password: <password>
  rooms: 
    - "#homeassistant:<domain>"
  commands:
    - word: "ha"
      name: ha_cmd

notify:
  - name: matrix_notify
    platform: matrix
    default_room: "#homeassistant:<domain>"

From my reading of the Matrix integration docs, this should cause a “matrix_command” event to be fired in Home Assistant when I send a message like “!ha testing” to the #homeassistant room on my Matrix server, but I’ve monitored for that event in the Developer Tools page event monitoring and no “matrix_command” events are firing when I send messages to that room.

I’m able to use the notify service to send messages to the configured Matrix server/room successfully, just not able to receive commands via the “matrix_command” event.

1 Like

I’m having the exact same issue.

Did you figure it out yet?

Edit: Running HA (hassio) on RPi4.

Nope, still not sure what’s going on. It seems to work for just a short time after I restart Home Assistant, and then stops working. Looking at the Home Assistant user I’ve added on my Matrix server, it shows as “offline”. So it seems like HA just stops listening after a bit or doesn’t reconnect or something.

Yes you are right, the user seems to be offline.

Slightly off topic:
Did you figure out how to verify the session home assistant uses
to get rid of the red exclamation mark next to all messages?

matrix

I don’t see that red exclamation mark on the messages from my HA user, though maybe it’s because I haven’t done the verification on my own session either. Couldn’t get it to work right for my own account.

I think the issue in both cases is that the matrix bot running in HA doesn’t support e2e encryption. make a channel that’s unencrypted and see if that helps.

The channel/room I’m using is already not encrypted:
image

Ah, I see and apologies: I missed your message about it working briefly. If it were an encrypted channel, it would never work.

I’ll see if I can reproduce it. I’d like to be able to rely on this instead of using slack or telegram and I currently have a terrible network connection so I suspect that I’ll be able to :smiley: At least if it has anything to do with disconnection issues.

So, I did a little testing and am able to get the sample chat client from that library to fail with enough bad network connectivity. I suspect this is what’s happening. Unfortunately, I don’t really know enough about HA to do anything about it right now but I’ve been enjoying the ecosystem and plan to get more familiar with it so I can make some stuff. Once I’m to that point, I’ll circle back here and see if I can lend a hand.

If my suspicions about network connectivity are correct, you could probably fix your issues by setting up a matrix server locally and federating it. Then connect HA to your local server and let your local server deal with the intricacies of disconnection and resync.

Hopefully someone who has a more informed clue will stumble upon this though and get you some answers sooner.

Same problem here.

Hmm…
The example in https://github.com/anoadragon453/nio-template ist working (inclduing encryption). With this I can send and receive messages in a matrix room.

I am also able to communicate with HA using the websocket API in the same stand-alone program (though I haven’t yet connected these two ends, but this shouldn’t be too complicated).

Both APIs are based on asyncio.

So I’m sure that I can implement what the HA core matrix component(?) does with a stand-alone program.

But what we actually need is a rewrite of the HA core matrix component itself (I mean integrated into HA).

However, I’m stuck at this point for several reasons and really need help from an experienced HA developer.

  • While I’m an experienced Python developer I have never before worked with async/await.
  • I have absolutely no experience with HA development - indeed I’m still struggling with the basics (nomenclature etc).
  • The async matrix client needs a native library (libolm) for encryption/decryption.
  • My HA works on a 4 GB Raspi 4 (based on the hassos_rpi-4.14.img.gz). I don’t know how I could develop there.
2 Likes

Hi,

I have the same problem. I have an not encrypted room. After start of HA the matrix user shows “online” and receives commands as expected. After a nightly or another reconnect of my connection the matrix user showing offline and don’t accept any command. But the matrix user is able to send notifications. But after sending the messages to the room the user is still offline and don’t receive anything until restarting my HA.

Christopher

Hello!
same here :frowning:
Has someone any updates on this?

Just found this post, here is mine 2cents:

my configuration.yaml:

##################################
notify:
    
  - name: matrix_notify
    platform: matrix
    default_room: "!ngOVAHjEysFUqyMfake:matrix.w00t.nl"

    ##################################
matrix:
  homeserver: https://matrix.w00t.nl
  username: "@HASS:matrix.w00t.nl"
  password: ***
  rooms:
    - "!ngOVAHjEysFUqyMfake:matrix.w00t.nl"
  commands:
    - word: help
      name: help
      rooms:
        - "!ngOVAHjEysFUqyMfake:matrix.w00t.nl"
    - word: pauze
      name: pauze
      rooms:
        - "!ngOVAHjEysFUqyMfake:matrix.w00t.nl"
    - word: song
      name: song
      rooms:
        - "!ngOVAHjEysFUqyMfake:matrix.w00t.nl"
    - word: "home"
      name: home
    - expression: "(?P<name>.*) stinkt"
      name: stink
    - expression: "My name is (?P<name>.*)"
      name: introduction
    - expression: "(?P<name>.*) is lief"
      name: lief

the automations:

alias: React to !song
description: ''
trigger:
  - platform: event
    event_type: matrix_command
    event_data:
      command: song
condition: []
action:
  - service: notify.matrix_notify
    data:
      message: >
        Het nummer wat op staat is: {{
        state_attr('media_player.mpd','media_content_id') }}
mode: single
alias: React to an stinkie
description: ''
trigger:
  - platform: event
    event_type: matrix_command
    event_data:
      command: stink
condition: []
action:
  - service: notify.matrix_notify
    data:
      message: Tijd om jezelf ff te wassen {{trigger.event.data.args['name']}}
mode: single

screen01

i’ve set this up a long time ago… maybe i forgot something to post but it still works

1 Like

I gave up receiving commands and sending replies last year.
Now, learning that in HA 2023.10, the old matrix integration was replaced by one based on matrix-nio (see https://github.com/home-assistant/core/pull/72797), I picked it up again.

For me the results changed: Sending and receiving works, also for long time.
But, it only works when the command is issued in the first default room:

  rooms:
    - "!LloKBjcdpIRBwvNaxs:myhomeserver"
    - "!VoRgWK8nCYxSQdXzGq:myhomeserver"
  commands:
    - word: testcommand
      name: testcommand
      rooms:
        - "!VoRgWK8nCYxSQdXzGq:myhomeserver"

Commands will only be ever recognized when sent to room !LloKBjcdpIRBwvNaxs:myhomeserver, regardless if other rooms are provided.

So I’m interested what your experience is since Home Assistant 2023.10.

I am using the matrix integration for some days now, it’s working very flawless for me. The only thing which annoys me the most is, that it’s not possible to verify the session and send encrypted messages.

Did you find a way to verify the HomeAssistant Session?

No, I wasn’t able to verify the session either.

Ah, wait. My seconds room has encryption activated. Thus the client won’t accept commands from there though it can post there.
I now created another unencrypted test room and yes, it takes commands from there.

So the real issue is, how to verify the session.

For me it’s not possible to send encrypted messages from home assistant to other users on my matrix server. But as you said, the problem could be caused by the unverified session.

I found this custom HACS integration on GitHub: GitHub - PaarthShah/matrix-nio-hacs: A HACS Integration to replace the core matrix Home Assistant integration.

But it doesn’t offer the possibility to verify the session I think. I will take a look at it again tomorrow and try to figure out if I could expand this integration or how to verify the session.

I could fork that repo and try to integrate this feature, perhaps I can switch it

That HACS integration was integrated into Home Assistant as of 2023.10 and replaced the old one, see https://github.com/home-assistant/core/pull/72797.

Also AFAIK matrix-nio offers a way to verify a session. But I don’t have the faintest idea how this could be done.

Oh, I didn’t notice the PR. Thanks for mentioning it.

I will take a deeper look into matrix-nio at the end of November.