Decoding JSON in MQTT - flags and values

Hi

I’m using some products that convert analog values into MQTT messages. I do not have control over the message data. It’s formatted similar to JSON but not quite the same.

{"sensorDatas":[{"flag":"AI1","value":0.00},{"flag":"AI2","value":0.00},{"flag":"AI3","value":0.00},{"flag":"AI4","value":0.00},{"flag":"AI5","value":0.00},{"flag":"AI6","value":0.00},{"flag":"AI7","value":0.00},{"flag":"AI8","value":0.00}],"time":"1746721053"} 

I understand bit about state topics and value templates but the key value pair is not together in this case. Can anyone tell me how to get these 8 values into a sensor please?

That is valid json data. What values are you interested in?

They all show as 0 in that data:

https://jsonpathfinder.com/

@tom_l I’m trying to get all 8 values - they are a value between 0.00 and 5.00. I need to make each one gauge in the frontend.

AI1 is analog input 1 and it’s corresponding value is the value after “value”:
AI2 is analog input 2 and it’s corresponding value is the value after “value”:

all the way to AI8

Thanks for your help

Is the list order always the same?

Yes it’s always in the same order. Even if one of the inputs is not used it’s always the same. The values are always to 2 decimal places.

It updates every 30 seconds

That makes it easy then:

# Example configuration.yaml entry
mqtt:
  sensor:
    - name: "AI1"
      state_topic: "your/topic/here"
      value_template: "{{ value_json['sensorDatas'][0]['value'] }}"
      unique_id: ai1_sensor"
    - name: "AI2"
      state_topic: "your/topic/here"
      value_template: "{{ value_json['sensorDatas'][1]['value']  }}"
      unique_id: ai2_sensor"
    - name: "AI3"
      state_topic: "your/topic/here"
      value_template: "{{ value_json['sensorDatas'][2]['value']  }}"
      unique_id: ai3_sensor"

# ... same for all the rest up to:

    - name: "AI8"
      state_topic: "your/topic/here"
      value_template: "{{ value_json['sensorDatas'][7]['value']  }}"
      unique_id: ai8_sensor"

You are a genius, thank you. I’ll try this afternoon and report back.