Extract value from MQTT and rename

Dear All,

i have been trying now for more than one week but unfortunately have not suceeded yet, so i am asking you for help:

I am working on a LORAWAN automation an receive packages over MQTT.
The packages look like this:

{"deduplicationId":"xyz","time":"2025-01-24T06:08:03.175745+00:00","deviceInfo":{"tenantId":"xyz","tenantName":"ChirpStack","applicationId":"xyz","applicationName":"xyz","deviceProfileId":"xyz","deviceProfileName":"door","deviceName":"doorsensor","devEui":"xyz","deviceClassEnabled":"CLASS_A","tags":{}},"devAddr":"01a3554b","adr":false,"dr":0,"fCnt":63,"fPort":10,"confirmed":false,"data":"DDABAAAlAAAAAA==","object":{"MOD":1.0,"LAST_DOOR_OPEN_DURATION":0.0,"ALARM":0.0,"DOOR_OPEN_TIMES":37.0,"DOOR_OPEN_STATUS":0.0,"BAT_V":3.12},"rxInfo":[{"gatewayId":"xyz","uplinkId":16660,"gwTime":"2025-01-24T06:08:03.175745+00:00","rssi":-90,"snr":10.5,"channel":5,"location":{},"context":"4VsnzA==","crcStatus":"CRC_OK"}],"txInfo":{"frequency":867500000,"modulation":{"lora":{"bandwidth":125000,"spreadingFactor":12,"codeRate":"CR_4_5"}}},"regionConfigId":"eu868"}

The important information are contained in the "object-part:

"object":{"MOD":1.0,"LAST_DOOR_OPEN_DURATION":0.0,"ALARM":0.0,"DOOR_OPEN_TIMES":37.0,"DOOR_OPEN_STATUS":0.0,"BAT_V":3.12}

In order to handle the information i have created a sensor in my configuration.yaml:

mqtt:
  sensor:
    - name: "Door_state"
      unique_id: lorawan_state
      state_topic: "xyz"
      value_template: > 
        {% if is_state('value_json.object.DOOR_OPEN_STATUS','1.0') %} open
        {% else %} closed
        {% endif %}
      device:
        identifiers: lorawan_door
        manufacturer: Dragino
        name: LDS02
    - name: "Door_Voltage"
      unique_id: lorawan_battery
      state_topic: "xyz"
      value_template: "{{ value_json.object.BAT_V }}"
      unit_of_measurement: "V"
      device:
        identifiers: lorawan_door
        manufacturer: Dragino
        name: LDS02

The battery voltage is working like a charme, but i do not get the values for the state translated into “open/closed”.
Just coding "value_template: “{{ value_json.object.DOOR_OPEN_STATUS }}” is handing over correctly the “1.0” or “0.0”

mqtt:
  sensor:
    - name: "Door_state"
      unique_id: lorawan_state
      state_topic: "xyz"
      value_template: "{{ value_json.object.DOOR_OPEN_STATUS }}"
      device:
        identifiers: lorawan_door
        manufacturer: Dragino
        name: LDS02

I have also ttried:

      value_template: > 
        {% if value_json.object.DOOR_OPEN_STATUS == "1.0" %} open
        {% else %} closed
        {% endif %}

This is unfortunately also not working.

What am i doing wrong?

How can i get the open/closed status from the 1.0 or 0.0 signal?

Thanks a lot
Florian

is_state is incorrect, here.It’s used to check the state of HA entities

Just do

{{ 'open' if value_json["object"]["DOOR_OPEN_STATUS"] == 1.0 else 'closed' }}

And because it’s not a state but coming from a JSON object, the value is actually a number:

You could (should?) make this a binary sensor with device class of door:

mqtt:
  binary_sensor:
    - name: "Door_state"
      unique_id: lorawan_state
      state_topic: "xyz"
      value_template: "{{ value_json['object']['DOOR_OPEN_STATUS']|int(0) == 1 }}"
      device_class: door
      device:
        identifiers: lorawan_door
        manufacturer: Dragino
        name: LDS02
1 Like

Thank you so much!
@koying: Your solution is working fine.
@Troon: Unfortunately i was not able to get it running. Altough i see the advantegas of having a binary sensor, i can also live with the first solution.

Thanks again!
Florian

Troon’s way will work (and is a better idea) if you take my value template and replace “open”/“closed” with “ON”/“OFF” (uppercase)

Oh yeah, the default payload of the MQTT binary sensor. Forgot about that.

      value_template: "{{ 'ON' if value_json['object']['DOOR_OPEN_STATUS']|int(0) == 1 else 'OFF' }}"