Creating Sensors From Template Data in Home Assistant

2017-07-18 in Home Automation HomeAssistant

The TP-Link HS110 works really well as a smart outlet. They often are on sale along with the wall switches and I’ve found them to be more reliable than the Wemo equivalents. Home Assistant also supports these easily with the tplink component. The nice thing about the HS110 is that it also reports the power usage going through the plug. I use this specifically to monitor the power usage of my home network racks.

The only bad thing is that it doesn’t expose it directly as a sensor. This isn’t an issue for automations, etc as you can use the templating to extract the value but it is annoying when you just want a simple display of the power usage. To get around this, the template switch component allows parts of another item in home assistant to be extracted and turned into a sensor of its own.

Here is an example using the HS110 to show the current power usage of server racks:

  - platform: template
        entity_id: switch.server_racks
        value_template: "{{ states.switch['server_racks']['attributes']['Current consumption'] | replace( ' W', '') }}"
        friendly_name: "Server Rack Power Usage"
        unit_of_measurement: "W"

Note that I strip off the wattage symbol fromt he value as it is hard coded into the output.

Update: Home Assistant changed the configuration to require that the entity id be specified. The example has been updated above.