Adding iotawatt problem

I’ve found tons of stuff about adding iotawatt, but I’m missing a step somewhere. I found the thread on a jinja template to pull in all the sensors associated with it but it isn’t working for me.

Here is the jinja template from the other thread. The error I am getting is "Error rendering template: UndefinedError: ‘None’ has no attribute ‘attributes’

I have already added the rest sensor iotawatt into my configuration.yaml and rebooted and HA can see the iotawatt. If I check states in dev tools, I can even see all my outputs and inputs there. The template just doesn’t run and I can’t see my inputs and outputs outside of dev tools.

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["}}{{i}}{{"].Watts}}"}}
        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["}}{{i}}{{"].value}}"}}
        unit_of_measurement: {{ output.units }}
{%- endfor %}

Start by reading the templating docs and the template sensor docs. The formatting in yours is more than slightly wrong :wink:

You can’t template the generation of template sensors…

Tinkerer… thanks for reply, but I’m not sure what you are meaning it is incorrect.

Here is a youtube video (skip to 43:33 in the video) about iotawatt/HA integration showing this exact piece of code being pasted into the exact place that I am pasting it, but yet mine fails. I found the exact thread that he pastes the code from. Batch create multiple template sensors.

This would lead me to believe I have missed a needed step before or there is a version mismatch, not that batch creation of these is impossible. Unless I am fundamentally misunderstanding something, which means I need more help that just a doc link, because I’ve been reading on this for 4 days now, but I am admittedly very green to this.

I’m not sure what you are meaning it is incorrect.

He told you:

You can’t template the generation of template sensors.

You can template some of the sensor options (value_template for example) but not the whole sensor, entity_id, name, and everything.

In short, create a sensor for each channel you want to monitor.

Tom, please watch the video and see the other thread. I’m not arguing that MINE isn’t working, but you and the first response seem to be saying it isn’t possible. That video shows otherwise, again unless I’m fundamentally misunderstanding what I am seeing. If that is the case I need help to understand that what you guys are saying is impossible is something different than what is shown in the video.

Where are you putting that sensor template?

I’m putting it Dev tools/Template/Template editor just like in the video.

I edited my last post to make what I am asking for more clear and point out that I’m not arguing for the sake of arguing. I am admitting that i may be misunderstanding what I’m seeing

Yeah ok I understand now.

Have you created DotNetDann’s rest sensor the video talks about at 42:00? That is where the attributes for the template you use to generate the sensors comes from. This:

sensor:
  - platform: rest
    name: IoTaWatt
    json_attributes:
      - inputs
      - outputs
    resource: http://192.168.xxx.xxx/status?inputs=yes&outputs=yes
    value_template: '{{ value_json.inputs[0].Vrms }}v'
    scan_interval: 5

From here: https://github.com/DotNetDann/IoTaWatt/blob/c1fa9f1ca69b62c7bc21209e697cc85348edc25a/Example%20Home%20Assistant%20Configuration.yaml

Yes. Originally when I tried it, I had not and I got a slightly different error than the one I posted. I did some digging before starting this thread and found that the reason I was getting PART of the error was stuff wasn’t pre-existing. So I added the chunk of code that you just posted to my configuration.yaml and got it to the point it is throwing the error that I now have and put in my original post.

Apologies, I just re-read you original post and noticed you did mention creating the restful sensor.

In the template editor what is the result of pasting this:

{{ states.sensor.iotawatt.attributes }}

Nothing. No error like the other code though. Just a blank output.

Edit: DUH… I rolled my config back last night because I screwed everything up with a zwave thermostat. Putting rest sensor back in now… will reboot and try that again. Be back in a few.

1 Like

Well, your help and my rollback from last night showed me the ultimate cause of what was wrong and this tells you how green I am to home assistant…

When I had the rest sensor in there prior to my making this thread, it was the only sensor that I had other than a darksky weather sensor. My weather sensor kept dropping out and I thought that it was due to my HA instance losing internet connectivity. It wasn’t! It was due to me playing with the rest sensor.

Here is my initial stupidity/misconception. Every code snippet that I have seen always has sensor: or climate: etc in front of it. So every time I have added a new thing of that type I have added sensor: in front. When I only had my iotawatt and darkysky, everytime I dropped my iotawatt sensor in the configuration file it was killing my darksky sensor.

