Way to create multiple sensors per device with template?

Hi everybody,

I am currently trying to set up an overview for all my tasmota outlets with energy monitoring. My current file for this is sensors/power_test.yaml with this content

- platform: mqtt
  state_topic: "home/tasmota/tasmota-8833/tele/SENSOR"
  name: "(A) Rechner Watt"
  value_template: "{{value_json['ENERGY'].Power}}"
  qos: 2
  unit_of_measurement: "W"
  icon: mdi:flash-circle
  availability_topic: "home/tasmota/tasmota-8833/tele/LWT"
  payload_available: "Online"
  payload_not_available: "Offline"

- platform: mqtt
  name: "(A) Rechner Volt"
  state_topic: "home/tasmota/tasmota-8833/tele/SENSOR"
  name: "(A) Rechner Volt"
  value_template: "{{value_json['ENERGY'].Voltage}}"
  qos: 2
  unit_of_measurement: "V"
  icon: mdi:flash-circle
  availability_topic: "home/tasmota/tasmota-8833/tele/LWT"
  payload_available: "Online"
  payload_not_available: "Offline"

I want to add sections for this for each value_json I can use, so in my case =>
(cursive values are available, but I might not need them)

  • ENERGY.Total
  • ENERGY.Yesterday
  • ENERGY.Today
  • ENERGY.Period
  • ENERGY.Power
  • ENERGY.ApparentPower
  • ENERGY.ReactivePower
  • ENERGY.Factor
  • ENERGY.Voltage
  • ENERGY.Current

I can just write these values block by block, that is fine. However, I don’t only have the tasmota-8833 topic (my PC), but also tasmota-xxyy, tasmota-aabb, tasmota-you_get_it, etc.

tl;dr

Can I create a sensor template in which I have something like state_topic: "home/tasmota/tasmota-<VARIABLE>/tele/SENSOR and name: "<VARIABLE> Watt/Voltage/Today/etc", and then specify a list like

  • 8833: (A) Rechner
  • xxyy: (W) TV
  • aabb: (S) TV

So that for each device in this list, home assistant will automatically create each sensor with it’s corresponding name? For example, everything for xxyy would have state_topic: "home/tasmota/tasmota-xxyy/tele/SENSOR and name: (W) TV Watt, name: (W) TV Voltage, etc.?

I could just manually write a template, then use awk to create each .yaml file for this as well… But that would mean I’d have to repeat this for each newly added device as well. If there is a way to “just” tell hass “this is my array of device numbers and their corresponding names, please create a set of sensors from that template file for each one of them”, it would make it much easier to maintain.

Sorry if this is a naive question. I am fairly new to home assistant and the fact that one needs to restart it after changing the configuration.yaml (or any file that been !includeed) plus me always messing up the yaml syntax is making this a bit hard to “just try”.

Btw., same goes for a lot of switches as well as just on/off states that I work with. Currently I control them via node-red, which allows me to use a template node and just change part of the mqtt topic for each device. They all work with the same payloads, telemetry data, etc. etc. anyway, so I thought maybe there might be a way to save the time writing awk scripts or copy/pasting a lot of similar data and automate the process.

Thanks in advance for your help :slight_smile:

You may wish to use YAML Aliases and Anchors.

You can define a common block of YAML code once and then each sensor refers to it.

It’s not the ‘sensor template’ you requested but it can help to reduce the size of your YAML file and make it easier to manage common blocks of code.

https://confluence.atlassian.com/bitbucket/yaml-anchors-960154027.html

Thank you. I have tried multiple ways to do this (my last attempt below), but it does not work. hass states I have a valid configuration, but the sensors do not show up

platform: mqtt
name: &name
state_topic: &state_topic
availability_topic: &availability_topic
platform: mqtt
value_template: "{{value_json['ENERGY'].Power}}"
qos: 2
unit_of_measurement: "W"
icon: mdi:flash-circle
payload_available: "Online"
payload_not_available: "Offline"


rechner:
- name: "Rechner Watt"
- state_topic: "home/tasmota/tasmota-1834/tele/SENSOR"
- availability_topic: "home/tasmota/tasmota-1834/tele/LWT"

spuel:
- name: "Spuele Watt"
- state:topic: "home/tasmota/tasmota-4828/tele/SENSOR"
- availability_topic: "home/tasmota/tasmota-4828/tele/LWT"

I had also tried *name instead of &name and <<: *name: "Rechner Watt instead of the way above (and any variation of those I could think of), but it just does not work. Could you please give me an example how this should work? I am sure your link has all information in there, but I could not figure it out…

Here are several examples of how to use it with Home Assistant:

Miscellaneous tricks

For this one, scroll down to Node Anchors:
YAML for Nonprogrammers

Used with Lovelace configuration:


EDIT
Fixed broken link.

Thank you.

I really don’t get it. I have tried different versions of those templates multiple times (especially the “Reducing tasmota (or any) entity config yaml” one), but it just does not work for me.

HASS will tell me that I have a valid configuration at times, but not display anything it should; other times I won’t have a valid configuration, even when I copy&pasted that example code from the tasmota thread exactly as is (which should work if it worked for the person posting it, right?).

Would anybody be willing to get me started by giving me a minimal example on how this should look in my actual use case so that I can improve on that?

changing values

device 1 watt

name: "(A) Rechner Watt"
state_topic: "home/tasmota/tasmota-1834/tele/SENSOR"
availability_topic: "home/tasmota/tasmota-1834/tele/LWT"
value_template: "{{value_json['ENERGY'].Power}}" 
unit_of_measurement_ "W"

