MQTT JSON - processing Incoming data - Value template - one value controls processing of another value

Extracting a value in JSON object, but only if another value is true

Incoming data: (its from a LoraWan Server)

{"applicationID":"6","applicationName":"Lab","devEUI":"0000000000000002","deviceName":"dev-0000000000000002","timestamp":1612360964,"fCnt":177,"fPort":1,"data":"0C800C4E","data_encode":"hexstring","adr":false,"rxInfo":[{"gatewayID":"ac1f09fffe00aba2","loRaSNR":6.3,"rssi":-105,"location":{"latitude":-33.xxxxx,"longitude":18.xxxxx,"altitude":15}}],"txInfo":{"frequency":867700000,"dr":5}}

Wanted value:

“data”:“0C800C4E”

but only if:

fport == 1 “fPort”:1,

Do I need to use a script of can this be done within jinja sensor template?

Any ideas would be very much appreciated!
P

You can do it with jinja, but you have not said what you want if data != 1.

If you want it to retain the previous value and the template sensor entity_id is sensor.test

value_template: "{{ value_jason.data if value_json.fport == 1 else states('sensor.test') }}"

Hi Tom_I,
Thanks very much for looking at this and giving me a pointer…

Im basically tying to setup MQTT sensors for each Fport.

I will be receiving JSON objects every 10 mins with fports being set between 1 and 10 and I would like a sensor for each one as each fport will represent a different data type.

EG:

Fport Data:
1 Temperature
2 Light level

I tried this:

 platform: mqtt
    state_topic: "RAK-application/6/device/0000000000000003/rx"
    name: "test1"
    value_template: "{{ value_jason.data if value_json.fport == 1 else states('sensor.test') }}"

But Im not seeing any data on test1

P

This:

Must match this:

So:

 platform: mqtt
    state_topic: "RAK-application/6/device/0000000000000003/rx"
    name: "test1"
    value_template: "{{ value_jason.data if value_json.fport == 1 else states('sensor.test1') }}"

Thanks.

I tried in the Developer Tools:

{% set value_json={“applicationID”:“6”,“applicationName”:“Patricks Lab”,“devEUI”:“0000000000000002”,“deviceName”:“dev-0000000000000002”,“timestamp”:1612368549,“fCnt”:423,“fPort”:1,“data”:“10CC0C62”,“data_encode”:“hexstring”,“adr”:false,“rxInfo”:[{“gatewayID”:“ac1f09fffe00aba2”,“loRaSNR”:5.8,“rssi”:-103,“location”:{“latitude”:-33.990200,“longitude”:18.473170,“altitude”:15}}],“txInfo”:{“frequency”:867300000,“dr”:5}}
%}
{{ value_jason.data if value_json.fPort == 1 else states(‘sensor.test’) }}

RESULT:

UndefinedError: ‘value_jason’ is undefined

Any Ideas?
:wink:

Ugh. Stupid autocorrect (or me).

json not jason

Try this:

 platform: mqtt
    state_topic: "RAK-application/6/device/0000000000000003/rx"
    name: "test1"
    value_template: "{{ value_json.data if value_json.fport == 1 else states('sensor.test1') }}"
1 Like

I owe you beer dear friend.
Thanks very much!
Patrick

1 Like