Example of the value_template solution for users with OpenMQTTGateway
I would like to share a working value_template solution for users that are on OpenMQTTGateway and not Tasmota. There are a few small differences.
First Tasmota returns the data as a string and it is the value in HEX.
OpenMQTTServer returns the value as a number in decimal. This is important as you need to quote the hex string but it will not work if you quote the decimal number
The JSON Payload is different (value_json.value)
First a Binary Sensor for a sensor with both on code and off code (window/door sensors reporting open and closed). In this example I have 3 RF bridges all reporting the sensors in their own topics
OpenMQTTGateway/RFBridge1/433toMQTT
OpenMQTTGateway/RFBridge2/433toMQTT
OpenMQTTGateway/RFBridge3/433toMQTT
I do not care that I get 3 messages that trigger the sensor 3 times. The objective is to ensure the opening or closing of a window is detected. We use the wildcard + in the sensor in HA so it listens on all 3 bridges and triggers 1 to 3 times depending on coverage and noise. Note that the data is not quoted as it is a number and not a string
binary_sensor:
- platform: mqtt
name: "Kitchen Window RF"
state_topic: "OpenMQTTGateway/+/433toMQTT"
value_template: >-
{% if value_json.value == 13718474 %}
{{'ON'}}
{% elif value_json.value == 13718478 %}
{{'OFF'}}
{% else %}
{{states('binary_sensor.kitchen_window_rf') | upper}}
{% endif %}
Same setup but here we use a Motion sensor that only sends a code when detecting motion. There is no off code. We use a 180 second time-out
binary_sensor:
- platform: mqtt
name: "Hallway Motion RF"
state_topic: "OpenMQTTGateway/+/433toMQTT"
value_template: >-
{% if value_json.value == 13621870 %}
{{'ON'}}
{% else %}
{{states('binary_sensor.hallway_motion_rf') | upper}}
{% endif %}
off_delay: 180
Finally an automation triggered by an RF button we have in the kitchen. Here used to trigger a fun message via Alexa. Note that for automations there is no issue with error logging. You can have as many automations you want listening to the same MQTT topic and the payload does not need to be matched. But again note that different value_template needed for OpenMQTTGateway compared to Tasmota.
For this automation I do not use a wildcard as it would trigger the beer message 2-3 times and I would develop an alcohol problem
automation:
- alias: 'Black Button'
initial_state: true
trigger:
platform: mqtt
topic: "OpenMQTTGateway/RFBridge1/433toMQTT"
condition:
condition: template
value_template: '{{ trigger.payload_json.value == 4389249}}'
action:
action:
- service: script.alexa_tts_message
data:
message: 'Would you like a little beer?'
I hope this is useful for people.
All these examples also work with Tasmota but then you need a different value_template to decode the JSON payload. value_json.RfReceived.Data and you then quote the hex number in double quotes -“EB67A1”
Kenneth