Need to set MQTT values to 0 after sensor loss

Hi, I´m using a smartmaic energymeter (HACS integration) via MQTT on a location where sometimes to power is switched off. I’m searching now for a possibility to set the values to 0 if within 2minutes no new values are received. After a sensor loss HA is using the last received valid data whithout any expire-time…
Tried to insert an expire_after in developer tools/states but without success.
Any idea from you how to get that solved? I’m new to HA btw

state_class: measurement
unit_of_measurement: A
device_class: current
expire_after: 60
friendly_name: D103_2000A_1 Current 1

expire_after goes into the definition of the MQTT sensor.
If you don’t define it yourself but use the discovery feature, you have to check on the sending side if you can enable that feature when the “discovery” mqtt topic is set.

In MQTT, the usual way a failure or disconnect is detected is with a Last Will and Testament (LWT) which the broker uses if there is no communication within a keepAlive timer:

Sending a LWT needs config on the client, so there is another way in the HASS MQTT Sensor:

Search is your friend - just expect to need several attempts as you learn better and better search terms and start to understand the features you’re homing in on.

If this helps, :heart: this post!

1 Like

We meet again in a MQTT-related question thread! :grin: :man_mage:

Good to see we both homed in on expire_after - hope it solves the query.

On the sending side (the smartmaic meters) you have only limited possibilities. I see no chance to insert there some expire feature.

That LWT is exactly what I need to get implemented, but how to do? I´m using the core-mosquitto integration in HA.

Once again, it’s the device that must implement LWT.

Hi Koying,
understood now LWT and asking if anybody has implemented LWT to the smartmaic devices or can give me information how to implement that.

If that is not possible with that devices maybe some workaround is possible. HA knows when it received the last data, so checking that and set the data to zero could be an ugly but working solution. But I don’t know how to do that.

Seems like a web-page to configure the mqtt settings. I wonder if you could ping it from HA.
if so, if the ping sensor goes offline for 2 Minutes, you could publish a 0 value

I’m able to ping the device. Would you be so kind so to mention where exactly I can create that code to ping and post the MQTT?


and publishing an mqtt message is simply an action, example

  - data:
      payload: '14163857'
      topic: home/OpenMQTTGateway/commands/MQTTtoSRFB
    service: mqtt.publish

1 Like

That solved the problem. Created an automation which publishes at ping->disconnect the MQTT in the topic of that meter a payload with zero values for the voltage an current.
In parallel I contacted the creator of the integration. So maybe an LWT functionality will be implemented in future.

1 Like

Hi all. I have a bit different problem. How to set value for Power sensor of Energy meter to 0, if no value changes for certain period?I have energy meter before my Solar inverter, and when grid outage is happen my esp32 on Easy Mega platform, keeps pushing last value. So values keep still. After grid back sensor continue to measure. Would be appreciated for any help and hints.