I am an average programer and I need some help please.
I have a problem that I thought would be easy but it appears to be difficult to solve. ( or I am making it hard)
I have a water tank level meter
I have 5 inputs. Full, 3/4, 1/2, 1/4, 0
Due to corrosion concerns I turn the power on to the level sensor for 10 sec, get the level reading and then turn it off.
When the power is on to this system I need to take the value that is read, either 5, 4, 3, 2, 1 and save it as a variable.
This is what I am struggling with.
I am using and ESPHome device to deliver these levels to HA.
I have tried an automation but I cannot get the value to stick.
Here is my code
When the switch.water_relay = ON
Then I need the sensor.water_tank_level value to be saved to sensor.water_tank_level_store_2
automation:
alias: âWater tank Store 3â
trigger:
platform: state
entity_id: switch.water_relay
to: âonâ
action:
state: "{{ states('sensor.water_tank_level') | int }}"
So putting that all together:
template:
- trigger:
- platform: state
entity_id: switch.water_relay
to: 'on'
sensor:
- name: Water Level
state: "{{ states('sensor.water_tank_level') | int }}"
unit_of_measurement: ' ' # if you want a history graph
We could get fancy and convert that to % full if you want.
Good question.
When the Relay is off, the sensor.water_tank_level = 5 ( because there is no power to energise the sensors)
When the relay is on it gives the real reading for the tank level. Currently 3.
Therefore when the relay is on, it reads the true value of the tank level.
Because the sensor.water_tank_level changes when the relay ( power) is of, I need to store that value some how.
I need to do this but I do not know how.
I need a funtion that does this
When the relay is ON, sensor.water_tank_level_store_2 = sensor.water_tank_level
Then when the relay goes of, sensor.water_tank_level_store_2 retains the level value.
If that does not work we can use this in the template:
template:
- trigger:
- platform: state
entity_id: switch.water_relay
from: 'off'
to: 'on'
for: 1
sensor:
- name: Water Tank Level Store 2
unit_of_measurement: '%'
state: >
{% set level = states('sensor.water_tank_level') | int %}
{% if is_state('switch.water_relay', 'on') %}
{% if level == 5 %}
100
{% elif level == 4 %}
75
{% elif level == 3 %}
50
{% elif level == 2 %}
25
{% elif level == 1 %}
0
{% else %}
unknown
{% endif %}
{% else %}
{{ states('sensor.water_tank_level_store_2') }}
{% endif %}
But we really shouldnât have to (it triggers when the relay is on, so the âif relay onâ test should not be required. If this is the only thing that works we need to open an issue.
Thanks for your help but no luck sorry.
I still cannot keep that value for sensor.water_tank_level after the relay is off.
When the relay is on, it reads fine ( i see a value of 3 ) but when the relay is off it goes back to 5.
I am lost
I was hoping I could do something like this
When the relay is ON, sensor.water_tank_level_store_2 = sensor.water_tank_level
I have tested and use this format many times, though admittedly not with the new template format. Lets try the legacy format, put this under your sensor: definition in configuration.yaml, or in your sensor.yaml file if you use !includes.
I inserted the content above into the sensor.yaml and ( checked config) restarted
The sensor.water_tank_level_store_2 now continually says , unknownâŚ