How to access helper text or helper number in esphome

" How to pass a number or time value from the Home Assistant Dashboard to esphome where something expects a time format like sleep_duration: or delay: or update_interval: in seconds s minutes min hours h. That is the issue. "

How to access HA helper text or helper number in esphome?
Only look at the last line: sleep_duration: 15s
I’m using an IR remote to put esp8266 to sleep, and want to be able to change
values used in esphome on the fly from the dashboard helper texts/numbers.

binary_sensor:
  - platform: remote_receiver
    name: "Samsung36 Remote BUTTON 1"
    samsung36:
      address: 0x0400     
      command: 0x000E40BF    # samsung bluray    1 button
    on_press:
      then:
         - switch.toggle: huGPIO0      #  TOGGLES RED LED huzzah
         - deep_sleep.enter:           #   TESTING   DEEP SLEEP
             id: sleepNOW
             sleep_duration: 15s   #  <<<<<<  ONLY LOOK HERE   

HOW DO I REPLACE 15s with:
ENTITY ID input_text.helper_text_1 OR
ENTITY ID input_number.helper_number_1 (seems harder since can’t specify time unit)

I defined Helper Text in HA Automations with ENTITY ID input_text.helper_text_1
I would like to be able to change the sleep duration with this value from Hassio Dashboard,
rather than re flashing to make minor changes. Can you help? thanks

service: homeassistant.update_entity ??

I am lost in espHome trying to do something similar. I do not have an answer for you but maybe we can help each other.

If you can create a sensor with the sleep_duraton in the front end, then you may be able to read it in espHome using a lambda call. Look at on_raw_value.

I want to have a switch in the front end that turns OTA mode on or off (without MQTT). But I am having tremendous frustration figuring out how to get and handle the sensor value.

Any tips you throw my way would be appreciated.

Oh, I think that we both will need to use the interval component to periodically look for changes to the sensor value. We also will need to handle the case if the homeassistant server is offline because esphome is supposed to work without a network connection.

Thoughts?

I too want to stay native with no mqtt until I have to. Yes I have shown that I can run mqtt on my home assistant hassio pi, so it’s all still in house, but still.

You just gave me a great idea, as soon as I found a major problem. I don’t yet have a solution passing
sleep_duration:
what it wants. I just found that it needs and number AND a time unit, together, like 15s for 15 seconds. You CANNOT leave off the s. It needs to end with h or s or min, etc. I assumed that it would default to milliseconds but once I tried it with just the number, it flags as bad syntax. Bingo, that’s why I couldn’t pass it HOME ASSITANT SENSOR with a lambda call, because it’s just a number. I compiled and flashed all green, but crashed at run time with initiating deep_sleep. So, I can’t just pass it an integer and I don’t know how to pass it a string using HOME ASSISTANT TEXT SENSOR, yet, if that’s possible. That crashed at compile time.

Do you have a syntax issue like this? Test with strait #s without passing in anything to verify first. How and why are you trying to disable OTA mode? Why not just use a password to protect?

I will try your idea of having a matched entity on the front end. Maybe that’ll work.
( I have no idea how to do that. )

What I did get working is passing a number from a HELPER NUMBER box on the HA dashboard, via lambda call in esphome, and it controls the timing of a blinking led, merely as proof that this approach does work. I added nothing to configuration.yaml. I only added the following to the esp8266.yaml and the subsequent usage, and it works. There was no need to periodically update anything on an interval. The HOME ASSISTANT SENSOR in esphome seems like a pointer to the ENTITY ID when needed, and in real time.

# In esphome (for my esp8266) I added:
sensor:
  - platform: homeassistant
    id: hasensor1     # this is the ID for THIS sensor, and copies:
    entity_id: input_number.helper_number_1  # this points to the front end entity

#usage
  - platform: remote_receiver
    name: "Samsung36 Remote BUTTON 2 to SONOFF RELAY"
    samsung36:
      address: 0x0400       
      command: 0x000EC03F     # samsung bluray    2 button
    on_press:
      - switch.toggle: huGPIO0
      - delay: !lambda 'return id(hasensor1).state;'
      - switch.toggle: huGPIO0
      - delay: !lambda 'return id(hasensor1).state;'
      - switch.toggle: huGPIO0
      - delay: !lambda 'return id(hasensor1).state;'
      - switch.toggle: huGPIO0

Set up a number helper adjust values as needed

  - platform: template
    id: my_number
    name: "Template number"
    optimistic: true
    min_value: 0
    max_value: 100
    step: 1 

then in the component use

    on_press:
      then:
         - switch.toggle: huGPIO0      #  TOGGLES RED LED huzzah
         - deep_sleep.enter:           #   TESTING   DEEP SLEEP
             id: sleepNOW
             sleep_duration: !lambda 'return id(my_number).state * 1000;'    # esphome needs ms for time

