Signal Messenger using received message

I’m trying to use the Signal Integration and trigger automations with received messages.

Home Assistant should receive messages and switch either a red lightbulb or a green lightbulb on, depending on the message text. It should also reply with “OK, green” or “OK, red”.

I started with the standard REST sensor from the docs:

- resource: "http://127.0.0.1:8080/v1/receive/<number>"
  headers:
    Content-Type: application/json
  sensor:
    - name: "Signal message received"
      value_template: "" #this will fetch the message
      json_attributes_path: $[0].envelope
      json_attributes:
        - source #using attributes you can get additional information, in this case, the phone number.

But this gives me chatty messages every few seconds in my log (JSON result was not a dictionary or list with 0th element a dictionary), which I’d rather avoid because it pollutes the log when looking for real errors.

Is there a standard way to avoid those log messages?

I’ve tried this to get rid of the warnings by configuring the sensor like this:

resource: "http://127.0.0.1:8080/v1/receive/<number>"
headers:
  Content-Type: application/json
sensor:
  - name: "Signal message sender"
    value_template: "{{ value_json[0]['envelope']['sourceNumber'] if value_json else '' }}"
  - name: "Signal message received"
    value_template: "{{ value_json[0]['envelope']['dataMessage']['message'] if value_json else '' }}"

This seems to get rid of the warning and gives me two entities for the message text and the message sender.

However, when testing I can see that when receiving a message sometimes only one of the sensors is set and the other remains at unknown.
Can anyone explain what’s going on there?

It may have to do with the message being re-set to unknown before the sender?

The automation for turning on the green light looks like this. It tries to make sure that HA only reacts when my numbers sends the command.

triggers:
  - trigger: template
    value_template: >-
      {{ is_state('sensor.signal_message_sender', '+44444444444') and
      (states('sensor.signal_message_received') | lower == 'green')
      }}
actions:
  - action: notify.signal
    data:
      message: OK, green

Is that a viable way of implementing this automation?