Meshtastic gate sensor node JSON message

Hey everyone,
I was lucky enough to be given two T Beam meshtastic nodes for Christmas and have been having a play.
I have managed to get them up and running with the App and talking to each other etc. Also managed to fit a switch to one and configure as a sensor to send an MQTT message into Mosquito on HA. So far so good.
I have then used a sensor to get the message like this:

sensor:

################################################################################
### T Beam Meshtastic gate sensor ##############################################
################################################################################

  - name: "gate sensor"
    unique_id: "gate_sensor_messages"
    state_topic: "msh/ANZ/2/json/MY CHANNEL/MY DEVICE"
    value_template: >-
      {% if value_json.from == 3234014136 and 'text' in value_json.payload %}
        {{ value_json.payload.text | trim }}
      {% else %}
        {{ this.state }}
      {% endif %}

This gives me a state of ‘Gate triggered detected’ when I press the button.
So this is where I am stuck, I made an automation, Some I did my self and some of it is copied from different sources (with a little AI) but this is what I have:

- id: '1737355387967'
  alias: Notify Gate Triggered with Reset
  trigger:
  - platform: mqtt
    topic: msh/ANZ/2/json/MY CHANNEL/MY DEVICE
    value_template: '{{ value_json.from == 3234014136 and value_json.payload.text is defined and value_json.payload.text.strip() == "Gate triggered detected" }}'
  action:
  - service: notify.alexa_media_family_echo_dot
    data:
      message: "We have visitors through the bottom gate, release the vicious sheep, hide the naughty chickens."
      data:
        type: announce
  - delay:
      seconds: 7
  - service: mqtt.publish
    data:
      topic: msh/ANZ/2/json/The_Crumps/!c0c31914
      payload: '{"from": 3234014136, "payload": {"text": "Gate closed"}}'
      retain: false

This sort of works but seems to trigger on every MQTT message either to or from that device.
What am I missing?
Cheers

You need to match a payload.

  trigger:
  - platform: mqtt
    topic: msh/ANZ/2/json/MY CHANNEL/MY DEVICE
    payload: "Gate triggered detected"
    value_template: "{{ value_json['payload']['text'] if value_json['from'] == 3234014136 else '' }}"

Are you running a recent version of HA? Your automation syntax is not the latest, despite the automation ID being yesterday’s timestamp. I’d expect:

triggers:
  - trigger: mqtt

rather than trigger / platform. Still works, but just curious.

Strongly recommend against that for HA config.

Hey Troon,
Thanks for you response and help.
HA is the last but one version but I have been using HA for a fair time and have some old automations from a long time ago, so I havent kept up with some of the new syntax I dare say… plus snippets from other places. Also jumped back and forth between automations.yaml in the files and the visual editor to get things working etc… So, I dont fully understand Jinja or JSON but try to get by and am not a programmer… I only use the AI for comparison or help on what something means etc I wouldnt fully use it.
I have had a read through the triggers info before, although a lot went over my head.

This is what I now have in the visual editor.

I have tried triggering them from the bottom up and all of the then do section executes fine on their own.

The first part does seem to do something as watching the sensor that I showed in the first post, it triggers and sets the state correctly.


Then nothing else happens strangely, but I must be missing something? Did I have to add something to the line you provided in the value template?
From my understanding the template is saying if a payload of Gate triggered detected is received from device 3234014136 within the text part then continue with the automation, else do nothing.
If it wasn’t triggering why does the state change as it should… I just love to hate electronics…
Any ideas?

If you have a working sensor, why not just trigger off the state of that?

Alternatively, do the device test in the conditions:

- id: '1737355387967'
  alias: Notify Gate Triggered with Reset
  triggers:
    - trigger: mqtt
      topic: msh/ANZ/2/json/MY CHANNEL/MY DEVICE
      value_template: "{{ value_json.get('payload',{}).get('text','') }}"
      payload: "Gate triggered detected"
  conditions:
    - condition: template
      value_template: "{{ trigger.payload_json['from'] == 3234014136 }}"
  actions:
  - action: notify.alexa_media_family_echo_dot
    data:
      message: "We have visitors through the bottom gate, release the vicious sheep, hide the naughty chickens."
      data:
        type: announce
  - delay:
      seconds: 7
  - action: mqtt.publish
    data:
      topic: msh/ANZ/2/json/The_Crumps/!c0c31914
      payload: '{"from": 3234014136, "payload": {"text": "Gate closed"}}'
      retain: false

Here, the value_template in the trigger selects the contents of ['payload']['text'] using get() to protect against those keys not existing: that is then compared with the payload string “Gate triggered detected”.

If that trigger fires, the condition checks that the from key of the message was the correct number before proceeding.

If that still doesn’t work, have a look at some automation traces.

Hello,
Thanks for the advice, I tried to just trigger it off the sensor. That’s what I thought you could do, but it doesn’t work. If I say trigger on change from anything to Gate triggered detected it does nothing…

Will give your second suggestion a go later and report back…

Edit:
Nope using the GET and using the condition doesn’t work either.
I have looked at the traces part and it says there are no traces to see? I have never used these so may be doing something wrong…

So for anyone in the future I just solved this.
In my MQTT.yaml

################################################################################    
### Sensor setup ###############################################################
################################################################################
sensor:

################################################################################
### T Beam Meshtastic gate sensor ##############################################
################################################################################

  - name: "gate sensor"
    unique_id: "gate_sensor_messages"
    state_topic: "msh/ANZ/2/json/MY DEVICE/MyID"
    value_template: '{{ value_json.payload.text }}'

and in automations

- id: 6352f8ee2d2f4c6c9329ae1b0b4a8985
  alias: Notify Gate Triggered with Reset
  triggers:
  - trigger: template
    value_template: '{{ ''Gate triggered detected'' in states(''sensor.gate_sensor'')
      }}'
  conditions: []
  actions:
  - action: notify.alexa_media_family_echo_dot
    data:
      message: We have visitors through the bottom gate, release the vicious sheep,
        and hide the naughty chickens.
      data:
        type: announce
    enabled: true
  - delay:
      seconds: 7
  - action: mqtt.publish
    data:
      topic: msh/ANZ/2/json/MY DEVICE/MyID
      payload: '{"from": 3234014136, "payload": {"text": "Gate closed"}}'
      retain: false

It was the value template in the automation that stumped me for a while. Enjoy.