Platform Home assistant sensor used in template

I want to make a Solar boiler project to store PV energy in a boiler.
In HA I have made a sensor named Dump Current.
The goal is to have a PWM output that is mapped to a range of current (Amps) value.
When compiling the code I get the error below:

Lambda contains reference to entity-id-style ID ‘sensor.dump_current’ ‘sensor.dump_current’ ‘sensor.dump_current’ ‘sensor.dump_current’. The id() wrapper only works for ESPHome-internal types. For importing states from Home Assistant use the ‘homeassistant’ sensor platforms.

esphome:
  name: boiler
  comment: boiler heater with solar energy (dump current)
  platform: esp8266
  board: d1_mini
  on_boot:
    priority: 0
    then:
      - output.set_level:
          id: pwm_output
          level: 0

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

web_server:
  port: 80  


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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
   
  

captive_portal:

dallas:
  - pin: GPIO0
    update_interval: 1s

globals:
  - id: pwm_off
    type: int
    restore_value: no
    initial_value: '0'
  - id: min_pwm_value
    type: int
    restore_value: no
    initial_value: '30'
  - id: actual_pwm_value
    type: int
    restore_value: no
    initial_value: '0'
  - id: max_pwm_value
    type: int
    restore_value: no
    initial_value: '255' 
  - id: temp_start_heating
    type: float
    restore_value: no
    initial_value: '56.0' 
  - id: temp_stop_heating
    type: float
    restore_value: no
    initial_value: '60.0' 
  - id: min_current
    type: float
    restore_value: no
    initial_value: '1.0' 
  - id: max_current
    type: float
    restore_value: no
    initial_value: '5.0'


sensor:
  - platform: dallas
    address: 0x280008037073a910
    name: "DS18B20 Sensor" ## THIS SENSOR MEASURES THE WATER TEMPERATURE IN THE BOILER
    id: boiler_temp
    

  - platform: wifi_signal
    name: ${friendly_name} Signal
    update_interval: 60s

  - platform: homeassistant
    name: "Dump Current HA"
    entity_id: sensor.dump_current 
    accuracy_decimals: 1

output:
  - platform: esp8266_pwm
    pin: GPIO16
    frequency: 10000Hz
    id: pwm_output
    inverted: true

  - platform: template
    id: pwm_output
    type: float
    write_action:
      - output.set_level:
          id: pwm_output
          level: !lambda |-
                    if ((id(sensor.dump_current) > 1) && (state) < id(max_pwm_value 
                      {
                          id(actual_pwm_value) +=1;
                          ### Map new value##
                          return ((id(sensor.dump_current) - (id(min_current)) * ((id(max_pwm_value) - (id(min_pwm_value) / (id(max_current_current) - id(min_current)) + (id(min_pwm_value);
                      }
                      else if ((id(sensor.dump_current) < 1)
                      {
                         id(actual_pwm_value) -=1;
                         ### Map new value##
                         return ((id(sensor.dump_current) - (id(min_current)) * ((id(max_pwm_value) - (id(min_pwm_value) / (id(max_current_current) - id(min_current)) + (id(min_pwm_value);
                      }
                      else 
                      {
                          return 0;
                      }

Give the sensor an id like the others, then use that in the lambda.

You mean change the entity_id to id?
like this ? id: sensor.dump_current
Or add an additional id with a different name?

Add one, like your boiler_temp. The entity_id is a reference to the HA entity.

If you add id: dump_current, you can then reference its state in the lambda with id(dump_current).state.

Thanks Troon for your reply. I still don’t understand exactly what you mean. I added a template sensor but I think you don’t mentioned this. Please feel free to adapt the code with your thoughts. This would help me a lot.

sensor:
  - platform: dallas
    address: 0x280008037073a910
    name: "DS18B20 Sensor" ## THIS SENSOR MEASURES THE WATER TEMPERATURE IN THE BOILER
    id: boiler_temp
    

  - platform: wifi_signal
    name: ${friendly_name} Signal
    update_interval: 60s

  - platform: homeassistant
    name: "Dump Current HA"
    entity_id: dump_current 
    accuracy_decimals: 1

  - platform: template
    name: "dump_current_template"  
    entity_id: dump_current
    accuracy_decimals: 1
    

output:
  - platform: esp8266_pwm
    pin: GPIO16
    frequency: 10000Hz
    id: pwm_output
    inverted: true

  - platform: template
    id: pwm_output
    type: float
    write_action:
      - output.set_level:
          id: pwm_output
          level: !lambda |-
                    if ((id(dump_current_template).state > 1) && (state) < id(max_pwm_value)) 
                      {
                          id(actual_pwm_value) +=1;
                          ### Map new value##
                          return ((id(dump_current_template).state - (id(min_current)) * ((id(max_pwm_value) - (id(min_pwm_value) / (id(max_current_current) - id(min_current)) + (id(min_pwm_value);
                      }
                      else if ((id(current_to_dump) < 1)
                      {
                         id(actual_pwm_value) -=1;
                         ### Map new value##
                         return (id(dump_current_template).state - (id(min_current)) * ((id(max_pwm_value) - (id(min_pwm_value) / (id(max_current_current) - id(min_current)) + (id(min_pwm_value);
                      }
                      else 
                      {
                          return 0;
                      }

Looks Troon recommended to write it so:

  - platform: homeassistant
    name: "Dump Current HA"
    entity_id: sensor.dump_current
    accuracy_decimals: 1
    id: dump_current

And now use id(dump_current).state in lambdas.

2 Likes