Help constructing json, using secrets

I’ve built this kit: https://sensor.community/en/sensors/airrohr/ and I configured all sensors in ESPHome :-). I do manage to post JSON data to their API and the madavi API. I’m now struggling to post JSON to opensensebox.

The problem I’m facing is that this API expects dynamic keys related to my sensor, I don’t want to hardcode these keys. Instead, I want to use them from the secrets (the reason is that these keys are only for my device + I want to share the config with others).

So I was trying injecting secrets like this:

json: |-
  root[!secret opensensebox_sensorid_pm10] = id(${devicename}_pm10).state;
  root[!secret opensensebox_sensorid_pm25] = id(${devicename}_pm25).state;

When trying to install this fails:

error: 'secret' was not declared in this scope
             root[!secret opensensebox_sensorid_pm10] = id(${devicename}_pm10).state;
             ^

The resulting JSON should be simple for exampel:

{
	"sensor175643576": 5,
	"sensor254543554": 3
}

So maybe there is another way I could get this done, eg. a string template?

The secret must be the entire value of a key: value pair. So:

json: !secret sensorid

And how would I add the values, eg id(${devicename}_pm10).state ?

To the secrets file.

sensorid: id(${devicename}_pm10).state

Pretty sure substitutions wont work there though.

I haven’t managed to get that working, but I managed to find a super duper easy solution, opensensemap API had an option to accept the same JSON as luftdaten, so in the end, I managed to get what I want and it was not needed to use these keys.