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 :
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.
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_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
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).