I have an ESP32 controlling all aspects of my koi pond.
I have a few timers and variables to manage water changes, water levels etc.
Changing these will be much faster and more user friendly if I could pass a value from a HA slider to a global variable in ESPHome.
My thinking is to:
Create an input_number in HA.
Convert the integer to text.
Receive it into ESPHome using HomeAssistant text sensor.
Convert text to integer.
Set ESPHome global variable to integer value.
(2.) and (4.) are the ones Iām struggling with.
Is there a way to go straight from (1.) to (5.), avoiding the conversions?
Can this be done? If so how?
Any suggestions?
Itās amazing how posting the problem changes my thinking and solves the problem. Hereās the solution below for anyone else with this issue.
In HA, define your input numbers:
koi_water_change:
name: Water change litres
icon: mdi:water
initial: 1000
min: 0
max: 1200
step: 100
drum_wash_timer:
name: Drum wash time
icon: mdi:timer
initial: 5
min: 0
max: 20
step: 1
spindrifter_off_timer:
name: Spindrifter off timer
icon: mdi:timer
initial: 5
min: 0
max: 20
step: 1
Next, define sensor template using the input_numbers:
- platform: template
sensors:
koi_water_change:
value_template: '{{ states.input_number.koi_water_change.state | int }}'
drum_wash_timer:
value_template: '{{ states.input_number.drum_wash_timer.state | int }}'
spindrifter_off_timer:
value_template: '{{ states.input_number.spindrifter_off_timer.state | int }}'
Lastly, in ESPHome, define the homeassistant sensors:
- platform: homeassistant
name: "Water change litres"
entity_id: sensor.koi_water_change
- platform: homeassistant
name: "Drum wash timer"
entity_id: sensor.drum_wash_timer
- platform: homeassistant
name: "Spindrifter off timer"
entity_id: sensor.spindrifter_off_timer
Then use the sensor in your ESPHome automation:
- lambda: |-
delay(id("Spindrifter off timer")*60);
6 Likes
Seems I was a little hasty:
The following compiles fine but when called has a delay of 0 seconds. When called again, the device stops responding.
The āSpindrifter off timerā sensor has the correct value (it can be seen in the webserver.
Any ideas?
patfelst
(Patrick)
April 5, 2020, 5:38am
4
because youāve put delay in a lambda, youāre actually calling the C++ delay function, which takes milliseconds as a parameter. Thatās probably why it seems so short. Iām not sure this is a good idea in ESPHome code. You may actually be crashing your ESP32 as delays interfere with ESP watchdog timers and WiFi processing.
Maybe try coding it in yaml, i.e. using an ESPHome delay . Note the ESPHome delay is a non-blocking, i.e. other code will run in the background.
Thanks. Yes, youāre correct there. It is crashing it.
I was using the delay in initial testing and all is fine, but I need to be able to adjust the length of the delay from HA, and that is where I am stuck.
The values are being passed from HA to ESPhome OK, I just canāt find a way to assign them as variables to use in the delay.
Any ideas?
patfelst
(Patrick)
April 5, 2020, 9:52am
6
Just give your home assistant sensors an ID, e.g. for your drum wash timer
sensor:
- platform: homeassistant
name: "Drum wash timer"
entity_id: sensor.drum_wash_timer
id: my_drum_wash_timer
then you can refer to it in an action
on_...:
then:
- switch.turn_on: relay_1
- delay: !lambda 'return id(my_drum_wash_timer).state;'
- switch.turn_off: relay_1
2 Likes
Thank you, Pat. I tried that already with no success. Iāll certainly give it another go.
With the delay like that, does it evaluate as seconds?
The docs say it has to have units like - delay: 2s, - delay: 2m. I think that is where it is failing.
patfelst
(Patrick)
April 5, 2020, 10:00am
8
oh yeah, sorry, when delay is templated like this, it assumes milliseconds, so assuming your delays are in seconds, multiply by 1000 as per the example in the docs , so
on_...:
then:
- switch.turn_on: relay_1
- delay: !lambda 'return id(my_drum_wash_timer).state * 1000;'
- switch.turn_off: relay_1
4 Likes
Brilliant! Thatās the piece of info Iāve been missing all along.
Iāll give it a try now.
ashscott
(Ash)
April 5, 2020, 10:25am
10
patfelst:
delay: !lambda āreturn id(my_drum_wash_timer).state * 1000;ā
Genius! It works. Thank you so much.
1 Like
patfelst
(Patrick)
April 5, 2020, 10:26am
11
thatās great. Please mark as solution!
1 Like
Petos
(Petos)
April 19, 2020, 1:01pm
12
@ashscott Hi, could you send new config?
I have same issue. I need set one variable from both sides - HA and ESP.
ashscott
(Ash)
April 19, 2020, 1:04pm
13
Sure.
What specifically, do you need?
Petos
(Petos)
April 19, 2020, 5:13pm
14
I still donāt know how to pass value to ESP and back to HA when i change value in ESP.
ashscott
(Ash)
April 19, 2020, 5:27pm
15
You just use:
delay: !lambda āreturn id(my_drum_wash_timer).state * 1000;ā
to get it into ESPHome.
A value coming back the other way is just reading the sensor or counter etc in HA.
Petos
(Petos)
April 19, 2020, 5:35pm
16
I use values for different purpose. I donāt need delay. I need set temperature on both sides (HA & ESP).
Itās not general thermostat. Itās solar panel controller (hot water system).
I need set remotely and localy difference temperature for turn on the pump.
Could you refresh your second post in this thread?
ashscott
(Ash)
April 19, 2020, 6:09pm
17
Apart from the delay, itās all identical.
Is this the sort of thing you are looking for:
- platform: template
name: "Boiler running"
id: boiler_running
lambda: |-
if (id(boiler_output_temperature).state > 30) {
return true;
} else {
return false;
}
on_press:
then:
switch.turn_on: boiler_on_led
You could substitute the ā30ā for a sensor value derived from an input_number, as in my post #2 .
Have a read of this . You can then see the template sensor you publish to in HA.
1 Like
@ ashscott Thank you! That is excactly what I was struggling with.
Heya
Wrote a little instructable with a little project I done passing variables from home aasistant to esphomehttps://www.instructables.com/id/ESPHOME-SONOF-S26-Timed-Light/
1 Like
laca75tn
(My Home)
May 14, 2020, 1:35pm
20
Can you pass a value from one esphome node to another?
I want to set the value of a binary_sensor on esphome node1 based on a value from esphome node2
Thanks in advance for your help