Change scan_interval for MQTT sensor?

Hi there,

I was wondering if it’s possible to change the scan_interval to something like 20 seconds for an MQTT sensor?

My sensor looks like this:

- platform: mqtt
  state_topic: "tele/greenhouse/SENSOR"
  name: "Greenhouse Temperature"
  qos: 0
  unit_of_measurement: "°C"
  value_template: '{{ value_json.AM2301.Temperature }}'
  scan_interval: 20

This doesn’t seem to have any effect on the sensor in my Home Assistant view - it still shows “7 minutes ago”. Now, the temperature might be the same as it was seven minutes ago. But I would have expected it to say no more than “20 seconds ago”.

Is this possible with the MQTT sensor?

Thanks!

Chris

I have a couple of temperature and humidity sensors integrated via MQTT, and experience a similar behaviour. I appears that HA does not record a change if the new value is the same as the previous value, hence HA won’t update that timestamp.

Ah, that’s interesting. I guess I need to do some tests of different scan_interval values and seeing how Home Assistant reacts to rapidly changing temperature.

In the sensor configuration, try adding “force_update: true”. This should force a republish even if the data has not changed. I have found this useful so that I can see a regular “heartbeat” of messages and spot a problem if they go missing.

3 Likes

Thanks for the reply, but that doesn’t seem to have made a difference :frowning:

- platform: mqtt
  state_topic: "tele/greenhouse/SENSOR"
  name: "Greenhouse Temperature"
  qos: 0
  unit_of_measurement: "°C"
  value_template: '{{ value_json.AM2301.Temperature }}'
  scan_interval: 20
  force_update: true

OK, to clarify, I dont use scan_interval but rather use force_update. I then make sure that my mqtt device firmware is configured to republish on a regular basis i.e. 20 seconds. This gives me a regular “heartbeat” of mqtt messages updating HA regardless of whether the state value has changed or not. So this is a different approach.

For reference, in the docs:

force_update - If True, a state change will be triggered anytime the state property is updated, not just when the value changes

So the device state is updated every time a new mqtt message is generated regardless of whether the value has changed or not. But this is only useful if your device keeps publishing mqtt messages on a regular basis.

What mqqt client are you using for this device? I am using Sonoff-Tasmota on a lot of my devices and you can set the device to republish tele messages every, say, 20 seconds. This is on the Configuration Logging menu item but other software should also let you set this up in different ways.

If you are using mosquitto as the mqtt broker in HA then you can check the message stream to see if the device is republishing regularly using something like:

mosquitto_sub -v -h localhost -u mqtt_user -P mqtt_password -t "tele/greenhouse/#"

3 Likes

Aha, I understand what you mean now! Thank you very much for the detailed info.

I am also using Sonoff-Tasmota - I will test what you suggest when I get home.

Chris

Super - it’s all working now!

The option I required in the Sonoff-Tasmota firmware (v5.4.0) was Configuration > Configure Logging > Telemetric period (300), which I changed from the default value of 300 to 20 seconds.

My sensors now update in Home Assistant as expected.

Thanks for your help!

2 Likes

Hi guys, i have resolved usign this command on tasmota consolle:

TelePeriod 0 = disable telemetry messages
1 = reset telemetry period to firmware default ( TELE_PERIOD )
10..3600 = set telemetry period in seconds (default = 300 )

Can someone assist me with this please.
I am running HassOS on a Raspi4, so I have installed the standard mosquitto mqtt broker.
I too would like to auto refresh the data even if it doesnt change, but mosquitto is self configuring - which I like, but I want one of the sensors to auto update (force_update) more often.
I have the correct settings in my tasmota device and its updating every thirty seconds.
What would I have to enter into a yaml file to get this to happen?
Any help appreciated.
Rob

Forget it. Having a moment!
For those unsure about it as I was, just go ahead and add a manual mqtt entry into your configuration file for the sensor that may have already got an automatic integration. You can then grab whatever output you need using value_templates. My setup below. (I added the mqtt server details even though im not sure its necessary).

mqtt:
  broker: 192.168.1.7

sensor:
  - platform: mqtt
    name: "Bedroom Temperature"
    state_topic: "tele/TempSens1/SENSOR"
    value_template: "{{value_json['ENERGY'].Power }}"
    force_update: true

  - platform: mqtt
    name: "Bedroom Humidity"
    state_topic: "tele/TempSens1/SENSOR"
    value_template: "{{value_json['ENERGY'].Current }}"
    force_update: true

Hi, I was also wondering how to increase the frequency of data refresh and data sharing via Zigbee2MQTT on home assistant.
In particular, for the temperature sensor: Aqara TVOC air quality monitor