Template (round 2)

Hi,

I am trying to round to 1 or 2 decimals in a template.

I have the following:

  • platform: template
    sensors:
    afgelopenjaareuro:
    value_template: ‘{{ (states.sensor.solaredge_overview.attributes.overview.lastYearData.energy
    |float * 0.22|float / 1000|round(2))}}’
    entity_id: sensor.solaredge_lastyearenergy2
    unit_of_measurement: ‘€’

116/5000

what it does is convert to kwh and then times 0.22 (kwh price) and then round up to 2 figures behind the comma.

But it gives me : €200,12353434

Hope you can help me…

Give this a try:

value_template: ‘{{ (states.sensor.solaredge_overview.attributes.overview.lastYearData.energy * 0.00022|float)|round(2) }}’
1 Like

Thanks ! Almost, iam getting ‘200.xx’ (between quotes)

That’s because you did not format your original post correctly and the quotes were ‘fancy’ single quotes and @Holdestmade copied them. Also you need to cast the attribute as floating point before performing the multiplication. Also is your attribute really overview.lastYearData.energy ?
I’ve used it but you might want to check that.

Try this:

value_template: '{{ ( states.sensor.solaredge_overview.attributes.overview.lastYearData.energy | float * 0.00022 ) | round(2) }}'

Or better yet to avoid errors if the attribute is undefined:

value_template: "{{ ( state_attr('sensor.solaredge_overview', 'overview.lastYearData.energy') | float * 0.00022 ) | round(2) }}"
1 Like

Thanks ! this is the solution:

value_template: '{{ ( states.sensor.solaredge_overview.attributes.overview.lastYearData.energy | float * 0.00022 ) | round(2) }}'

This one gives nothing (€0.00)

value_template: "{{ ( state_attr('sensor.solaredge_overview', 'overview.lastYearData.energy') | float * 0.00022 ) | round(2) }}"

states.sensor.entity.attributes.attribute is identical to state_attr('sensor.entity', 'attribute') except the second version will return the value of the attribute or None if it doesn’t exist. If it’s not working it might be because your attribute is badly named. It should not contain capitals or decimal points.

1 Like

Can you paste a screenshot of sensor.solaredge_overview as it appears in the States page? I’m curious to see how it displays an attribute like overview.lastYeardata.energy.

1 Like

That’s not sensor.solaredge_overview