Noob needs help with sensor.yaml code

Hello everyone. I am setting up HA and have some motorized curtains. I want to get the position of them but there isn’t an entity with it specifically. I learned that I can set up a sensor and use this to get the value.

This current_position will tell me where the curtain is at (0=closed 100=opened). I was able to generate this code with help

- platform: template
  sensors:
    name: "Curtain Inside 2 Position"
    state_class: measurement
    state: "{{ state_attr('cover.curtaininside2', 'current_position')|float}}"
    unit_of_measurement: "%"

But I am getting this when I check the configuration

Looking for a kind soul to help point the way to make this work. Thank you in advance.

- platform: template
  sensors:
    curtain_inside_2_position:
      name: "Curtain Inside 2 Position"
      state_class: measurement
      state: "{{ state_attr('cover.curtaininside2', 'current_position')|float}}"
      unit_of_measurement: "%"

That is not correct. The old format uses value_template: not state:, and friendly_name: not name:

You should be using the new format for new template sensors.

You should also define a default value for your float filter.

New format (configuration.yaml), preferred:

template:
  - sensor:
      - name: "Curtain Inside 2 Position"
        state_class: measurement
        state: "{{ state_attr('cover.curtaininside2', 'current_position')|float(0) }}"
        unit_of_measurement: "%"

Old format (sensors.yaml), not preferred:

- platform: template
  sensors:
    curtain_inside_2_position:
      friendly_name: "Curtain Inside 2 Position"
      state_class: measurement
      value_template: "{{ state_attr('cover.curtaininside2', 'current_position')|float(0) }}"
      unit_of_measurement: "%"
1 Like

Thanks for the help. So this is what happens when I put in that code to Studio Code Server

Then checking it shows this
image

Here is what I have in the config file

  template
    - sensor:
      - name: "Curtain Inside 2 Position"
        state_class: measurement
        state: "{{ state_attr('cover.curtaininside2', 'current_position')|float(0) }}"
        unit_of_measurement: "%"

Not sure if I did it correctly. Thanks for the previous help but I need a little more.

That whole thing is indented two spaces too far. template: should be hard up against the margin of your configuration.yaml file. Spacing is important in YAML.

Thanks again. I’ll change that

So here is that same code hard to the left. Does this look correct?

You are missing the colon : after template on the first line.

Oops. I did in my example too, sorry. :flushed:

I edited my post above to correct it.

Thank you. What about the line that says missing property “device_id” do I need that after the name or before the name?

Has all of that not cleared up after you put the colon in?

Close but it wants a platform

You are putting this in your sensors.yaml file. Don’t do that. Template is an integration like Sensor. It needs to go in your configuration.yaml file:

You can set up an include in your configuration.yaml file and use a seperate file if you wish.

template: !include templates.yaml

The config (without the first line “template:”) would then go in the new templates.yaml file you create.

Got it! Again thanks. Here is my config file now


# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include sensors.yaml

# Example configuration.yaml entry
# Example configuration.yaml entry for Google Mail.
notify:
  - name: "Steven"
    platform: smtp
    server: "smtp.gmail.com"
    port: 587
    timeout: 15
    sender: "XXXXXXXX"
    encryption: starttls
    username: "XXXXXX"
    password: "XXXXXX"
    recipient:
      - "[email protected]"
    sender_name: "Chico House Home Assistant"

template:
  - sensor:
    - name: "Curtain Inside 2 Position"
      state_class: measurement
      state: "{{ state_attr('cover.curtaininside2', 'current_position')|float(0) }}"
      unit_of_measurement: "%"

And we are good to go!

1 Like