Publish to specific MQTT topic when entity state changes

I have two sensors for my BMW. The battery soc and the range. I need the values to specific mqtt topics, when their state changes.

sensor.i3_120_verbleibende_batterie_in_prozent needs to be published to openWB/set/vehicle/1/get/soc

sensor.i3_120_verbleibende_reichweite_insgesamt needs to be published to openWB/set/vehicle/1/get/range

Whats the easiest way to achieve that? I already saw there’s an integration MQTT statestream, but thats not what I need as I cannot specify the topic where to publish the new state.

Thanks in advance for your help

alias: Publish Battery Data
triggers:
  - id: 'soc'
    trigger: state
    entity_id: sensor.i3_120_verbleibende_batterie_in_prozent
    not_to: unavailable
  - id: 'range'
    trigger: state
    entity_id: sensor.i3_120_verbleibende_reichweite_insgesamt
    not_to: unavailable
conditions: []
actions:
  - action: mqtt.publish
    data:
      topic: openWB/set/vehicle/1/get/{{ trigger.id }}
      payload: "{{ trigger.to_state.state }}"
mode: queued

I’m pretty new to Home Assistant. Where exactly do I put that? Is this a mqtt configuration and I have to put it into mqtt.yaml?

That’s an automation.

Start Home Assistant’s Automation Editor like this:

  • Settings → Automation & scenes → Create Automation → Create new automation

  • Click the overflow menu in the upper right corner (three vertical dots) and select Edit in YAML.

  • Delete the existing YAML code displayed in the editing window.

  • Copy the example I provided above and paste it into the Automation Editor’s editing window then click Save.


Do you already have MQTT installed and configured? Because that’s a prerequisite for any MQTT-related project. This automation will fail to publish anything unless you have the MQTT integration installed as well as an MQTT Broker.

Cool, the automation itself works. My only problem ist that the value isn’t transmitted via MQTT correctly.

I have the following configuration: I have MQTT installed on Home Assistant as an addon.

There is another MQTT on my OpenWB-Wallbox.

I have configured the HA Broker to establish a bridge to the OpenWB-MQTT.

#
# bridge to openWB Wallbox
#
connection openwb2
local_clientid openwb2.mosquitto
address 10.0.0.8:1883

#Sensors Controller
topic openWB/system/ip_address in
topic openWB/system/version in
topic openWB/system/lastlivevaluesJson in

#Sensors per Chargepoint
topic openWB/chargepoint/3/get/# in
topic openWB/chargepoint/3/config in
topic openWB/vehicle/# in
topic openWB/bat/6/get/# in
topic openWB/counter/4/get/# in
topic openWB/pv/5/get/# in
topic openWB/set# out

topic openWB/# in

When a MQTT state on the OpenWB changes, I get the update in Homeassistant immediately, so getting values from the other MQTT works properly.

Only when I send values to the other OpenWB MQTT, the value isn’t transported. I validated this behaviour by testing with MQTT Explorer. When I publish the topic openWB/set/vehicle/1/get/soc on the remote MQTT (10.0.0.8), the value gets updated. If I do the same on the Home Assistant MQTT, there is no effect.

So this seems like a configuration problem to me?

OK, I found the problem

needs to be changed to

topic openWB/set/# out

So there was only the / missing between set and #

Thanks a lot four your help with the automation. This is realy easy!

1 Like

One bonus question: In another scenario I have to entities. Both are numbers. Every time, when one of both entities changes it’s state, they need to be summed up and published to a MQTT topic. Can I do the sum operation also within the automation or do I need to create a helper?

Can you give me an example?

The entities names are:
solax_today_s_yield
solaxslave_today_s_yield

The topic is
openWB/set/pv/5/get/exported

If I understood your requirements correctly, the automation would look something like this:

alias: Publish Solax Sum
triggers:
  - trigger: state
    entity_id:
      - sensor.solax_today_s_yield
      - sensor.solaxslave_today_s_yield
    not_to: unavailable
conditions: []
actions:
  - action: mqtt.publish
    data:
      topic: openWB/set/pv/5/get/exported
      payload: >
        {{ (states('sensor.solax_today_s_yield') | float(0) +
          states('sensor.solaxslave_today_s_yield') | float(0))
          | round(2) }}
mode: queued