Helper sensor to modify sensor attribute value

Hi All.

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:

{{ states['sensor.ENTITY_ID']['attributes'] }}

with the correct entity ID inserted…

1 Like

Thanks for the prompt response - pls see below. Seems result is a string, not an attribute as I previously thought

Copy-paste the following template into the Template Editor and let us know what it reports.

{{ state_attr('sensor.calc_loadpower_day_prediction', 'results') }}

{{ state_attr('sensor.calc_loadpower_day_prediction', 'results') is list }}
1 Like

Thanks for performing the test.

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?

1 Like

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.

1 Like

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)

Do you need all the values with their timestamps, or just the most recent?

Create the following Template Sensor. Let me know if you have never created one before.

- sensor:
  - name: Loadpower Day Prediction
    unique_id: custom_loadpower_day_prediction
    device_class: energy
    state_class: measurement
    unit_of_measurement: kWh
    state: "{{ states('sensor.calc_loadpower_day_prediction') | float(0) | multiply(0.001) | round(3) }}"
    attributes:
      result: >
        {% set x = state_attr('sensor.calc_loadpower_day_prediction', 'results') %}
        {{ dict(zip(x.keys(), x.values() | map('multiply',  0.001))) }}

EDIT

I just noticed that your sensor reports watt-hours so it’s an energy sensor as opposed to a power sensor. I have modified my example accordingly.

1 Like

All the values, please. All of them will follow the same format so maybe regex could work?

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))) }}

BTW, regex isn’t the right tool for this job.

1 Like

It worked - thanks a lot :smiley: appreciate your help on this :slight_smile:

1 Like

yeah, you’re right. Thanks a lot :slight_smile:

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.

1 Like