Rest sensor with timestamp and value

I’d like to create a rest sensor for json values such as {“timestamp”:1676502393000,“power”:153}.
The device is documented to only update the value every 5 minutes so when HA will query the value
it will likely correspond to a datetime in the past 5min (at most).

My configuration file contains this:

rest:
  - scan_interval: 300
    resource: http://192.168.1.10/power_meter
    sensor:
      - name: "Device Power"
        device_class: "apparent_power"
        state_class: "measurement"
        unit_of_measurement: "VA"
        value_template: "{{ value_json.power }}"

This works but does not use the timestamp (unix epoch time in ms).
How can I set the timestamp of a sensor value ?

1 Like

Easy way, having the power as an attribute and the timestamp as the state of the sensor:

</s> <s>rest:</s> <s> - scan_interval: 300</s> <s> resource: http://192.168.1.10/power_meter</s> <s> sensor:</s> <s> - name: "Device Power"</s> <s> device_class: "apparent_power"</s> <s> state_class: "measurement"</s> <s> unit_of_measurement: "VA"</s> <s> value_template: "{{ now() }}"</s> <s> json_attributes:</s> <s> - power</s> <s>

If you prefer to have in the way you mentioned please let me know…

Sorry, just realized you wanna use the result as an apparent_power

I’d swap them around: you’re showing 1.67TVA of power with that unit_of_measurement and device_class.

        value_template: "{{ value_json.power }}"
        json_attributes:
          - timestamp
1 Like

The timestamps on the power entity will only show the last time the entity had a state change.

You can create a second entity that outputs the timestamp.

rest:
  - scan_interval: 300
    resource: http://192.168.1.10/power_meter
    sensor:
      - name: Device Power
        device_class: apparent_power
        state_class: measurement
        unit_of_measurement: VA
        value_template: "{{ value_json.power }}"

      - name: Device Timestamp
        device_class: timestamp
        value_template: "{{ (value_json.timestamp / 1000) | as_datetime | as_local }}"
1 Like

Would need to divide by 1,000 — OP’s timestamp is in ms:

value_template: "{{ (value_json.timestamp|float(0) / 1000) | as_datetime | as_local }}"

1 Like

yep, i’ll update that

thank you for your help but it looks like I wasn’t clear enough.
I don’t want to create another sensor for timestamp.
What I want is the last updated datetime of the sensor “Device Power” be set to the associated timestamp.

let’s say timestamp in the json refers to 14:39 and power is 153W.
if HA queries at 14:43, it will get { “timestamp”:<timestamp_for_14:39>, “power”:153 } but will report in the sensor history 14:43 for value 153VA instead of 14:39.

That’s not possible through normal means. You’d need to store the timestamp as an attribute, then run a script to update the timestamps in the database via SQL commands. Likely fragile and error-prone.

(happy to be corrected by anyone better informed)

2 Likes

That’s not possible.

that’s unfortunate HA just assumes the value received was acquired at the same time.

how do I make a feature request for this ?

In the #feature-requests section, please use the search function before you make the request. I’m pretty sure this request already exists.

Hi.

I just stumbled through this post searching to solve this same problem.
I have a feed that provides multiple pairs of type: [[timestamp],[value] ] and want the sensor to store the timestamps as they are read (every 15 minutes) from the feed source.
Has this changed in any way since this issue was first posted in 2016?

Best regards and thanks in advance.

You mean has this changed since February 16th 2023?

Ups.
My Bad.
Sorry.

Best Regards.

P.S:
Maybe this functionality can be achieved the other way around?
Instead of reading values from an external feed, posting the same 15 tuples (each one corresponding to one minute) to my instance of HA every 15 minutes.

Is it at all possible associate a sensor value to a timestamp before it gets written to the database? Is there any HA functionality available or is HA really only associating sensor values to the moment it receives the message?

HA controls the timestamps at all times, users cannot override them in any way.