Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTT sensor timestamp extraction #33742

Closed
ievren opened this issue Jul 13, 2024 · 3 comments
Closed

MQTT sensor timestamp extraction #33742

ievren opened this issue Jul 13, 2024 · 3 comments

Comments

@ievren
Copy link

ievren commented Jul 13, 2024

Feedback

Title: Clarification Needed on Extracting Timestamps from MQTT Sensors

Description:

I don't understand how to extract the timestamp from a sensor, and it's not very clear to me based on the current documentation.

There is an example explaining processing timestamps, but I am not 100% sure if my approach is correct:

I would appreciate it if there were a clear example of how to handle a scenario where a sensor wakes up and sends multiple time points. Is this not a common approach (for battery saving)?

Here's my specific case:

  1. Topic: enviro/enviro-urban
  2. Message payload:
    {
      "readings": {
        "pressure": 964.46,
        "pm1": 7,
        "pm2_5": 9,
        "noise": 0.036,
        "humidity": 31.65,
        "temperature": 31.53,
        "pm10": 10
      },
      "nickname": "enviro-urban",
      "model": "urban",
      "uid": "e66164084356202d",
      "timestamp": "2024-07-13T21:44:07Z"
    }

To handle this, I manually added the following section to configuration.yaml using the file editor:

mqtt:
  sensor:
    - name: "Enviro Urban Temperature"
      state_topic: "enviro/enviro-urban"
      unit_of_measurement: "°C"
      value_template: "{{ value_json.readings.temperature }}"
      json_attributes_topic: "enviro/enviro-urban"
      json_attributes_template: "{{ {'timestamp': value_json.timestamp} | tojson }}"
      expire_after: 3600
      force_update: true
    
    - name: "Enviro Urban Humidity"
      state_topic: "enviro/enviro-urban"
      unit_of_measurement: "%"
      value_template: "{{ value_json.readings.humidity }}"
      json_attributes_topic: "enviro/enviro-urban"
      json_attributes_template: "{{ {'timestamp': value_json.timestamp} | tojson }}"
      expire_after: 3600
      force_update: true

    - name: "Enviro Urban Pressure"
      state_topic: "enviro/enviro-urban"
      unit_of_measurement: "hPa"
      value_template: "{{ value_json.readings.pressure }}"
      json_attributes_topic: "enviro/enviro-urban"
      json_attributes_template: "{{ {'timestamp': value_json.timestamp} | tojson }}"
      expire_after: 3600
      force_update: true
    
    - name: "Enviro Urban Noise"
      state_topic: "enviro/enviro-urban"
      unit_of_measurement: "V"
      value_template: "{{ value_json.readings.noise }}"
      json_attributes_topic: "enviro/enviro-urban"
      json_attributes_template: "{{ {'timestamp': value_json.timestamp} | tojson }}"
      expire_after: 3600
      force_update: true
    
    - name: "Enviro Urban PM1"
      state_topic: "enviro/enviro-urban"
      unit_of_measurement: "µg/m³"
      value_template: "{{ value_json.readings.pm1 }}"
      json_attributes_topic: "enviro/enviro-urban"
      json_attributes_template: "{{ {'timestamp': value_json.timestamp} | tojson }}"
      expire_after: 3600
      force_update: true

    - name: "Enviro Urban PM2.5"
      state_topic: "enviro/enviro-urban"
      unit_of_measurement: "µg/m³"
      value_template: "{{ value_json.readings.pm2_5 }}"
      json_attributes_topic: "enviro/enviro-urban"
      json_attributes_template: "{{ {'timestamp': value_json.timestamp} | tojson }}"
      expire_after: 3600
      force_update: true
    
    - name: "Enviro Urban PM10"
      state_topic: "enviro/enviro-urban"
      unit_of_measurement: "µg/m³"
      value_template: "{{ value_json.readings.pm10 }}"
      json_attributes_topic: "enviro/enviro-urban"
      json_attributes_template: "{{ {'timestamp': value_json.timestamp} | tojson }}"
      expire_after: 3600
      force_update: true
    
    - name: "Enviro Urban Voltage"
      state_topic: "enviro/enviro-urban"
      unit_of_measurement: "V"
      value_template: "{{ value_json.readings.voltage }}"
      json_attributes_topic: "enviro/enviro-urban"
      json_attributes_template: "{{ {'timestamp': value_json.timestamp} | tojson }}"
      expire_after: 3600
      force_update: true

It may be related to this discussion:

Would it be possible to get a clear example or further guidance on this?

URL

https://www.home-assistant.io/integrations/sensor.mqtt/

Version

2024.7.2

Additional information

No response

@home-assistant
Copy link

Hey there @emontnemery, @jbouwh, @bdraco, mind taking a look at this feedback as it has been labeled with an integration (mqtt) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of mqtt can trigger bot actions by commenting:

  • @home-assistant close Closes the feedback.
  • @home-assistant rename Awesome new title Renames the feedback.
  • @home-assistant reopen Reopen the feedback.
  • @home-assistant unassign mqtt Removes the current integration label and assignees on the feedback, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information) to the feedback.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information) on the feedback.

@jbouwh
Copy link
Contributor

jbouwh commented Jul 14, 2024

I would not recommend using json attributes with changing values on every update, and neither, I would encourage not to set force_update. Both will have a negative impact on the performance as all entities will enforce a state write when an update is received. I you want to have a timestamp in a sensor, I suggest to add a timestamp sensor for it instead.
Assign timestamp as the sensor's device_class.

For the question on multiple time points of data in the past:
Home Assistant assumes incoming sensor data is actual data. I do not know of any support to import a time series of data into Home Assistant yet. You could consider using Influx DB to import/store such data.

@jbouwh jbouwh closed this as not planned Won't fix, can't repro, duplicate, stale Jul 14, 2024
@jbouwh jbouwh reopened this Jul 14, 2024
@ievren
Copy link
Author

ievren commented Jul 15, 2024

@jbouwh, thank you for your answer.

Thank you for clarifying this. It was not clear to me that Home Assistant assumes incoming sensor data is actual data. We can consider this matter resolved

@ievren ievren closed this as completed Jul 15, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Aug 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants