Getting a template sensor to work

Hello Experts,

To begin, I’m new to HA - attempting to move over from OpenHAB. In my current config, I have a ‘switch’ entity that has a (power consumption) value associated with it. I want to extract the power consumption value and make it a ‘sensor’ entity so that I can graph it. To this end I have setup the following configuration in “sensor.yaml”, after having checked it in the Template Editor:

sensor:
  - platform: template
    sensors:
      american_fridge_power:
        friendly_name: "American Fridge Power"
        unit_of_measurement: 'Watt'
        value_template: "{{ states.switch.americanfridge.attributes.current_consumption }}"

Sensor.yaml is hooked into configuration.yaml with an include. The Template Editor gives the following result:

sensor:
  - platform: template
    sensors:
      american_fridge_power:
        friendly_name: "American Fridge Power"
        unit_of_measurement: 'Watt'
        value_template: "90.1"

The problem is that I have no evidence that this configuration results in a sensor being created. I don’t see a sensor called ‘american_fridge_power’ appear anywhere ie not in the ‘Entities’ list. But neither do I have any evidence that the sensor is not being created - ie there’s nothing in the log file to indicate a problem.

So what am I doing wrong? Have I missed a step out somewhere? Or has the sensor been created and I’ve not yet found it.

Many thanks.

Remove the first line that says sensor: from your file and adjust the indentation

- platform: template
  sensors:
    american_fridge_power:
      friendly_name: "American Fridge Power"
      unit_of_measurement: 'Watt'
      value_template: "{{ state_attr('switch.americanfridge', 'current_consumption' }}"

Also use the format shown above to extract the attribute. It will not generate errors while the sensor is initialising after a restart.

One other bit of advice. If that switch is really on a fridge you might want to search out the custom restriction-card to prevent it being turned off accidentally.

1 Like

If you look at Tom’s code you will see that there is a sensor: (implicit as you had to create an include in your config to point to this file) There can only be one sensor: heading (unless you use packages, but you are not anywhere near using those yet)
And under that you have the - platform: template (again only one of those)
And under that you have a sensors: heading (again only one of those)
Then you list your sensors, your first being : - american_fridge_power:
So in effect you have created a sensor under sensor,template,sensors,american_fridge_power it needs to be unique and implicit in that is the uniqueness of it’s path address (again ignore packages for the moment)

Guys,

Many thanks. I’ve now got this:

### Sensor.yaml

### Plugwise config to log power use

- platform: template
  sensors:
    american_fridge_power:
      friendly_name: "American Fridge Power"
      unit_of_measurement: 'Watt'
      value_template: "{{ state_attr('switch.americanfridge', 'current_consumption') }}"
        

But still no joy - I think. I expect to be able to find a Sensor entity with the name ‘american_fridge_power’. But there’s still no such entity created. Note that the Switch entity is present.

Regards, Robert.

Good point. But I’m a long way from such considerations at the moment. Still can’t get HA to read all my plugwise circles - only 10 from 18 and then unreliably. Also can’t get it to read my SMA inverter. All these things were working in OpenHAB so I assume that I must be able to reach a similar configuration with HA.

Robert

Did you do a configuration check before restarting? - turn Advanced mode on in your profile if you do not see this on the configuration / server controls page.

Did you restart after making the changes?

Are there any errors in Developer Tools / logs?

Are you looking in developer tools / states?

… (again a follow up to Tom’s)

  1. You have restarted haven’t you ? Tom asked this already
  2. Playing with the template editor is very quick to test different outcomes
  3. Are you sure you have these entities you are playing with ?

Hi Tom,

Yes. I always run a config check before restarting - saves a lot of time. :roll_eyes:

Yes. I restarted a couple of times. I also forced an error in the sensors.yaml file to makes sure that it was being read.

Yes. I checked the config with the Template Editor. Result below:

### Sensor.yaml

### Plugwise config to log power use

- platform: template
  sensors:
    american_fridge_power:
      friendly_name: "American Fridge Power"
      unit_of_measurement: 'Watt'
      value_template: "90.1"

The fact that there’s a value for the “value_template” would indicate that at least the source entity is present and possesses a current value.

Yes, there are errors in the home-assistant.log file. Most have to do with the plugwise circles that is cannot read at the moment. This is a problem I was postponing till later. The ‘American Fridge’ circle is working and showing a value. There are two lines in the log that might be relevant:

2020-04-11 11:32:10 WARNING (SyncWorker_0) [homeassistant.util.yaml.loader] YAML file /config/configuration.yaml contains duplicate key "sensor". Check lines 16 and 66.
2020-04-11 11:32:10 WARNING (SyncWorker_0) [homeassistant.util.yaml.loader] YAML file /config/configuration.yaml contains duplicate key "group". Check lines 12 and 71.

Yes. I checked the states in the Developer Tools:

Clearly I’m missing something. But as yet I don’t have a clue.

Robert

Those errors are relevant. The problem is that you have both sensor: and group: defined more than once in your configuration file. These are preventing your config from loading completely.

Fixing these issues will probably solve all the other problems you are having.

Ok. I’ll try sorting that out…

Tnx.

YES!. That fixed it. Teach me not to ignore a warning message.

Thanks for your help. :blush:

Hmmm !

:thinking:

Then why is not an Error instead of a warning? Errors I take seriously, warnings less so. If a double sensor entry prevents something from working then it’s an error. If it’s merely a sub-optimal configuration that still works, then a warning is suitable.

I dunno, maybe you should take it up with the guy who wrote the config checker, and who keeps it up to date with the ongoing changes in HA as it develops through its beta releases.
I think he does a pretty damn good job as it is and you were warned specifically.

Your config was still loading, just not all of it. That’s sub optimal in my books. If it prevented your config from loading at all then it would be an error.