Write value from HA for use in lambdas?

I (mostly) get how you can create a custom sensor so you can display the current value of something from ESPHome to Home Assistant, but what if I want to write back to a variable for use in a lambda?

For example, when calculating a flow rate I need to count the number of cycles equal to one cubic foot. On my meter that happens to be 8, but when sharing the code I’d like to be able to let people adjust this from HA instead of having to change some values in the code and upload it fresh.

Is it possible to write data back to a variable in ESPHome from Home assistant?

You’d make an ESPHome number entity that you’d expose to HA. Automate using that entity. You can control that entity from HA.

You can also access an HA sensor, but to me that’s backwards and too coupled. You’d make an input number in HA, wrap it in an HA template sensor and access that from ESPHome.

1 Like

Okay so I made a number entity, and at first glance it looks okay, but it updates once and then never updates again on the home assistant side. I know it’s updating in the lambda area elsewhere in my code since my math calculations are coming out correctly, but it’s not updating the value as shown in HA.

number:
  - platform: template
    optimistic: true
    name: "${friendly_name} Max Value"
    id: gas_max
    update_interval: 10s
    initial_value: -1000.0
    min_value: -5000.0
    max_value: 5000.0
    step: 0.00001
    restore_value: false
    unit_of_measurement: ft³/min
    device_class: 'gas'

Not sure what you mean here. When you say “updates” are you changing it on the ESP device?

Number entities on the ESPHome device can be updated using number.set: etc, or via lambda using make_call(). On the HA side they are an entity that can be updated on the dashboard or using developer tools.

Either way, an update on one side is always reflected on the other. Updating the entity in HA updates the value on ESPHome and vice-versa.

Its being changed on the esp device in a lambda, but the change isnt reflected in home assistant.

Can you give an example of the number.set or lambda make_call? I read the example on the esphome number template, but in that example, it was a hardcoded number.

Currently in an interval lambda i do a

  Id(variable).state = math goes here

To change the value, but after that change I’m at a loss on how to get HA to reflect the change to that number template variable.

That doesn’t work. .state is read only. To set the value of a number in lambda, use call.set_value() as per the docs:

auto call = id(variable).make_call();
call.set_value(maths goes here);
call.perform();

You could also use number.set:

- number.set:
    id: variable
    value: !lambda |-
      return math goes here;
1 Like

Thank you! I kept trying to search for information on the Number Template and ending up in areas that just never quite gave the information to do this. I set up two “autos”

auto maxCall = id(gas_max).make_call();
auto minCall = id(gas_min).make_call();

set them using

maxCall.set_value(maths goes here);
minCall.set_value(maths goes here);

and then pushed the new values using

maxCall.perform();
minCall.perform();

I also had a binary_sensory, but that easy set with

id(calibrated).publish_state(true);

Thanks for the help!