Two things I have learned about Lambdas.

  1. They are not evaulated by the editor, so you can get ‘all green’ up to the compile. Because,
  2. the code in a lambda is C++ (sort-of), and only compiled when you hit “install”.
  3. Beginner documentation simply does not exist. With the exceptional documentation for Automations and templates

I haven’t much experience with Lambdas (like none that wasn’t just copied) but since it is C++, couldn’t you just append the units after getting the state?. Instead of .state have you tried .raw_units?

Disable OTA?
The project is solar powered and I want it to sleep between readings, but I want OTA to work sometimes, so I need to disable sleep when I want to use OTA. It beats bringing the project back into the workshop just to update the code.

I use a Raspberry Pi3 running Mosquitto as my MQTT broker. That’s all it does.

I can’t use MQTT and api: at the same time according to the docs. But if I don’t have api: running, all of my device entities go away. I am wondering if a better solution would be to use MQTT to send the sensor data in a status message, then turn them into entities in Node Red? This way the MQTT retained message semaphores could control sleep. No API needed.

@stevemann
Of course you can use api: and mqtt: at the same time. Where does it say that you can’t? I just don’t want to, or see a need yet (until I add non hassio non esphome components).

Here’s an example using both api: and mqtt: that I roughly followed at adafruit, but I changed mqtt to talk to my local mosquitto broker HA add-on. SEE IMAGE above. This runs on the same pi as my hassio, so no need for another device. I’m not paying anyone for cloud if I can be the cloud.

(PAGE FWD/BACK ARE AT THE BOTTOM)

There are errors in this example, but I was able to fix and it seems to work. Then I removed/disabled mqtt until I need it.

I haven’t much experience with Lambdas either (only cut and paste usage). I thought of trying to append the units to the number, but I don’t know how, and seems clugy. .raw_units? Can’t find anyting on that.

Like I showed in my last response, I was able to pull a NUMBER from a hassio ENTITY with only the ENTITY ID, and I did not need to create a template with an id: to reference (but I will eventually try that).
entity_id: seems like the real ID
id: seems like a nickname, useful as that is


The reason for this post/topic is that I need to pass a 15s or 10m or 1h from the home assistant dash board to esphome delay: (for example) that is expecting a TIME value, and will not accept an integer. Either compile fails, or esp8266 crashes.
What am I missing?

Dashboard inputs, helper number (works) and helper text (not working yet).
helper number helper text

You’d think these would be helpful:

Here’s an example for how to suspend deep_sleep using mqtt to check the state of something, before going back to sleep.

Go to time 5:25
If set, your device suspends sleep mode (then you can use OTA), if not, it goes right back to sleep. With my method or the template method (the same really) you can do this with api: easier than with mqtt. Program your device to check a switch or state of something before going back to sleep, then suspend sleep until you OTA update and then send it back to sleep.
This is also covered at:

So far, I’m able to do everything I want and need in api: and esphome.
No node red or mqtt yet needed. Not learned yet either!

Where are you bringing in the helper text? It needs to be under text_sensor: and not sensor:

If you are going to use helpers off the esp, it may be prudent to use globals. In the case that HA is not available it can store the last value or revert to a default if if all data is lost.

globals:
## pressure loops
  - id: count
    type: int
    restore_value: yes
    initial_value: '5' ## set a fallback value
## time pump is on start
  - id: start
    type: int
    restore_value: yes
    initial_value: '1200'

Set them under where you bring in the home assistant helpers

## time pump is on
  - platform: homeassistant
    id: pump_start
    internal: true
    accuracy_decimals: 2
    entity_id: input_number.pump_start
    on_value:
      - globals.set:
          id: start
          value: !lambda 'return id(pump_start).state * 1000;'
## pump repeat count
  - platform: homeassistant
    id: pump_count
    internal: true
    entity_id: input_number.pump_count
    on_value:
      - globals.set:
          id: count
          value: !lambda 'return id(pump_count).state;'

To call a global you don’t use state

delay: !lambda 'return id(start);'
1 Like

@Mikefila
Thanks for your help. Where do I put globals?

globals:
## pressure loops
  - id: count
    type: int
    restore_value: yes
    initial_value: '5' ## set a fallback value
in configuration.yaml?  in esp8266.yaml?

And something is missing above -platform

- platform: homeassistant
...

This would all go in the esp yaml file. You need to place each type of component under it’s respective heading.

globals:
    - global entities under this portion

binary_sensor: 
    - true or false / on and off entities here eg. input_booleans

sensor:
    - number entities here eg input_number

text_sensor:
    - entities that contain strings eg input_text

Any type of entity can go under globals, you define its type in the global

