How to use Number Component

I am struggling to find out how to use the Number Component in ESPHome (and not the number template).

None of the examples on that page tells me which platform to use…? All examples say:

number:
  - platform: template

But then I get the number template which is not what I am looking for. How am I supposed to find out what platform to use for the number component?

Am I missing something here?

What exactly are you looking for then? What functionality?

As far as I know there are 3 number platforms:

  1. Template
  2. Tuya Number
  3. Modbus Controller Number

Using number template to specify and modify then some parameters from HA, like temperature thresholds, coefficients, offsets, etc. F.e. temperature sensor offset which not hardcoded, but can be changed in runtime.

X-Y Problem.

Instead of telling us you can’t get your chosen solution to work, tell us what you want to accomplish and why you think an input number is the solution for this.

Why not Number Template? What disadvantage do you have if use it?

It seems like Number Component creates an entity in Home Assistant that I can show and change both in the Home Assistant GUI and in my ESPHome code/yaml.

I thought it was an easy way to “send” numbers between HA och ESPHome. I set the value in HA and it shows up in ESPHome. I set another value in ESPHome and the value changes in HA.

Because I want to learn more and understand how this Number Component works. :wink:
Mayby it can be useful for more things than just this particular use case?

I am a little surprised that noone knows what this Number component is all about?

I think you’re confusing things. A ‘number template’ is just a type of ‘number component’. Same as there is a switch component, light component, etc.

So to setup a number, you have to use a platform, one of which is a template.

That’s why folks are asking what you’re trying to achieve; that will determine the best approach (and whether a number is even the best approach).

Thank you, you are right that I am confused - but mostly by the documentation. :slight_smile: :confused:

What I want to do is to

  1. Manually set an servoangle in HA (for example an input_number)
  2. Click on button in HA and get that number transferred to ESPHome
  3. ESPHome gets the number an turns the servo to the set angle

My main problem is 2; how to get the number transferred over to ESPHome in an easy way. I was hoping for the Number Component to “automatically syncronize” the number entity between HA och ESPHome, but I guess I was wrong.

I seems easy to get numbers (via sensors) to HA, but how do I easily “send/set” numbers from HA to ESPHome?

Did you read through the documentation? It answers all of your questions.

FYI… lets say your going to use a server for an esphome project, wheres the first place you should go to learn how to set it up and use that servo in esphome???..4…3…2…1

The esphome documentation!

If you would have, you wouldnt be asking questions that are specifically addressed in the Docs.

It plainly tells you how to do this exact thing in the Servo Docs.

This one is a little bit trickier and only because there are a whole bunch of ways to control or send commands to change your servo angle. It can be done automatically once you select a new number from your Number entity or it could be done from a button or other ways.

Once again, its all in the documentation! The example they show you, the exact one from above. The “automatic” way is shown in the example.

   set_action:
      then:
        - servo.write:
            id: my_servo
            level: !lambda 'return x / 100.0;'

And what is (set_action: … ) and what does it do? Look at the documentation!!

set_action (Optional, Action): The action that should be performed when the remote (like Home Assistant’s frontend) requests to set the number value. The new value is available to lambdas in the x variable.

So, set_action is the configuration option where you tell your Number entity what it should do when a value is set from HA.

Another part of that is the configuration option, “Optimistic:” what is that? It tells you in docs!

optimistic (Optional, boolean): Whether to operate in optimistic mode - when in this mode, any command sent to the template number will immediately update the reported state. Cannot be used with lambda. Defaults to false.

Now, these are just 2 configuration options out of several. You always need to read them and decide if or if not you need them and then configure them according to the needs of your project.

Personally, im not a fan of using a number box or slider to set a value for servos. One example would be a rotating camera i use and there I prefer buttons and call them Left, Right. Can you guess where you might find information on how to do this??? Ya! The documentation for Servo and its just right above your Number example also.

This here,

on_...:
  then

That (…) is for whichever platform you may need. For example if you wanted a servo to move when a light turns on/off youd go to the documentation and look up “light” Here it tells you to use, on_turn_on: or on_turn_off: Light uses specific action/configuration where something like Button does not use on_turn_on:

on_turn_on (Optional, Action): An automation to perform when the light is turned on. See light.on_turn_on / light.on_turn_off Trigger.

So, it would be…

on_turn_on:
  then:
  - servo.write:
      id: my_servo
      level: -100.0%

What if you want to use a Button instead of a light to control your servo? Guess where you gotta go look???

Here you can see that Button uses on_press.