SOOOO… just now when I added my rest sensor back in, a WHOLE BUNCH of newly added items dropped off.

My whole issue was including sensor: in in multiple places. I couldn’t find it initially because I only had a few items in there and I was confused when it lost connectivity.

Long story short. Your test code now works as well as the initial piece that I pasted in. I now have EVERYTHING in HA.

THANK YOU. All I needed was a little push and some prodding with my severe green-ness! And thank you for taking time to watch the video instead of just saying it was impossible!!!

1 Like

I was a little too quick to think it was done. I assumed since the template built that the code would be perfect and the code contained all my sensors. But I’m getting an error in the yaml when I put the rest sensor together with the template into my configuration.yaml

Here is the whole thing. The error that I get when I run homeassistant check from command line is:
Successful config (partial)
sensor.template

But comparing output to other people, I can’t find anything wrong. It initially complained about missing single quotes. The jinja template generated the code without the single quote around the {{ and }}, but I found the issue and got past that one. But now I’m stuck on this.

  - platform: rest
    name: iotawatt
    json_attributes:
      - inputs
      - outputs
    resource: http://192.168.1.247/status?inputs=yes&outputs=yes
    value_template: '{{ value_json.inputs[0].Vrms }}v'
    scan_interval: 5
  - platform: template
    sensors:
      iotawatt_input_1:
        value_template: '{{states.sensor.iotawatt.attributes.inputs[1].Watts}}'
        unit_of_measurement: Watts
      iotawatt_input_2:
        value_template: '{{states.sensor.iotawatt.attributes.inputs[2].Watts}}'
        unit_of_measurement: Watts
      iotawatt_input_3:
        value_template: '{{states.sensor.iotawatt.attributes.inputs[3].Watts}}'
        unit_of_measurement: Watts
      iotawatt_input_4:
        value_template: '{{states.sensor.iotawatt.attributes.inputs[4].Watts}}'
        unit_of_measurement: Watts
      iotawatt_input_5:
        value_template: '{{states.sensor.iotawatt.attributes.inputs[5].Watts}}'
        unit_of_measurement: Watts
      iotawatt_input_6:
        value_template: '{{states.sensor.iotawatt.attributes.inputs[6].Watts}}'
        unit_of_measurement: Watts
      iotawatt_input_7:
        value_template: '{{states.sensor.iotawatt.attributes.inputs[7].Watts}}'
        unit_of_measurement: Watts
      iotawatt_input_8:
        value_template: '{{states.sensor.iotawatt.attributes.inputs[8].Watts}}'
        unit_of_measurement: Watts
      iotawatt_input_9:
        value_template: '{{states.sensor.iotawatt.attributes.inputs[9].Watts}}'
        unit_of_measurement: Watts
      iotawatt_input_10:
        value_template: '{{states.sensor.iotawatt.attributes.inputs[10].Watts}}'
        unit_of_measurement: Watts
      iotawatt_input_11:
        value_template: '{{states.sensor.iotawatt.attributes.inputs[11].Watts}}'
        unit_of_measurement: Watts
      iotawatt_input_12:
        value_template: '{{states.sensor.iotawatt.attributes.inputs[12].Watts}}'
        unit_of_measurement: Watts
      iotawatt_input_13:
        value_template: '{{states.sensor.iotawatt.attributes.inputs[13].Watts}}'
        unit_of_measurement: Watts
      iotawatt_input_14:
        value_template: '{{states.sensor.iotawatt.attributes.inputs[14].Watts}}'
        unit_of_measurement: Watts
      iotawatt_output_aTotalUsage:
        value_template: '{{states.sensor.iotawatt.attributes.outputs[0].value}}'
        unit_of_measurement: Watts
      iotawatt_output_bEnviroTotal:
        value_template: '{{states.sensor.iotawatt.attributes.outputs[1].value}}'
        unit_of_measurement: Watts
      iotawatt_output_cWaterHeaterTot:
        value_template: '{{states.sensor.iotawatt.attributes.outputs[2].value}}'
        unit_of_measurement: Watts
      iotawatt_output_dDryer:
        value_template: '{{states.sensor.iotawatt.attributes.outputs[3].value}}'
        unit_of_measurement: Watts
      iotawatt_output_eDishwasher:
        value_template: '{{states.sensor.iotawatt.attributes.outputs[4].value}}'
        unit_of_measurement: Watts
      iotawatt_output_fStove:
        value_template: '{{states.sensor.iotawatt.attributes.outputs[5].value}}'
        unit_of_measurement: Watts
      iotawatt_output_gFreezer:
        value_template: '{{states.sensor.iotawatt.attributes.outputs[6].value}}'
        unit_of_measurement: Watts
      iotawatt_output_hComputerDesk:
        value_template: '{{states.sensor.iotawatt.attributes.outputs[7].value}}'
        unit_of_measurement: Watts
      iotawatt_output_iUnderHouse:
        value_template: '{{states.sensor.iotawatt.attributes.outputs[8].value}}'
        unit_of_measurement: Watts
      iotawatt_output_jRefrigerator:
        value_template: '{{states.sensor.iotawatt.attributes.outputs[9].value}}'
        unit_of_measurement: Watts
      iotawatt_output_kLivingRoom:
        value_template: '{{states.sensor.iotawatt.attributes.outputs[10].value}}'
        unit_of_measurement: Watts
      iotawatt_output_lKitchenCounter:
        value_template: '{{states.sensor.iotawatt.attributes.outputs[11].value}}'
        unit_of_measurement: Watts
      iotawatt_output_zUnMonitored:
        value_template: '{{states.sensor.iotawatt.attributes.outputs[12].value}}'
        unit_of_measurement: Watts

