Assistance with device_class: heat ----> value_template: (please)

Hi all,

Hoping someone can assist, as i am stumped.

I have a Sonoff sv configured with esphome yaml and a ds18b20 temperature sensor. I’m also able to trigger the relay on the sv board as a dry circuit contact, which toggles the power on my gas fireplace. As can be seen in the image below, I am receiving a temp value from the sensor and a ‘normal’ value from the device_class: heat. The relay switching also works.
image
I would like to configure the device_class: heat with a temperature parameter, so that i can remove the “Fireplace 21.1C” entity from the card, and just display the “Fireplace state” which will show when the fireplace is on (when it is more than a defined value).

The above image is captured while my yaml does not include the value_template code, ie i have not defined a temp value which will determine whether the Fireplace state shows Normal or Hot

  - platform: template
    sensors:
      fireplace:
        friendly_name: "Fireplace Sensor"
        device_class: heat
        value_template: "{{ states('binary_sensor.fireplace')|float > 30.0 }}"

When i add the code above, i get an error [sensors] is an invalid option for [binary sensor.template]. Please check the indentation

esphome:
  name: sonoff_sv_fireplace
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  reboot_timeout: 0s
  fast_connect: true

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

web_server:
  port: 80

dallas:
  - pin: GPIO14

sensor:
  - platform: dallas
    address: 0x42041670FCC2FF28
    name: "Fireplace"

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO14
      mode: INPUT_PULLUP
      inverted: True
    id: fp_state
    name: "Fireplace state"
    device_class: heat
  - platform: template
    sensors:
      fireplace:
        friendly_name: "Fireplace Sensor"
        device_class: heat
        value_template: "{{ states('binary_sensor.fireplace')|float > 30.0 }}"

switch:
  - platform: gpio
    pin: GPIO12
    id: sonoff_sv_fp_relay_gpio
  - platform: template
    id: fireplace_switch
    icon: "mdi:fireplace"
    name: "Fireplace Switch"
    turn_on_action:
    - switch.turn_on: sonoff_sv_fp_relay_gpio
    - delay: 500ms
    - switch.turn_off: sonoff_sv_fp_relay_gpio

I have tried all manner of indentation and structure but cannot seem to work it out… Any help would be appreciated.

Thanks in advance

This YAML appears perfectly valid to me.

There must be something else that is incorrect that I just can’t see here (config in the wrong file, indentation using mixed spaces and tabs, something).

Sometimes, using this method of config check can give more information:

Thanks for the feedback. Can I just ask, would that also work with the yaml code within esphome rather than in a configuration.yaml or other HA yaml files like scripts, automations…

No. check_config only works for Home Assistant YAML.

I don’t think the state of your binary_sensor.fireplace is a number, I think it is on/off?
And I suppose the temperature-value is an attribute?
Can you show us the state-info of the binary_sensor.fireplace as shown under Developer Tools - States?

@bouwew - Thanks for the feedback.

Yes, you are correct, the binary_sensor is an on/off as opposed to a number.

I see why you have identified that is the possible cause of the issue, i have updated it to reference sensor instead of binary_sensor, but am still getting the same issue…
image

Question to anyone smarter than me - should the binary_sensor: platform: template part of the code go into the configuration.yaml file or is it ok to be in the yaml file of the esphome component (in this case sonoff sv)?

@Mikkaat, do you know how to use the Developer-Tools of Home Assistant?
Please find both your sensor_fireplace and your binary_fireplace_sensor in the STATES-tab in the Developer-Tools page.
Then look at the Attributes, copy these and show them for both sensors in this topic, please.
Without this information, we will not be able to help you.

To answer you question: both options are possible, it depends on the purpose of the binary_sensor. Best to read the ESPHome documentation and try stuff out.
To see the result always look in Developer Tools - STATES-tab.

@bouwew - apologies, I will do this tonight when i get back home…

In the meantime, can i trouble yourself or anyone else with some noob questions…

The Binary Sensor Template page https://www.home-assistant.io/components/binary_sensor.template/ has the following example for a value template, which is pretty much what i am trying to do…

binary_sensor:
  - platform: template
    sensors:
      furnace_on:
        friendly_name: "Furnace Running"
        device_class: heat
        value_template: "{{ states('sensor.furnace')|float > 2.5 }}"

My question is around the Configuration Variables as per the help text / examples on the page… The help text states

sensors: is required and is “List of your sensors” - Should i be adding in the value fireplace: instead of the word sensors?

sensor_name: is required and is “The slug of the sensor” - What is the slug?

