Automation: "global variables" and sensor states

Hi all,

I’m pretty new in HASS and I have some questions that you probably know so hope you can help me :slight_smile: I am getting the soil humidity value from an external device (ESP-12E) via mqtt and I would like to show if the soil is wet or dry depending on the voltage level and the previous states (hysteresis) instead of the value I’m getting (between 0V - 3,3V).
What I was thing is to create a “global variable” that will report the state of the soil but it will be also a variable that helps me to apply the hysteresis. Thinking in C it would be:

boolean soil_state = wet;

if ((soil_state == wet) && (sensor_value <= 1,1)):
soil_state = dry;
else if ((soil_state == dry) && (sensor_value >=2.):
soil_state = wet;

my current implementation of the sensor is:

platform: mqtt
state_topic: ‘homie/soil/data/adc_0’
name: ‘Soil’
unit_of_measurement: ‘V’
value_template: ‘{{ (value_json | float) | round(1) }}’

The firts thing I need is the “global variable”… how can I create it?
The second thing is the automation that would be:

automation:
alias: Soil status
initial_state: “global_variable”
trigger:
platform: mqtt
event: adc_0
condition:
??? here it will be the if-else if condition
action:
??? depending on the condition global_variable = wet or dry

I don’t know if I have explain myself properly :sweat: … if not, please ask me and I will try to clarify your doubts.

Thanks in advance,
Oscar

Not sure if this is exactly what you’re aiming for, but I use a slider as a “number holder” to remember the state of my front porch. This example shows how to set and retrieve the value:

An other solution (it is easier so personally i would go for that) is to do the «calculation» on the ESP and send another mqtt info for wet or not (then simply display it in hass with a template sensor). As the criterion for determining if the soil is wet should be constant and independent of anything else i dont think its absolutely nessecary to implement it in hass

1 Like

Yesss, that could work to me! Thanks for sharing, I will try it and let you know :wink:

You are right, that will be the easiest way but the problem is that the ESP-12E wakes up for a while to get the data and after that it goes to deep sleep, what means that ram memory is not powered so not possible to implement what you suggest.
But thanks anyway :+1:

You can put the value in the flash memory

Do you mean in the raspberry memory? Well, I think it would be better to storing it whitin HASS data. Maybe I am missing something :thinking:sorry

when your ESP aquire new data, before you send it to you can retrieve the old one from MQTT, perform your calculation, then update the new value and change the value for wet or not… That way, no memory trouble.

Yeah, it is a good option :clap: and it could be a good example to learn how to send data from hass and hoy to get it from my external device. Thanks

You can store variables in the ESP’s flash memory so that when it goes to sleep they can be recovered when it wakes up, search the arduino website, it’s something like …

EEPROM.write(0, 1);
x= EEPROM.read(0);