I want to create an automation that triggers on all mqtt messages topic ending with /batt and a condition when the value of this message is below 10%
The first issue is on the topic. It doesn’t work with #/batt
Second issues is on the value condition since I want to make it generic for all mqtt messages ending with /batt I don’t want to link it to a specific entity.
Third is to add the entity id in the message
that is exactly my issue. I try to find a way to link the topic condition to an entity_id so I can add that to the message.
I now subscribe on the topic /bat that is send by multiple sensors so linked to multiple sensor entity_id’s.
Is there a way to link the topic message I receive again to an entity_id ?
As you have already discovered, this is not an acceptable way to use a wildcard (#) in an MQTT topic:
topic: "#/batt"
The # wildcard can appear at the end of a topic, like this:
topic: "home/sensors/#"
or alone:
topic: "#"
You didn’t mention the format of the battery topic but maybe the + wildcard can be useful. It is designed to allow for any sub-topic like this:
topic: "home/OpenMQTTGateway/BTtoMQTT/+/batt"
That depends entirely if there is information in the topic or in the payload that can be used to make an association with an entity_id. Before anyone can help with that, you will have to provide examples of the topic and payload.
Suggestion:
You have already made MQTT Sensors to report temperature. Have you considered using the same approach and create MQTT Sensors to report battery level?
Alternatively, you don’t need to create separate battery level sensors and can incorporate it into your existing temperature sensors. Try enhancing your existing temperature sensor configuration so that the battery level is reported as an attribute.
- platform: mqtt
name: ATC_1F0F33 Tem Hum 1 tem
state_topic: home/OpenMQTTGateway/BTtoMQTT/A4C1381F0F33/tempc
json_attributes_topic: home/OpenMQTTGateway/BTtoMQTT/A4C1381F0F33/batt
json_attributes_template: '{ "battery": {{ value }} }'
NOTE
This combined sensor might not work because Home Assistant expects the attributes topic to receive a JSON payload and it appears (from what you have described) that it isn’t in JSON format. If it fails to work then the fallback is to create a separate sensor to report battery level.