Composing a state from multiple MQTT topics

Hi, I am using SAIC iSMART addon to integrate my MG EV. Works great (or as good as it can, considering that this is a Chinese car with Chinese SW that it integrates against).

The add-on integrates via MQTT and I have added an MQTT lock entity, which works for displaying LOCKED / UNLOCKED states as well as setting them.

Basically,
State is in .../locked and holds a bool
Cmd is in .../locked/set and is also a bool

However, I would like to show LOCKING and UNLOCKING states, which I would have to extrapolate from a combination of the state_topic and command_topic. Straight forward logics - but how can I do that in the best way in HA?

The MQTT Lock is designed to look at just the state_topic.

Just for clarity, here’s a table of the states (true is locked):

state_topic cmd_topic lock state
false false UNLOCKED
false true LOCKING
true false UNLOCKING
true true LOCKED

Thanks in advance!

Note: The real reason for wanting these in-between states, is that the car does not automatically send updates, when locking. So I have to force a refresh after e.g. 30 seconds. I need something to trigger the refresh, so I get the proper state of the locks after setting it.

Did you ever resolve this? I have a very similar thing with a garage door and using two sensors to determine if the door is open, closed, opening or closing.

I can use an automation to combine both topics into one but I would rather have the entire garage door definition in one place.

No, sorry.

@SynAckFin do you have the sensors available as entities in HA?
if so, you could bring them together in a template sensor.
I don’t know how your sensors are called and what state they use (on, off, true or false), but as an example I used sensor.door_openend and sensor.door_closed and the state ‘on’ when the sensor is active.

# state door
    - trigger:
      - platform: time_pattern
        seconds: /5  # checking state every 5 seconds
      sensor:
      - name: state door
        state: >
            {% set a = states('sensor.door_opened') %}
            {% set b = states('sensor.door_closed') %}
            {% if a == 'on' %}  # assuming when door is open the opened-detector hase state 'on'
            door opened
            {% elif b == 'on' %}  # assuming when door is closed the closed-detector hase state 'on'
            door closed
            {% else %}
            door opening or closing
            {% endif %}

not sure if this is what you are looking for?

I’ve used state changes in the sensors to generate an MQTT message that I then use as a state change topic for the cover entity. The garage door consists of 2 sensors and a 2 channel relay (open/close). It is provisioned via MQTT discovery and I was looking for a solution that could be provisioned entirely via MQTT discovery without having to manually add automations.

alias: GarageDoorFudge
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.doorbottom_contact
    from: "off"
    to: "on"
    variables:
      doorstate: Opening
  - platform: state
    entity_id: binary_sensor.doorbottom_contact
    from: "on"
    to: "off"
    variables:
      doorstate: Closed
  - platform: state
    entity_id: binary_sensor.doortop_contact
    from: "off"
    to: "on"
    variables:
      doorstate: Closing
  - platform: state
    entity_id: binary_sensor.doortop_contact
    from: "on"
    to: "off"
    variables:
      doorstate: Open
condition: []
action:
  - service: mqtt.publish
    metadata: {}
    data:
      qos: 0
      topic: z/b/c/d
      payload_template: |
        {
          "top":"{{ states('binary_sensor.doortop_contact') }}",
          "bottom":"{{ states('binary_sensor.doorbottom_contact') }}",
          "state": "{{ doorstate }}"
        }
mode: single