Using value_template and regex_search on mqtt 'text' payload

Hi
I am a hassio beginner and have managed to solve most of my basic system challenges with google and the generous advice given by this amazing community (thanks). I am struggling with a mqtt server message coming from an alarm panel where the payload is a text field of variable length: e.g. one or more Zones (ZXX) in every message e.g.

Topic: alarm/problem_report
Payload: Living room (Z15) low batt,
Fire (Z23) low batt,
Upstairs (Z24) low batt

(Each payload line is separated with \r\n).

I am attempting to either write automations triggered by each zone detected (these messages are always low battery), or create binary (battery) sensors to highlight the low battery status for each zone. I only need to find the zone code e.g. Z23.

I have tried many permutations of regex etc. but just can’t seem to get a trigger e.g. (created with the automation tool but copied from automations.yaml).

- id: AlarmPanelTest
  alias: Alarm Panel Test
  trigger:
  - platform: mqtt
    topic: alarm/problem_report
  condition:
  - condition: template
    value_template: '{{value|regex_search("Z15",ignorecase=FALSE) }}'
  action:
  - data:
      message: Living Room PIR - Low Battery
      notification_id: Alarm Panel Problem Z15
      title: Alarm low battery
      service: persistent_notification.create

I have tried single quotes and double single quotes surrounding the Z15 but nothing triggers.

I have also tried: value_template: ‘{{“Z15” in value}}’

The binary sensor looks like this:

  - platform: mqtt
    name: "Living room PIR"
    state_topic: "alarm/problem_report"
    device_class: battery
    value_template: '{{ value|regex_search(''Z15'', ignorecase=FALSE)}}'

It doesn’t change the battery status when the problem report.

I am wondering if I am misunderstanding the use of value_template or whether regex_search is the right tool and if so, what is returned when there is a match and whether it can be used as a condition?

I am also wondering whether I can have multiple automations triggering off the same message (one per zone)?

All thoughts and guidance gratefully appreciated…

I abandoned the binary sensor and solved using a standard sensor as follows:

- platform: mqtt
  name: "Patio Door PIR Battery"
  state_topic: "alarm/problem_report"
  device_class: battery
  value_template: '{% if value|regex_search(''Z15'', ignorecase=FALSE)%}Low{% else %}OK{% endif %}'

I’m having the same problem, I cannot get regex to work with an MQTT Binary Sensor with a Value Template.

This code just doesn’t change the status. It works in Developer Tools using Template, but doesn’t change state once in config.yaml. Any ideas anyone?

  - platform: mqtt
    name: "Test Detector"
    state_topic: "tele/RF_Bridge_Upstairs/RESULT"
    value_template: "{% if value_json.RfReceived.Data | regex_search('89B6') %}on{% else %}off{% endif %}"
    off_delay: 5
    device_class: motion
    qos: 1

The data coming from JSON is:

{"Time":"2020-04-13T21:14:42","RfReceived":{"Sync":12160,"Low":410,"High":1190,"Data":"89B6E0","RfKey":"None"}} %}

As @mykp says, it works with a Sensor, but not a Binary Sensor. I really need it to work with a Binary Sensor.

You need to return true or false, not on or off. Also, you don’t even need regex for your search.

  - platform: mqtt
    name: "Test Detector"
    state_topic: "tele/RF_Bridge_Upstairs/RESULT"
    value_template: "{{ '89B6' in value_json.RfReceived.Data }}"
    off_delay: 5
    device_class: motion
    qos: 1

and for you…

  - platform: mqtt
    name: "Living room PIR"
    state_topic: "alarm/problem_report"
    device_class: battery
    value_template: "{{ 'Z15' in value }}"

The reason it wasn’t working for you is because you weren’t returning true or false. Binary_sensors REQUIRE true or false as an output. Not ‘low’ and ‘OK’.

Hi @petro, thanks for the response!

I’ve tried this and the state still doesn’t change. Any other ideas?

binary_sensor:
  - platform: mqtt
    name: "Test Detector"
    state_topic: "tele/RF_Bridge_Upstairs/RESULT"
    value_template: "{{ '89B6' in value_json.RfReceived.Data }}"
    off_delay: 5
    device_class: motion
    qos: 1

what’s not changing? Is it always on?

The state is always ‘off’ never changes to ‘on’.

are there errors in your logs?

Yep.

No matching payload found for entity: Test Detector with state topic: tele/RF_Bridge_Upstairs/RESULT. Payload: True, with value template Template("{{ '89B6' in value_json.RfReceived.Data }}")

Finally fixed it, it needs a “payload_on” for the ‘True’ output:

  - platform: mqtt
    name: "Test Detector"
    state_topic: "tele/RF_Bridge_Upstairs/RESULT"
    value_template: "{{ '89B6' in value_json.RfReceived.Data }}"
    payload_on: "True"
    off_delay: 5
    device_class: motion
    qos: 1
1 Like

Ah, ok, apparently I was wrong with my other posts. I’ll scratch those out.

For an MQTT Binary Sensor, its default values for payload_on and payload_off are ON and OFF, respectively. Therefore the template can be like this:


- platform: mqtt
  name: 'Test Detector'
  state_topic: 'tele/RF_Bridge_Upstairs/RESULT'
  value_template: "{{ 'ON' if '89B6' in value_json.RfReceived.Data else 'OFF' }}"
  off_delay: 5
  device_class: motion
  qos: 1

For more information:

2 Likes

Yeah, that’s what I was wrong about with my previous posts. Confusing this with the rest sensor :man_facepalming:

Nice one - thanks