Apologies if these are basic questions, i have done my research but just cannot get this to work…and i am hoping this thread helps others as well…

Thanks in advance,

The binary_sensor (State is ON or OFF) that you are creating, requires another sensor as the basis (the sensor.furnace in the example). A sensor with a State that shows a value.
The sensor - platform:dallas that is shown in your post above, could work. I depends on what this sensor outputs into its State. Or you could use one of the additional Attributes, when present, as the basis.
So, find this sensor in Developer Tools- States. The name is most likely sensor.fireplace

What is a “slug”? I googled this term, seems to mean a short name that describes the main function of the binary_senor. See here: https://www.awardforce.com/blog/what-is-a-slug

you created the binary_sensor.furnace_on with the code above, and that is what you should use in the card to display.

the device_class will take care of the icon it shows. Since you’ve created it to be on when sensor.furnace > 2.5, it will use that as the decisive threshold for the binary_sensor to be ‘on’ or ‘off’.

No, the way you did it above is correct. This is the order of things in the yaml:

binary_sensor:
  - platform: template
    sensors:
      name_of_sensor:  # this is the slug
        friendly_name:  # this is the name displayed in the frontend
        value_template: >
          {{whatever_value is 'True'}}
        etc

Try this:

esphome:
  name: sonoff_sv_fireplace
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  reboot_timeout: 0s
  fast_connect: true

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

web_server:
  port: 80

dallas:
  - pin: GPIO14

sensor:
  - platform: dallas
    address: 0x42041670FCC2FF28
    name: "Fireplace"
    id: fireplacetemp

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO14
      mode: INPUT_PULLUP
      inverted: True
    id: fp_state
    name: "Fireplace state"
    device_class: heat
  - platform: template
    name: Fireplace sensor
    device_class: heat
    lambda: |-
      if (id(fireplacetemp).state > 30) {
        // Fireplace is on
        return true;
      } else {
        // Fireplace is off
        return false;
      }

switch:
  - platform: gpio
    pin: GPIO12
    id: sonoff_sv_fp_relay_gpio
  - platform: template
    id: fireplace_switch
    icon: "mdi:fireplace"
    name: "Fireplace Switch"
    turn_on_action:
    - switch.turn_on: sonoff_sv_fp_relay_gpio
    - delay: 500ms
    - switch.turn_off: sonoff_sv_fp_relay_gpio

HI,

the use of lambda: and id: is a first for me, please let me ask where did you find the lambda: way of creating templates in Jinja or Ha templating extensions?

Checked both documented ways for creating templates as instructed in HA, but didnt find any reference (yet…)

also, since I can’t enter this in the developer-tools/template,

does this:

|-
      if (id(fireplacetemp).state > 30) {
        // Fireplace is on
        return true;
      } else {
        // Fireplace is off
        return false;
      }

do the same as

{% if states.sensor.fireplace.state|float > 30) %} True
{% else %} False
{% endif %}

?

in which case the template could be made into

{{ states('sensor.fireplace')|float > 30 }}

thanks for any pointers!

Hi Marius,

I’m using this “exact” example (just for my washing machine) in ESPHome. I doubt that the lambda works in the Home Assistant config, but it sure does in the ESPHome-config. :slight_smile:

A ok, I see. thanks, couldn’t get if figured out :wink:

interesting though to pass an id on like that.

Agree, but I honestly just followed examples of others, and I’ve seen it multiple times :slight_smile:

@cklit - thank you for your help, this resolved the issue. Later tonight, I am going to try the fireplacetemp in the value_template to see what that does, just so i can understand why i cannot achieve the same result with value template.

Thanks again for your help.

@bouwew - CMDK has resolved the issue using lamda:, but i am still interested in why value_template would not work. I will completely understand if you do not want to look at this anymore though, so want to say thankyou for all your help and guidance. I am going to revert to my original code later tonight and add the fireplacetemp id as per cmdk’s code, to see if that works, and will reference the sensor.fireplace instead of binary_sensor.fireplace.
Below are the sensors in my Developer Tools, States tab.

Again, thanks a lot for all your help, and i would completely understand if you do not want to look at this further.

I think your value_template is wrong.

Go to Developer Tools - TEMPLATE
Under the blue text Template Editor create a new line and put your template text there. You can see the result on the right side of the page.

Never mind, others already showed how to do it correctly :slight_smile:

yes, almost, please add the quotes to the template around the sensor:

{{ float(states('sensor.fireplace')) > 30 }}

or:

Maybe this post from Otto will give you the answer:

Jinja templates simply does not work in ESPHome.