I have a sensor from an integration which has the following value under attributes that will change from time to time:
Last updated
14 January 2025 at 18:00:00
Results
2024-12-17T18:00:00+0000: 2900.41
2024-12-17T18:30:00+0000: 3122.22
2024-12-17T19:00:00+0000: 3343.41
2024-12-17T19:30:00+0000: 3558.43
2024-12-17T20:00:00+0000: 3765.56
2024-12-17T20:30:00+0000: 3961.39
2024-12-17T21:00:00+0000: 4149.23
2024-12-17T21:30:00+0000: 4327.85
2024-12-17T22:00:00+0000: 4499.66
I want to create a helper sensor which will have the same value as this sensor, except the number following the last colons per line will be divided by 1,000, e.g.,:
Last updated
14 January 2025 at 18:00:00
Results
2024-12-17T18:00:00+0000: 2.90041
2024-12-17T18:30:00+0000: 3.12222
2024-12-17T19:00:00+0000: 3.34341
2024-12-17T19:30:00+0000: 3.55843
2024-12-17T20:00:00+0000: 3.76556
2024-12-17T20:30:00+0000: 3.96139
2024-12-17T21:00:00+0000: 4.14923
2024-12-17T21:30:00+0000: 4.32785
2024-12-17T22:00:00+0000: 4.49966
Could you please help me create such helper sensor? Thanks
Please share a screenshot from Developer Tools / States showing the actual attribute structure. Alternatively, Developer Tools / Template and paste the result of:
The value in the results attribute is a dictionary. Each entry in the dictionary is a 'key-value pair" where the datetime is a key and its associated number is the key’s value.
Although what you have requested is possible, what is the purpose of performing the conversion on all of the dictionary’s values. Where will this modified dictionary be used or displayed?
Thanks for the response. Basically I just want to convert the units from Wh to kWh, ie divide all the figures by 1000. The source integration that created this sensor doesn’t have that capability yet, unfortunately
I believe it may be more practical to perform the conversion at the point of consumption. In other words, wherever you are consuming the values (for example, in a calculation) that’s where the conversion ought to occur.
Unless of course, the sole purpose of this exercise to simply see the converted values while browsing the entity in Developer Tools → States.
Thanks for the suggestion. I will be using this as an input inside another integration which just pulls the sensor name (and hence I cannot perform the division operation needed)
Before you attempt to create the Template Sensor I suggested, copy-paste the following template into the Template Editor (Developer Tools → Template) and confirm the resulting dictionary values are correct.
{% set x = state_attr('sensor.calc_loadpower_day_prediction', 'results') %}
{{ dict(zip(x.keys(), x.values() | map('multiply', 0.001))) }}
FWIW, this is just the second time ever that I found an application for the zip() function. The first time was earlier today, helping another person. Without it, the solution would need a for-loop.