Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 95324

How do I transform two lists into a dict in a rest sensor's value template?

$
0
0

@mrmachine wrote:

Given that my REST resource returns the following JSON:

{"range":[1572526800,1572605220],"labels":["voltage","mains","oven","air_conditioners_1","hot_water_system","solar_analytics","power_points_1","lights_1","power_points_2","lights_2","air_conditioners_2","fronius_inverter","latronics_inverter","air_conditioners","consumption","grid_export","grid_import","grid_import_neg","lights","mains_inverted","power_points","production","self_consumption","unmeasured"],
"data":[[244.8,-863.9,0.1,15.1,99.5,1.2,100.8,17.6,186.9,15.7,16.8,1098,224.5,32,458.5,863.9,0,-0,33.3,863.9,287.7,1322.5,458.5,4.8]]}

How would I zip the labels and data lists into a dict, so I end up with:

{'voltage': 244.8, 'mains': -863.9, 'oven': 0.1, 'air_conditioners_1': 15.1, 'hot_water_system': 99.5, 'solar_analytics': 1.2, 'power_points_1': 100.8, 'lights_1': 17.6, 'power_points_2': 186.9, 'lights_2': 15.7, 'air_conditioners_2': 16.8, 'fronius_inverter': 1098, 'latronics_inverter': 224.5, 'air_conditioners': 32, 'consumption': 458.5, 'grid_export': 863.9, 'grid_import': 0, 'grid_import_neg': 0, 'lights': 33.3, 'mains_inverted': 863.9, 'power_points': 287.7, 'production': 1322.5, 'self_consumption': 458.5, 'unmeasured': 4.8}

as the sensor value?

I was thinking something like:

{% set foo = {} %}
{% for i in range(0, value_json.labels|length) %}
{% set foo[value_json.labels[i]] = value_json.data[0][i] %}
{% endfor %}
{{ foo }}

But when I test that in the template editor, I get:

Error rendering template: TemplateSyntaxError: expected token 'end of statement block', got '['

Posts: 10

Participants: 4

Read full topic


Viewing all articles
Browse latest Browse all 95324

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>