Repeatedly publish MQTT data even if no update

I am sending out sensor data using MQTT. I am using an automation to do this - is that the correct thing to do? Or should it be in my config.yaml?

Either way, I would like to publish the MQTT data not only on sensor update, but also every 30 seconds (this can be in addition to the state change triggered updates) - even if there is no update to the sensor value (ie it is fine to repeat the old value).

Ideas how to do that? Thanks!

alias: "MQTT: Phase 2 Watts to mqtt"
description: phase2mqtt Watts
trigger:
  - platform: state
    entity_id:
      - sensor.esp8266_modbus_phase_2_current_watts
condition: []
action:
  - service: mqtt.publish
    data:
      topic: phase2mqttWatts
      payload: "{{ trigger.to_state.state }}"
mode: single
trigger:
  - time_pattern:
    seconds: "/30"
alias: "MQTT: Phase 2 Watts to mqtt"
description: phase2mqtt Watts
trigger:
  - platform: state
    entity_id:
      - sensor.esp8266_modbus_phase_2_current_watts
  - time_pattern:
    seconds: "/30"
condition: []
action:
  - service: mqtt.publish
    data:
      topic: phase2mqttWatts
      payload: "{{ trigger.to_state.state }}"
mode: single
1 Like

Missing the :

updated :slight_smile:

Thanks very much!

Um missing the platform too.

  - platform: time_pattern
    seconds: "/30"
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.


What if you enable LWT ?

If i enable LWT the value of power sensor is stack, also created new topic with LWT. But as far as i know LWT can be useful only when network disappeared or client disconnected from network. But when my electricity meter turned off, and esp32 with mqtt keeps alive, just no value incoming. And i need, when no new value, just set sensor to 0.


You need to find a way to reliable detect when your electricity meter is turned off. Maybe it is more a question for EspEasy ?