Send sensor value to HA on wifi reconnect

I have an ESP8266 on my balcony which I use to water my plants automatically with a pump. I also have a sensor in Home Assistant to keep track of the last time the plants were watered. Everything works fine when the ESP8266 is connected to the WiFi, the issue arises when the blinds are closed and it disconnects, as I assume the pump gets turned on anyway at the correct time by the cron job but it isn’t reflected on the HA sensor.

I have tried as a test to force an update to a template binary sensor (pompa_attivata_oggi) as soon as the wifi is (re)connected but it doesn’t work, what am I doing wrong?

Esphome configuration:

esphome:
  name: esphome-balcone
  friendly_name: esphome-balcone

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  platform: esphome

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  power_save_mode: none

  on_connect:
    - binary_sensor.template.publish:
        id: pompa_attivata_oggi
        state: !lambda 'return id(pompa_attivata_oggi).state;'

captive_portal:

time:
  - platform: homeassistant
    id: homeassistant_time
    on_time:
      - cron: '0 0 8,21 * * *'
        then:
          - binary_sensor.template.publish:
              id: pompa_attivata_oggi
              state: OFF
          - if:
              condition:
                and:
                  - api.connected:
                  #compare value from sensor with threshold value, it is more complicated that "on_range" bcs it also triggers now when the value didnt changed
                  - lambda: 'return (id(pioggia_caduta).state + id(pioggia_cadente).state < id(soglia_pioggia).state) && id(ultrasonicsensor).state > 0.05;'
              then:
              - switch.turn_on: pump1
              - delay: !lambda 'return id(tempo_di_annaffiatura).state * 1000;'
              - switch.turn_off: pump1
              - homeassistant.service: 
                  service: input_boolean.turn_on
                  data:
                    entity_id: input_boolean.piante_annaffiate
              - binary_sensor.template.publish:
                  id: pompa_attivata_oggi
                  state: ON
              else:
                - if:
                    condition:
                      #compare value from sensor with threshold value, it is more complicated that "on_range" bcs it also triggers now when the value didnt changed
                      - lambda: 'return id(ultrasonicsensor).state > 0.05;'
                    then:
                    - switch.turn_on: pump1
                    - delay: !lambda 'return 120 * 1000;'
                    - switch.turn_off: pump1
                    - binary_sensor.template.publish:
                        id: pompa_attivata_oggi
                        state: ON

sensor:
  - platform: ultrasonic
    trigger_pin: 13
    echo_pin: 12
    name: "Ultrasonic Sensor"
    id: "ultrasonicsensor"
    accuracy_decimals: 3
    filters:
    - median: 
    - lambda: return (0.24 - x);

  - platform: homeassistant
    name: "Soglia pioggia"
    entity_id: input_number.soglia_ml_di_pioggia
    id: soglia_pioggia
    accuracy_decimals: 0

  - platform: homeassistant
    name: "Pioggia nelle prossime ore"
    entity_id: sensor.pioggia_nelle_prossime_ore
    id: pioggia_cadente
    accuracy_decimals: 2

  - platform: homeassistant
    name: "Pioggia nelle ultime ore"
    entity_id: sensor.pioggia_nelle_ultime_ore
    id: pioggia_caduta
    accuracy_decimals: 2

  - platform: homeassistant
    name: "Tempo di annaffiatura"
    entity_id: input_number.tempo_di_annaffiatura
    id: tempo_di_annaffiatura
    accuracy_decimals: 0

switch:
  - platform: gpio
    pin: 14 #D5
    name: "pumping"
    id: pump1
    restore_mode: ALWAYS_OFF
    icon: "mdi:water-pump"

binary_sensor:
  - platform: template
    name: "Pompa attivata oggi"
    id: pompa_attivata_oggi

HA configuration:

 trigger:
  - platform: state
    entity_id: binary_sensor.esphome_balcone_pompa_attivata_oggi
    to:
      - "on"
  - platform: state
    entity_id: switch.esphome_balcone_pumping
    to:
      - "on"
  sensor:
  - name: "Ultima annaffiatura"
    icon: mdi:clock
    state: "{{ as_timestamp(now()) | timestamp_custom('%d/%m/%y alle %H:%M') }}"

I would expect that esphome reboots after 15min when loosing connection to wifi and HA.

1 Like

It shouldn’t really disconnect just because a blind closes.

It’s also far from the router, but I don’t want to spend money on a repeater just for this small thing (I only close the blinds at night), since everything else works.

I also think it’s an interesting matter of discussion not only for my specific use case, but for every situation in which connection is not 100% reliable. I have looked at the reboot issue as @Karosm suggested and will report back as soon as I can.

1 Like

Mqtt might work better for this.

I’m not really sure, but you’re using a binary sensor template and you only defined its state to be updated according to cron.

In case of disconnection, the esp will restart in 15 minutes and when starting the sensor will not have a defined state.

From what I understand, you want to know if during a disconnection the pump was activated, I would create a Boolean variable to store the state of the sensor, or I would create a template text to store the time it was activated and a global variable to retrieve the state in the case of a restart.

Another thing would be to set the reboot_timeout to 0s (although according to the documentation this is not very recommended) or to a few minutes after you open the blinds , which from what you described, the esp reconnects the network.

1 Like

IMHO after WiFi connects some time needed to API to be connected.
Try to set sensor state on API connected.

1 Like

You are facing some network interruption by whatever reason. Are you able to mount an external antenna to your ESP8266? Is the range fine when you test in your house? There could be a broken circuit antenna. When it’s a cheap device replacement?

Beside that, is it a single device, could you replace at least for testing with tasmota and MQTT? When MQTT messages are retained the reconnect shouldn’t bother. Even for testing it could be interesting to see if tasmota has same disconnect issues. It could be a bug or setting in the esp source code as well.

I put together the reboot_timeout and on_client_connected suggestions and it works perfectly fine now!

The relevant changes in the final esphome config (with the timestamp instead of the binary sensor):

api:
  reboot_timeout: 0s
  on_client_connected:
    - sensor.template.publish:
        id: ultima_attivazione
        state: !lambda 'return id(ultima_attivazione).state;'
- sensor.template.publish:
    id: ultima_attivazione
    state: !lambda 'return id(homeassistant_time).now().timestamp;'
sensor:
  - platform: template
    name: "Timestamp ultima attivazione pompa"
    id: ultima_attivazione

HA config:

- trigger:
  - platform: state
    entity_id: sensor.esphome_balcone_timestamp_ultima_attivazione_pompa
    not_to:
      - "unknown"
      - "unavailable"
  sensor:
  - name: "Ultima annaffiatura"
    icon: mdi:clock
    state: "{{ states(trigger.entity_id) | int | timestamp_custom('%d/%m/%y alle %H:%M') }}"

Thank you all for replying, especially @Karosm, @walberjunior and @Masterzz. I wish I could mark multiple answers as solutions!

1 Like