Help with ESPHome YAML Template

Been struggling with what should be something real simple here. I get an error when validating the ESP Home YAML pasted below. However, YAML checker shows says my syntax and indentations are valid. Is the issue with my use of platform:template? What am I doing wrong?

sensor:

 - platform: hx711
   name: "Propane Tank Weight"
   id: propane_weight
   dout_pin: D2
   clk_pin: D1
   filters:
      - calibrate_linear:
          - 364518 -> 0
          - 442950 -> 11.4
      - sliding_window_moving_average:
          window_size: 5
          send_every: 5
   update_interval: 2s
   unit_of_measurement: kg
   accuracy_decimals: 2

 - platform: template
   name: "Propane Weight Percentage"
   id: propane_percent
   value_template: "{{ ((states('sensor.propane_scale_propane_tank_weight') | float - 1.59)/0.2914) | round(2) }}"
   unit_of_measurement: "%"
   icon_template: "mdi:weight"

AND HERES MY ERROR:

INFO Reading configuration /config/esphome/propane-scale.yaml...
Failed config

sensor.template: [source /config/esphome/propane-scale.yaml:67]
  platform: template
  name: Propane Weight Percentage
  id: propane_percent
  
  [value_template] is an invalid option for [sensor.template]. Please check the indentation.
  value_template: |-
    {{ ((states('sensor.propane_scale_propane_tank_weight') | float - 1.59)/0.2914) | round(2) }}
  unit_of_measurement: %
  
  [icon_template] is an invalid option for [sensor.template]. Please check the indentation.
  icon_template: mdi:weight

:thinking: lambda

You are using the HA template format in ESPHome. That won’t work…

Read the linked page above and also have a look at: Automations and Templates — ESPHome for examples.

Thank you both for setting me on the right path. Reworked it. Having some issues with return syntax in the lambda function. Tried a few things. None correct yet. Where am I going wrong here? Any guidance is appreciated.

USING THIS CODE:

- platform: template
   name: "Propane Percent"
   id: propane_percent
   lambda: |-
      return ((id(propane_weight).state | float - 1.59/0.2914));
   unit_of_measurement: "%"
   icon: "mdi:weight"```

GETTING THIS ERROR WHEN I COMPILE:

/config/esphome/propane-scale.yaml: In lambda function:
/config/esphome/propane-scale.yaml:75:40: error: expected primary-expression before 'float'
   75 |       return ((id(propane_weight).state | float - 1.59)/0.2914));
      |                                        ^~~~~
/config/esphome/propane-scale.yaml:75:39: error: expected ')' before 'float'
   75 |       return ((id(propane_weight).state | float - 1.59)/0.2914));
      |               ~                       ^~~~~~
      |                                       )
/config/esphome/propane-scale.yaml:75:62: error: expected ')' before ';' token
   75 |       return ((id(propane_weight).state | float - 1.59)/0.2914));
      |              ~                                               ^
      |                                                              )
*** [/data/propane-scale/.pioenvs/propane-scale/src/main.cpp.o] Error 1

Lambda is C++. So pipes like they use in Jinja in HA are not supported.

Try just:

return (id(propane_weight).state - 1.59/0.2914);

Unsure what you are trying to calculate - but you may also need another set of brackets there if you wanted to do the subtraction from the weight before the division.

1 Like

Thanks so much for the quick assist! It is much appreciated. And yes, you were also correct, I needed to add another set of brackets to subtract before dividing.