Why does this simple template not work?

I’m a total noob when it comes down to templates and already got stuck at the first go.

I read a value via the rest platform. This correctly resturn a ‘value’ (I can see this nicely in the interface). As HAss does not know what it is,m it does not track historic value etc. So I want to use a template to show actual Watts (numeric value).

I have below code. In the log I find: homeassistant.components.sensor.template: UndefinedError: ‘None’ has no attribute ‘state’.

I’m sure its something stupid!

sensor p1elec:
  platform: rest
  resource: http://192.168.1.5:3480/data_request?id=variableget&DeviceNum=266&serviceId=urn:micasaverde-com:serviceId:EnergyMetering1&Variable=Watts
  method: GET
  name: Watts

sensor kWtemplate:
  - platform: template
    sensors:
      energy_meter:
        value_template: "{{ states.sensor.p1elec.state | round(1) }}"
        friendly_name: Energy Meter
        unit_of_measurement: 'kW'

@Tyfoon

Does HA pull in the Rest sensor?

If so cool .

To get kW from Watts you need to multiply… | multiply(0.001) }}

I believe a lot of math functions are unavailable in HA templates.

Hope this helps!

@PtP I originally use your template (with the multiply(0.001)) but this yields the same result.

The REST is pulling fine (and displays nicely in the interface) but the template does not work (just shows a - instead of a number).

So I think the REST pulls a text string (therefore also not logging the value to make a graph) which I need converted into a number. As such I only need a 1:1 conversion to number as I want to show Watts and not Kw (like you do)

It will take almost 24 hours for the graph to update or be created.
I will look at your code a bit more.

I also had to make changes to the sensor in vera.

Should have added a screenshot: Right is the REST output , Left the templated (with above code)

I think the problem is in your formatting and names.

Layout all your sensors in your yaml like this

    sensor:
    - platform: rest
      code...
    - platform: template
      code...

Do this for all of your “sensors”

What I mean is don’t use: sensor p1elec: If you want to do something for your reference use comments above each platform.

Bingo! Also made the effort to split all sensors out into a seperate YAML. Had to do that anyway. All reporting fine now! Thanks for your help

Nice!! Like I said it will take a while for your graphs to change and kW to show up in the History but they will.

I meant to dig in and see what it would take to make the change happen faster but I have not had not had a chance.