device 1 voltage

name: "(A) Rechner Voltage"
state_topic: "home/tasmota/tasmota-1834/tele/SENSOR"
availability_topic: "home/tasmota/tasmota-1834/tele/LWT"
value_template: "{{value_json['ENERGY'].Voltage}}"
unit_of_measurement_ "V"

device 2 watt

name: "(K) Spuele Watt"
state_topic: "home/tasmota/tasmota-8483/tele/SENSOR"
availability_topic: "home/tasmota/tasmota-8483/tele/LWT"
value_template: "{{value_json['ENERGY'].Power}}"
unit_of_measurement: "W"

device 2 voltage

name: "(K) Spuele Voltage"
state_topic: "home/tasmota/tasmota-8483/tele/SENSOR"
availability_topic: "home/tasmota/tasmota-8483/tele/LWT"
value_template: "{{value_json['ENERGY'].Voltage}}"
unit_of_measurement_ "W"

static values

- platform: mqtt
  qos: 2
  icon: mdi:flash-circle
  payload_on: "ON"
  payload_off: "OFF"
  payload_available: "Online"
  payload_not_available: "Offline"
  retain: false

I am not sure if I should rather have the value_template in the actual template as well and then set multiple templates (one for voltage, one for watt, one for yesterday’s consumption, etc.), or if I should keep the main template smaller and create the variables the way I currently have them.

Also I am trying to keep my configuration.yaml as tiny as possible, so I source sensors via sensor: !include_dir_merge_list script and keep all sensors in hass-root/sensor/, currently one .yaml file per room. I guess if I can get this template thing to work I’ll sort by type (power consumption meters in one file, so that the template will work, rather than one file per room).

I tried minimalizing my code and got this

platform: mqtt
unit_of_measurement: "MyUnit"

<<: 
name: "Spüle"
state_topic: "test/okay"

This will be accepted by hass and will actually create a new sensor called Spüle that will display whatever I send to test/okay via mqtt.

But when I enhance my code to

platform: mqtt
unit_of_measurement: "MyUnit"

<<: 
name: "Spüle"
state_topic: "test/okay"

<<:
name: "Heizung"
state_topic: "test/somethingelse"

I get

Error loading /config/configuration.yaml: while constructing a mapping
  in "/config/sensor.yaml", line 1, column 1
expected a mapping or list of mappings for merging, but found scalar
  in "/config/sensor.yaml", line 4, column 4

I also tried something like

template: &template
platform: mqtt
unit_of_measurement: "MyUnit"

<: *template
name: "Spüle"
state_topic: "test/okay"

<: *template
name: "Heizung"
state_topic: "test/somethingelse"

which will give me

Invalid config for [sensor.mqtt]: [template] is an invalid option for [sensor.mqtt]. Check: sensor.mqtt->template. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.mqtt/

I am trying to create one single template I can use for, i.e., power measurement and then feed this template multiple and different names, state_topics, unit_of_measurements, etc. etc. but no matter what I try, it will not work. It seems like to use &something, I need something that is accepted as a value for mqtt sensor, but when I do, it still doesn’t work.

How do I feed my template multiple keys and values to automatically generate similar sensors/switches where only, for example, the state_topic changes?

Here’s how I use it. The following is a portion of my lights.yaml file.

The configuration of the first light includes the definition of a node anchor called dimmer. All subsequent light configurations refer to the dimmer anchor.

  - name: "Foyer"
    state_topic: "premise/foyerlight/powerstate"
    command_topic: "premise/command/foyerlight/powerstate"
    brightness_state_topic: "premise/foyerlight/brightness"
    brightness_command_topic: "premise/command/foyerlight/brightness"
    <<: &dimmer
      platform: mqtt
      payload_on: "1" 
      payload_off: "0"
      brightness_scale: 100
      on_command_type: 'brightness'

  - name: "Mantle"
    state_topic: "premise/mantlelight/powerstate"
    command_topic: "premise/command/mantlelight/powerstate"
    brightness_state_topic: "premise/mantlelight/brightness"
    brightness_command_topic: "premise/command/mantlelight/brightness"
    <<: *dimmer

  - name: "Family"
    state_topic: "premise/ceilinglight/powerstate"
    command_topic: "premise/command/ceilinglight/powerstate"
    brightness_state_topic: "premise/ceilinglight/brightness"
    brightness_command_topic: "premise/command/ceilinglight/brightness"
    <<: *dimmer
5 Likes

Oh my god, thank you so much! I understand how it works now. I have been reading a lot about this and watched tutorials, but it was never explained well enough for me to get it. Now I can finally set up all my sensors with templates. Thank you. :pray:

After a lot of RTFM and posts I finally landed to this post, just before giving up.
It really helped me writing my first successful anchors and aliases, so I think it may be useful to others to bump it again.

Thank you @123 for this very useful recap.

[EDIT Feb 20, 2020] Corrected thanks to @123

Piero

I find this, I spend so much time reading, youtubing googling etc. but as soon as someone shows me. Boom, got it.

I’ll assume your thanks was directed at me (123) rather than someone named Taras.pastukh … you’re welcome!

You may have noticed that I defined <<: &dimmer within the configuration of the first light. Ideally, one would be able to define it separately (i.e. not within the first light configuration but standalone). It’s possible and the following post by petro shows how to do it but the limitation is that the anchor is only valid within the file where it is defined (and won’t work with packages and includes).