What are you stuck on? It looks OK.

At the end of that last post where I put in my whole bit of code, it is not working. I keep a constant error about a sensor. The error gives no line number and I see nothing that looks out of place like a type. But yet it is still complaining about an error with a sensor in configuration.yaml.

I am using the following template to obtain data from my iotawatt, it might help you with your template(s).

    iotawatt_freq:
      friendly_name: IotaWatt Frequency
      entity_id: sensor.iotawatt
      unit_of_measurement: Hz
      availability_template: >-
        {% if states('sensor.iotawatt') == "unknown" %}
          false
        {% else %}
          true
        {% endif %}
      value_template: >-
        {% if states('sensor.iotawatt') == "unknown" %}
          0
        {% else %}
          {{ states.sensor.iotawatt.attributes["stats"]["frequency"]|float|round(2) }}
        {% endif %}

I would recommend running your own instance of emoncms, and integrating both iotawatt and HA to that.

Its just easier all round :slight_smile:

I couldn’t let this go so I’ve been fiddling with influxdb and creating sensors from my iotawatt export.

I’ve set up my iotawatt to publish to influxDB with the following tags:

device	$device	(uniquely identifies this device)
entity_id	$name	
unit	$units

Then I read latest value in HomeAssistant as a rest sensor:

sensor:
  - platform: rest
    name: IoTaWatt
    resource: 'http://192.168.1.2:8086/query?db=iotawatt&q=SELECT%20last(value)%20FROM%20Watts%20WHERE%20time%20>%20NOW()-10m%20GROUP%20BY%20entity_id'
    value_template: 'OK'
    json_attributes:
      - results

This creates a sensor with all the states in the results attribute. To check that everything worked go to http://hassio.local/developer-tools/template and paste:

{% for row in state_attr("sensor.iotawatt", "results")[0]["series"] -%}
{{ row["tags"]["entity_id"] }} {{ row["values"][0][1] | float }}
{% endfor -%}

In my setup it generates this:

Bathroom 231.41
Bedroom_1 0.0
Bedroom_2 0.01
Dishwasher 0.0
Fridge 23.23
Inverter 328.49
Kitchen 66.41
Living_room 138.22
Total 838.77
VVB 0.0
Washer 51.01

To generate yaml configuration paste the code below in the template editor:

 sensor:
  - platform: template
    sensors:
{%- for row in state_attr("sensor.iotawatt", "results")[0]["series"] %}
      iotawatt_input_{{ row['tags']['entity_id'].lower() }}:
        value_template: '
{{- '{% for row in state_attr("sensor.iotawatt", "results")[0]["series"] -%}' }} 
{{- '{{% if row["tags"]["entity_id"] == "{}" -%}}'.format(row['tags']['entity_id']) }}
{{- '{{ row["values"][0][1] | float }}{% endif -%}{% endfor -%}' }}'
        unit_of_measurement: W
        device_class: power
        friendly_name_template: '{{ row["tags"]["entity_id"].replace("_", " ") }}'
{%- endfor %}

