Force refresh sensor data from Home assistant to ESPHome

Hi,

I have a ESPHome display to show my current energy values.

Now I have the problem, that my battery chargers change from x to 0 W, when the charging is done. My display does not always got this update to 0 W. So the old value is still showing. It seems, that home assistant does not send a refresh of the values, because they are no more changing.

At the moment I try the timeout filter. But this will also switch to 0 W even the charger is on, when the value is not changed.

  - platform: homeassistant
    id: BatteryIn3
    entity_id: sensor.batterie_ladegerat_3_energy_power
    internal: true
    filters:
      - timeout:
          timeout: 120s
          value: 0

Can I force a refresh of the Home Assistant values to ESPHome every 120 seconds?

Are you using mqtt or the native api? The later should work with local push in both ways afaik and the expected behavior is the moment a value changes in HA it should be immediately pushed to the esphome node :thinking:

As you can see in the code above, I use the native API.

In 90% of the cases, the values are pushed to my ESP immediately. But in 10% they are lost in my network. I do not know why. Maybe because of bad WiFi of the ESP. This is normally not bad because when next value update works correct.
But if the value in home assistent do not change anymore, there is no refresh or repush of this value. If this value does not receive the ESP, it will always shown the old value. This is my issue.

With the timeout filter I can fix this behavior when it change to 0. But if it stays on one other value for more then 120 seconds (see my code above) it changes also to 0. I want to force push the value to the ESP even it do not change.

You should be able to figure this out easily. Monitor the wifi signal and api status of your esphome node to have an idea how bad (or hopefully good) your infrastructure is.

That’s indeed the expected behavior of local push.

One way to force the esphome node to receive the value again from HA is probably a restart :arrows_counterclockwise:

I wouldn’t go that far to restart the node. Seems a bit drastic. :laughing:

As a workaround you could setup a service on the node and an automation in HA, that runs every X minutes and calls that service. See the second example here.

Or you could just check on the node if the api is connected (or maybe re-connected) and react on that like sending the value with the above method or start a new poll for the state. Should work equally well. See here.

Or use the wifi_connected condition to see, if there are any connection problems. That would be here.

I think the first solution is the simplest. But how should the service to update the values look like? I did not find some examples.

All my values are created as sensors:

sensor:
  - platform: homeassistant
    id: Power
    entity_id: sensor.wechselrichter_active_power
    internal: true
    filters:
      - timeout:
          timeout: 120s
          value: 0
  - platform: homeassistant
    id: BatteryOut
    entity_id: sensor.hm_300_1_p_ac
    internal: true
    filters:
      - lambda: return id(BatteryOut2).state + id(BatteryOut3).state + x;
      - timeout:
          timeout: 120s
          value: 0
  - platform: homeassistant
    id: BatteryOut2
    entity_id: sensor.hm_300_2_p_ac
    internal: true
    filters:
      - timeout:
          timeout: 120s
          value: 0
  - platform: homeassistant
    id: BatteryOut3
    entity_id: sensor.hm_1500_p_ac
    internal: true
    filters:
      - timeout:
          timeout: 120s
          value: 0
  - platform: homeassistant
    id: BatteryIn3
    entity_id: sensor.batterie_ladegerat_3_energy_power
    internal: true
    filters:
      - timeout:
          timeout: 120s
          value: 0
  - platform: homeassistant
    id: BatteryIn2
    entity_id: sensor.batterie_ladegeraet_2_energy_power
    internal: true
    filters:
      - timeout:
          timeout: 120s
          value: 0
  - platform: homeassistant
    id: BatteryIn
    entity_id: sensor.batterie_ladegeraet_energy_power
    internal: true
    filters:
      - lambda: return id(BatteryIn2).state + id(BatteryIn3).state + x;
      - timeout:
          timeout: 120s
          value: 0
  - platform: homeassistant
    id: Verbrauch
    entity_id: sensor.smart_meter_active_power
    internal: true
    filters:
      - lambda: return id(Power).state + id(BatteryOut).state - id(BatteryIn).state - x;
      - timeout:
          timeout: 120s
          value: 0