Now you know what to use for the (on_…: ) from the example, you use on_press: if you need a button.

on_press:
  then:
  - servo.write:
      id: my_servo
      level: -100.0%

Is esphome getting the number outside of HA or from esphome itself? set_action takes care of however the number gets changed from esphome, HA, or whatever else.

So… whats the takeaway point from all this???

USE THE DOCUMENTATION!

Thats whats its there for and why so many people work to keep it up-to-date, so you can help yourself as much as possible!

This may be helpful too but, it isnt a reason not to go look up the documentation either!

Well, not really…
I want to know how to do it, but the documentation just tells me what can be done. Big differens when you are not very good at ESPHome yaml coding. :frowning:

My best guess now is that I should use the lambda or set_action configuration variable. And with a bit of luck and by copying from other parts of my yaml code, I got this to work: :slight_smile:

# Setting the servo angle from Home Assistant.
# Shows up in HA as a "number.servo_angle" entity
# and whenever the entitis value is changed in HA,
# it also gets updated in your ESPHome yaml.
number:
  - platform: template
    name: "Servo angle"
    id: servo_angle
    optimistic: false
    min_value: -30
    max_value: 30
    step: 1
    set_action:
      - servo.write:
          id: servo1
          level: !lambda "return x / 100.0;"

Yes, it tells things that can be done with it. Im not exactly sure what it is your wanting if not that.

Bud, I hate hearing excuses about not being good at esphome, coding or infuriatingly, saying “I cant”

If I can, you sure as hell can too. It takes time and practice but, if you use the documentation for the excellent resource it is, its a lot easier to learn.

Also, dont ever be afraid to try things or experiment. You dont need to first become a grand master for Number inorder to use it.

Create a basic number template, button template, etc then flash it to an esp board inorder to learn and familiarize with them. Try the different configuration options if you dont fully understand them.

Its perfectly fine to do exercises like that toplay around, explore, and ultimately you learn. When your done then delete the “test entities” by reflashing your esp32 after you remove them.

Did you look at the other configuration options for servo? What are you doing with the servo? Whats it moving?

FYI. Number component and Select component are nearly identical how they work. Obviously one does numbers and one does selectable options but other than that, they basically work the same way.

I must admit to a little TLDR in the posts above, and solutions are there. But as you want to learn, there are ways I didn’t see in my quick scan of the above. Sorry if I mention something that was answered before.

From what I get, all the answered are focussed on what you can do in ESPHome to get things done. You want to get HA data into ESPHome, and there’s also an option to do it the other way around.

Instead of creating entities in ESPHome (a fine solution), there’s also the option to get HA entities that were created there into your ESP:

Note that this is from HA to the ESP, I don’t think it is two way for input numbers (I never tried). It might work if you set internal false, but I did not fully understand the docs on that bit. So the solution you have is probably best.

With regard to documentation: it is unavoidable that it describes how things work, not how thy can be used to solve a problem. There may be an example to suit your needs, but more often that is not the case. That is because there many, many of problems that can be solved using the same tool. When you buy a drill, you get instructions how to operate the drill, not instructions how to put up shelves on the wall, get an electricity wire into a closed wooden cabinet, …

No, I understand the confusion. You can make Number, Select, Switch, sensors, etc in esphome or HA. Your confusion is when to use the esphome one or the HA one, correct?

For what your doing, youd definitely want to keep it all in esphome for 1 simple reason. Only that eaphome node is needed to move your servo. If for example you made your Number component in HA and then linked it to your esp node with servo. What happens if HA crashes or it has some issue? Because you split up your servo controls between esphome and HA, they both have to be working and communicating with each other inorder to use your servo at all. You couldn’t even use the node’s AP to login to and move it because it can’t communicate with Number in HA, so you have 2 points of potential failure instead of just 1 point.

Generally, you want to avoid doing stuff like that for obvious reasons. If it can be made in esphome then i’d strongly suggest doing that.

The HA sensor is just for importing any HA sensor into your esphome node whether it’s an entity from HA or some Integration or add-on, it can be imported to any esphome node. For example, let’s say you build an esphome sprinkler. Sprinkler Controller — ESPHome

To make it smarter you could import rain forcast sensors and use that data so your sprinkler can run “smarter” and possibly not run on a day when there is a 80% chance of rain in the forcast. Likewise I could import the outside temperature from HA and if today was going to be 105f, my sprinkler could use that data point to increase watering times for each zone. I can’t get that future data from some temp\humidity sensor wired to the esp board but, I can from HA.