How to pass a user value?

This code works with static “- above:” and “- below: 70.0” values. I want to pass the value “Set Temp” from the web ui .

esphome:
  name: temp-led
  friendly_name: temp-led

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxx"

ota:
  password: "xxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Temp-Led Fallback Hotspot"
    password: "xxxx"

i2c:
  scan: true

sensor:
  - platform: sht3xd
    temperature:
      filters:
      - lambda: return x * 1.8 + 32.0;
      unit_of_measurement: "°F"
      name: "Temperature"
      id: temp_led_temperature
      on_value_range:
        - above: 70.0
          then:
            - light.turn_on: led
        - below: 70.0
          then:
            - light.turn_off: led

    humidity:
      name: "Humidity"
      id: temp_led_humidity
      
    address: 0x44
    update_interval: 60s


light:
 - platform: status_led
   name: "LED Output"
   pin: D7
   id: led

number:
  - platform: template
    name: "Set Temp"
    id: newtemp
    mode: slider
    optimistic: true
    step: 1

2024-05-02_19-48-35

Hello and welcome to the forum. :slight_smile:

It doesn’t make sense to take a look at the code you posted, as it could simply be wrong by formatting. :slight_smile:

Please post the code properly formatted, so we can help you! Thanks! :slight_smile:

Thanks, Sorry about that. This is my first post. I re-posted the code.

Not a problem, but the code is still not properly formatted. :wink:

Take a look at the link I posted above, there it is explained, why formatting matters very much, and how to format the code correctly to show here in the forum.

Thanks again. I think, I think is now properly formatted.

:+1:

Just to be sure, we’re talking about the HA dashboard you showed in your second picture, right? So you want to set the temp in your dashboard and your ESP should work with that?

If so, you’d need to import the value from HA via the sensor component in ESPHome, see here:

I’m not 100% sure that the “below” field is templatable, but I think so. :slight_smile:

Let me know if it works for you! :slight_smile:

Unfortunately on_value_range and similar stuff accepts only fixed value.
Use on_value with lambda and write code You want like:

- platform: sht3xd
  ...
  on_value:
    - lambda: |-
        if (x>id(newtemp).state) { id(led).turn_on(); } else { id(led).turn_off(); }
1 Like

Thanks for the clarification, I wasn’t sure. :+1:

Another way is to use binary_sensor - I like it, actually.

binary_sensor:
  - platform: template
    id: bs_newtemp
    lambda: return (id(temp_led_temperature).state > id(newtemp).state);
    on_press:
      - light.turn_on: led
    on_release:
      - light.turn_off: led

You can use binary_sensor in frontend, add more conditions to it lambda, as well it leads to less operations with led - will turn on/off only on transition, not on every temperature reading. It is not a big, but overhead. IMHO.

1 Like

thanks very much all. I finally did it and working… For all new coders below is my final version of my yaml.

# turn-on LED and relay by user set temperature.

esphome:
  name: temp-on-off
  friendly_name: temp-on-off

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxx"

ota:
  password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Temp-On-Off Fallback Hotspot"
    password: !secret ap_password

captive_portal:

i2c:
#  sda: D2
#  scl: D1
  scan: true

sensor:
  - platform: sht3xd
    temperature:
      filters:
      - lambda: return x * 1.8 + 32.0;
      unit_of_measurement: "°F"
      name: "Temperature"
      id: temperature
      on_value:
      - lambda: |-
         if ( x > id(newtemp).state) { id(led1).turn_on(); id(relay1).turn_on(); } else { id(led1).turn_off(); id(relay1).turn_off(); }

    humidity:
      name: "Humidity"
      id: humidity

    address: 0x44
    update_interval: 60s

switch:
 - platform: gpio
   name: "LED"
   pin: D7
   id: led1

 - platform: gpio
   name: "Relay"
   pin: D6
   id: relay1

number:
  - platform: template
    name: "Set Temp 'ON'"
    id: newtemp
    optimistic: true
    mode: box
    initial_value: 70   
    restore_value: True
    min_value: 50
    max_value: 100
    step: 0.1
1 Like