type of the global variable, for example bool (for true/false), int (for integers), float (for decimal numbers).

globals:
## pressure loops
  - id: count
    type: int     #### define type here
    restore_value: yes
    initial_value: '5' ## set a fallback value

The important part is that you add to the existing components that you brought over from HA. This automation that when it gets an update from home assistant it writes it to the global you create.

    on_value:
      - globals.set:
          id: count
          value: !lambda 'return id(pump_count).state;' ### the return id, is the id you set in the sensor above this

Am I misreading the docs?

Warning

If you enable MQTT and you do *not* use the “native API” for Home Assistant, you must remove the `api:` line from your ESPHome configuration, otherwise the ESP will reboot every 15 minutes because no client connected to the native API.

Care to clue me in? I’ve been trying to do this for the past three days.

Hey Steve you need to setup a template switch to turn off sleep mode.

switch:
  - platform: template
    name: "Turn off sleep"
    turn_on_action:
      - deep_sleep.prevent: deep_sleep_1
    turn_off_action:
      - deep_sleep.enter: deep_sleep_1

Now, I’m new and not an expert at any of this, but that does NOT say you cannot use both.
What it’s saying has nothing to do with MQTT.
It does say this:
If you don’t have (ie use) api: in your Home Assistant configuration.yaml file, then you should not put api: in the esphome yaml file. If you only have api: in esphome yaml file, it will reboot to try and regain connection to Home Assistant via api:
Simply have api: in both HA and esphome and that’s it. mqtt or not.

I did not know that there was a comm timeout that will reboot esp devices if they loose connection with HA.

UPDATE: You can disable this reboot timeout by adding reboot_timout: 0s under api: in your esp file.
" Can be disabled by setting this to 0s " See here…

I am far, far away from anything resembling an expert in HA or yaml. That’s why I have been fighting this for four days so far.

It appears that api: and mqtt: work together in ESPHome, but maybe someone with more experience can confirm this or not.

I will try to do this in the next week and get back to you, here. Did you see the video and page that shows how to do this in mqtt? I see no reason why the same thing couldn’t be done more simply with api: so I’ll do both and get back to you, since I have acquired all of the ‘pieces’ I need to do this.
@Mikefila has some great ideas on globals that will certainly help, so I need to explore that first because it covers the case of HA being offline/unreachable.

That would be appreciated as I have been trying many solutions over the past four days.

I stumbled across this custom component:

There is no readme file, and this is the total documentation:
“Custom ESPHome deep sleep component that’s controlled via MQTT (adjustable sleep duration, sleep enter trigger)”

@Mikefila
What goes just before - platform: template in your example ?
Where do I put this? HA or esphome? thx
Incomplete suggestions… I just don’t know what to do with, and lose countless hours trying to put pieces together.

I linked directly to the documentation, the link wasn’t even clicked. Had you clicked it you would have found your answer.

I’ve done it: I have control of deep sleep using api:
I can control if my feather huzzah esp8266 stays awake upon returning from deep sleep, and if it goes back to sleep, and for how long, all from the dashboard.
I’ve done it several ways, with and without using globals as suggested by @Mikefila.
I’ve done it with Input Number at the front end (int or float in configuration.yaml) and with Input Boolean (switch toggle on dashboard, added via Configuration - Automations - Helpers).

The hardest part is figuring out lambda conditions for different types that work, as “They are not evaluated by the editor” as you said. It takes an incredible amount of trial and error, searching online and testing to get the simplest of these to compile and work. I haven’t found a single source online that is is anyway helpful with lambdas or conditions re different data types, and it’s something you can’t even search the web for that doesn’t lead to esphome pages, which again have no helpful examples of how to evaluate different types of data.

The last piece I need is a way to post, push, or update a measured value from esphome to HA when it wakes up. I would like to verify this was done before going back to sleep. I also want to add in a safety: a way to keep it from sleeping by pulling a GPIO pin up or down. I already got trapped in a sleep loop once, and once this is installed in something remote, I will NOT want to uninstall it again to escape a sleep loop.

I will post my solutions here in the next week or two. There’s much to clean up before I can share, so thanks for your patience. Just wanted to let you know the good news.

NOTE: An esphome number template’s front end display format (slider or box) is determined by step: value being an integer or a float. It cannot be specified otherwise. I discovered this when using a number template locally in the esphome device .yaml (not recommended in this use case, as it is not accessible for alteration if the esphome device is sleeping).

number: 
- platform: template   
...
step: 1   
# will result in a SLIDER on the dashboard step: is a whole number / integer 

number: 
- platform: template   
...
step: 0.1
# will result in a BOX on the dashboard if step: is NOT a whole number /  integer 

Reference: Template Number — ESPHome