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

@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:

@CMDK - 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.

Wow, complex.

My suggestion: create the binary_sensor inside configuration.yaml.

If it’s a binary sensor that is written for Home Assistant, it goes in configuration.yaml. If it’s written for ESPHome it goes in the ESPHome YAML. Binary Sensors with templates like this go in configuration.yaml.

Hi, I would like to do a similar project with a wood stove.

I have an ESP32 with a Max6675 termocouple which gives me fire temperature.


  - platform: max6675
    name: "Temperatura Stufa"
    cs_pin: GPIO15
    update_interval: 30s
    id: fire_temp

I would like a sensor that gives me this information:

From 0 to 50 degrees = OFF
From 50 to 150 degrees = CHECK FIRE
From 150 to 300 degrees = FIRE OK

How can i get this?
Thanks

WERE YOU ABLE TO GET THIS WORKING?

I want to be able to control a light that is on an ESP32, from anther ESP32.

THANKS!

Apologies for the delayed response.
@rlust @KameDomotics
I did get this to work by adding the following code into the ESPyaml

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO5
      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;

Although i am not using it at the moment (1. its summer here and 2. I have just updated to a power monitor which does not have to wait for the temp to hit target.

1 Like