In my setup I get this:

sensor:
  - platform: template
    sensors:
      iotawatt_input_bathroom:
        value_template: '{% for row in state_attr("sensor.iotawatt", "results")[0]["series"] -%}{% if row["tags"]["entity_id"] == "Bathroom" -%}{{ row["values"][0][1] | float }}{% endif -%}{% endfor -%}'
        unit_of_measurement: W
        device_class: power
        friendly_name: 'Bathroom'
      iotawatt_input_bedroom_1:
        value_template: '{% for row in state_attr("sensor.iotawatt", "results")[0]["series"] -%}{% if row["tags"]["entity_id"] == "Bedroom_1" -%}{{ row["values"][0][1] | float }}{% endif -%}{% endfor -%}'
        unit_of_measurement: W
        device_class: power
        friendly_name: 'Bedroom 1'

The for row in .. is there as I don’t trust InfluxDB to always return the same order.

Good luck! ::slight_smile:

Fredrik/all,

Wondering if you can help, I just purchased, installed, and hooked up an IoTaWatt box to Influx. I’m also using Influx to store my long-term Home Assistant data. I tried following your instructions but have hit a wall.

I’ve setup the same tags as you in IoTaWatt.

But then when trying to pull data from Influx using the rest code it returned no results:

http://192.168.1.7:8086/query?db=IotaWatt&q=SELECT%20last(value)%20FROM%20Watts%20WHERE%20time%20%3E%20NOW()-10m%20GROUP%20BY%20entity_id

returns

{"results":[{"statement_id":0}]}

However, if I modify the rest call to this I do get results:

http://192.168.1.7:8086/query?db=IoTaWatt&q=SELECT%20last(%22Watts%22)%20FROM%20%22IotaWatt%22.%22autogen%22.%22iotawatt%22%20WHERE%20time%20%3E%20NOW()-10m%20GROUP%20BY%20entity_id

producing:

{"results":[{"statement_id":0,"series":[{"name":"iotawatt","tags":{"entity_id":"ac"},"columns":["time","last"],"values":[["2020-08-14T11:05:40Z",1740.58]]},{"name":"iotawatt","tags":{"entity_id":"consumption"},"columns":["time","last"],"values":[["2020-08-14T11:05:40Z",2552.91]]},{"name":"iotawatt","tags":{"entity_id":"front_power"},"columns":["time","last"],"values":[["2020-08-14T11:05:40Z",339.12]]},{"name":"iotawatt","tags":{"entity_id":"hob"},"columns":["time","last"],"values":[["2020-08-14T11:05:40Z",0]]},{"name":"iotawatt","tags":{"entity_id":"hotwater"},"columns":["time","last"],"values":[["2020-08-14T11:05:40Z",25.66]]},{"name":"iotawatt","tags":{"entity_id":"house_lights"},"columns":["time","last"],"values":[["2020-08-14T11:05:40Z",276.69]]},{"name":"iotawatt","tags":{"entity_id":"lights"},"columns":["time","last"],"values":[["2020-08-14T11:05:40Z",82.64]]},{"name":"iotawatt","tags":{"entity_id":"mains"},"columns":["time","last"],"values":[["2020-08-14T11:05:40Z",2552]]},{"name":"iotawatt","tags":{"entity_id":"oven"},"columns":["time","last"],"values":[["2020-08-14T11:05:40Z",0.01]]},{"name":"iotawatt","tags":{"entity_id":"pool"},"columns":["time","last"],"values":[["2020-08-14T11:05:40Z",0.7]]},{"name":"iotawatt","tags":{"entity_id":"solar"},"columns":["time","last"],"values":[["2020-08-14T11:05:40Z",0.91]]}]}]}

I then created the sensor.iotawatt using the code abave, the senor is created but on checking the attributes on developer-tools/template I get the following:

Error rendering template: UndefinedError: None has no element 0

Can you spot what silly mistake I’ve made?

Thanks,
Eoin

Can you show the output from:

{{state_attr("sensor.iotawatt", "results")}} 

And if that works:

{{ state_attr("sensor.iotawatt", "results")[0]["series"] }}