How to save a Variable, triggered by an event

FIXED

Sorry
My error with the binary_sensor.level’s that I did not share with you in the start.

I was blinded to this silly error.
Thank you for you patients.

- platform: template
    sensors:
        water_tank_level_store_2:
          friendly_name: 'Water Tank Level Store 2'
          unit_of_measurement: '%'
          value_template: >
              {% set level = states('sensor.water_tank_level') | int %}
              {% if is_state('switch.water_relay', 'on') %}
                {% if is_state('binary_sensor.level_5', 'off') %}
                  100
                {% elif is_state('binary_sensor.level_4', 'off') %}
                  75
                {% elif is_state('binary_sensor.level_3', 'off') %}
                  50
                {% elif is_state('binary_sensor.level_2', 'off') %}
                  25
                {% else %}
                  0
                {% endif %}
              {% else %}
                {{ states('sensor.water_tank_level_store_2') }}
              {% endif %}

I can’t tell what the error was as you have not formatted your post correctly.

how do you format the code ?

Three backticks ``` above and below your pasted code (on their own line).

Or highlight the whole config section and press the <\> button at the top of your post edit window.

You got it. And I see your mistake now.

Ok, so there is an issue with the new template format. I’ll have a look and see if anything like this has been reported.

From the ESP Home I get the data like this

#      Level 1 = Less Than 1/4 Tank
#      Level 2 = 1/4 Full
#      Level 3 = 1/2 Full
#      Level 4 = 3/4 Full
#      Level 5 = Full

#sensor:
#  - platform: template
#    name: "water_tank_level_store"
#    id: water_tank_level_store

binary_sensor:
  - platform: gpio
    pin: D2
    name: "Level_2"
    id: level_2
    device_class: opening
    
  - platform: gpio
    pin: D7
    name: "Level_3"
    id: level_3
    device_class: opening
    
  - platform: gpio
    pin: D5
    name: "Level_4"
    id: level_4
    device_class: opening
    
  - platform: gpio
    pin: D6
    name: "Level_5"
    id: level_5
    device_class: opening
    
switch:
  - platform: gpio
    name: "water_relay"
    id: relay
    pin:
      number: D1
      inverted: False
      #mode: INPUT_PULLUP

Then in the Sensor.yaml file I have this

  - platform: template
    sensors:
        water_tank_level_store_2:
          friendly_name: 'Water Tank Level Store 2'
          unit_of_measurement: '%'
          value_template: >
              {% set level = states('sensor.water_tank_level') | int %}
              {% if is_state('switch.water_relay', 'on') %}
                {% if is_state('binary_sensor.level_5', 'off') %}
                  100
                {% elif is_state('binary_sensor.level_4', 'off') %}
                  75
                {% elif is_state('binary_sensor.level_3', 'off') %}
                  50
                {% elif is_state('binary_sensor.level_2', 'off') %}
                  25
                {% else %}
                  0
                {% endif %}
              {% else %}
                {{ states('sensor.water_tank_level_store_2') }}
              {% endif %}

image

You know you could do all this in ESPHome?

Yes, I do now.

I have being doing some other things in ESPhome since I created this.
I have discovered I can do a lot in ESPHome.

Thanks for the pointer. I appreciate it.
Sometimes it take a nudge from someone with more experience for us to look more deeply into other devices.
I may push all of this processing into ESPHome

Your gem for me was the last line

              {% else %}
                {{ states('sensor.water_tank_level_store_2') }}
              {% endif %}

I never thought to add this. Thanks

1 Like