I found this thread and used the template proposed by @petro but I had to modify it to make it work
I think it could be useful for others also.
I had to fix some syntax otherwise sensors wouldn’t show, I also added the right device class for power and voltage sensors, as well as some rounding
sensor:
- platform: template
sensors:
{%- for i in range(states.sensor.iotawatt.attributes.inputs | length) %}
{%- set input = states.sensor.iotawatt.attributes.inputs[i] %}
{%- if input.Watts %}
iotawatt_input_{{ input.channel }}:
value_template: {{'\'{{{{ states.sensor.iotawatt.attributes.inputs[{}].Watts }}}}\''.format(i)}}
unit_of_measurement: Watts
{%- endif %}
{%- endfor %}
{%- for i in range(states.sensor.iotawatt.attributes.outputs | length) %}
{%- set output = states.sensor.iotawatt.attributes.outputs[i] %}
iotawatt_output_{{ output.name }}:
value_template: {{'\'{{{{ states.sensor.iotawatt.attributes.outputs[{}].value }}}}\''.format(i)}}
unit_of_measurement: {{ output.units }}
{%- if output.units == 'Watts' %}
device_class: power
{%- elif output.units == 'Volts' %}
device_class: voltage
{%- endif %}
{%- set output = states.sensor.iotawatt.attributes.outputs[i] %}
iotawatt_output_{{ output.name }}_rounded:
value_template: {{'\'{{{{ states.sensor.iotawatt.attributes.outputs[{}].value | round(0) }}}}\''.format(i)}}
unit_of_measurement: {{ output.units }}
{%- if output.units == 'Watts' %}
device_class: power
{%- elif output.units == 'Volts' %}
device_class: voltage
{%- endif %}
{%- endfor %}
the template above creates 3 types of sensors:
- inputs
- outputs
- outputs rounded
the rounded ones are useful for immediate data visualization, so that annoying decimals don’t show up in graphs and gauges