Automation based on mqtt message with wildcard # and value condition

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

automation:

  • alias: low_battery
    initial_state: ‘on’
    trigger:
    platform: mqtt
    topic: “#/batt”
    condition:

    • condition: numeric_state
      entity_id: sensor.
      below: 10

    action:

    • service: notify.notify
      data:
      message: “ALARM! Low Battery {{ }}”

Where is this entity_id you have mentioned? Is it in the payload or is it part of the topic’s path?

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 ?

Can you provide an example topic + message you are looking to parse?

  • platform: mqtt
    name: ATC_1F0F33 Tem Hum 1 tem
    state_topic: “home/OpenMQTTGateway/BTtoMQTT/A4C1381F0F33/tempc”

this is my mqtt config from one of the many sensors so I was hoping to make a generic automation

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.

  - platform: mqtt
    name: ATC_1F0F33 Battery
    state_topic: home/OpenMQTTGateway/BTtoMQTT/A4C1381F0F33/batt
    device_class: battery
2 Likes

You might also have some luck with enabling Home Assistant discovery on the OpenMQTTGateway. That page has details on how you can get data into HA.

Thanks for the feedback. I have to learn json first so will have to play with this a bit to see if it works.