Templating not JSON value from sensor

hello everyone : my sensor reads the following through mqtt

  • topic is :growatt/data

what i obtain is :
{“realopper”:85,“batsoc”:75.0,“inputpow”:0.0,“constpow”:“Not_OK;”,“gridvolt”:246.4,“gridfreq”:49.98,“outputpow”:426.0,“invertercurr”:0.3,“opcurr”:2.0,“opdcvolt”:0.0,“pv1volt”:153.9,“pv1pow”:700.0,“pv1curr”:-0.1,ETCETC}

i want to create many mqtt sensors with each single value, up to now i managed only to find numbers and create a sensor in this way but this is not 100% easy and actually not a good solution :

  - platform: mqtt
    name: "growatt_realopper"
    state_topic: growatt/data
    unit_of_measurement: ''
    value_template:  >
      {{ (value | regex_findall_index('(\d+\.?\d+)', index=0) | float ) | round(2) }}

any suggestion how to do it?
thanks!

For starters, you should use the new method of configuring an MQTT Sensor.

Add this to your configuration.yaml file, execute Developer Tools > YAML > Reload Manually Configured MQTT Entities and then see if the resulting MQTT Sensor reports the values you want (in its attributes). If it reports unknown it means it hasn’t received a payload from its topic yet.

mqtt:
  sensor:
    - name: Growatt Realopper
      state_topic: growatt/data
      value_template: "{{ value_json.realopper }}"
      json_attributes_topic: growatt/data
      json_attributes_template: "{{ value_json | tojson }}"

this was actually my first trial, unfortunately is not working the sensor is

while mqtt explorer keeps receiving the data :

i’ve improved how to shown the data which contains number :

   - name: 'growatt_input_power'
     state_topic: 'growatt/data'
     unit_of_measurement: 'W'
     value_template:  >
       {{ value.split(',')[1] | regex_findall_index('(\d+\.?\d+)') }}  

still not a nice solution to me, and still i don’t know how to represent the non-number data.

Thanks a lot for your great help! all your posts about templating helped me a lot!

Giordano

Have you confirmed the payload published to growatt/data is valid JSON? If it isn’t, the value_json variable will contain nothing because the payload isn’t JSON. Check the Log for any errors rated to sensor.growatt_realopper.

actually you are right, the payload is not JSON formatted, actually all the problems start from this issue, for this reason i’ve used value instead of value_json.

here below is my last result which up to now provide the best solution, i’m sure there is a much elegant way to solve this :
This is used for the numeric values

    - name: Growatt Realopper
      state_topic: 'growatt/data'
      unit_of_measurement: ''
      value_template:  >
        {{ value.split(',')[0] | regex_findall_index('(\d+\.?\d+)') | float  | round(2)}} 

This is used for the non-numeric values

    - name: 'growatt_const_pow'
      state_topic: 'growatt/data'
      unit_of_measurement: 'W'
#index in the original list 3 , original variable in the list "constpow":"VALUE"
      value_template: "{{ value.split(',')[3] | replace('constpow', ' ')|trim }}"

unfortunately for the non-numerical value the output is not good : " ":"Not_OK;"
i tried to use again a value.split function but it didn’t work :frowning:

Yet the payload looks like JSON but, somewhere in its keys and/or values, fails to be compliant with the JSON standard.

I would report it as a bug (Growatt); it should be valid JSON.

If you post the entire payload into a JSON validator it will show you the part that isn’t compliant.

All techniques to extract information from the payload are complicated by its failure to be valid JSON. Here’s another way to get the value of the constpow key. It assumes constpow will always be the fourth item (index = 3).

    value_template: >
      {{ value.split(',')[3].split(':')[1].replace('"', '') }}
1 Like

this is exactly what i was trying to achieve! thanks so much for your posts on templating, now are much more clear to me!

Giordano

1 Like