Open behnam85 opened 3 years ago
Try sending the value to a text sensor instead.
Sensor encodes the value as a float, likely the timestamp is large enough that floats can't represent the number accurately anymore.
I just worked on that same problem. It is definitely the result of the fact that publish_state uses a float input parameter and the 4 byte floats have not enough resolution to store all digits of a Unix epoche value measured in milliseconds. The quantization error is about +/- 64 seconds on the values that appear in HA.
A text_sensor is no replacement as it accepts no device_class. In HA this will only be a string, but never a timestamp.
I see two options, both being feature requests:
If there is any chance that one of these options could be implemented I would be happy to issue a feature request. Could you give me some feedback?
Related to #1099
Add a publish_state_double method to sensors which accepts doubles instead of floats. This would be helpful in many cases. The current publish_state is not capable of using HA's full state resolution.
Not really an option IMO. That would mean all internals like filters and so on would need to switch to double too. But that has substantial performance regressions when I last tested it on these constrained systems. For an edge case like this that wouldn't be worth it.
Add device_class properties to text_sensors. From all possilble device classes I currently see "date" and "timestamp" as possible targets for this feature.
Yeah that's probably the best way. For timestamp the value should be an ISO8601 string - which is what HA expects (for normal sensor the esphome HA integration does some conversion of the number to a iso8601 string, but for text sensor we shouldn't do that).
Sensor encodes the value as a float, likely the timestamp is large enough that floats can't represent the number accurately anymore.
Yes, this is the problem, we should probably add a note about it to the documentation though, not the first report of this (#1099).
Sensor encodes the value as a float, likely the timestamp is large enough that floats can't represent the number accurately anymore.
Yes, this is the problem, we should probably add a note about it to the documentation though, not the first report of this (#1099).
Yeah probably we should even remove timestamp
support from sensor completely (it's the only instance i know of where the values get that high) when we have device class support for text sensors (and perhaps integration in the uptime integration as well)
Yeah probably we should even remove
timestamp
support from sensor completely...
That would be a breaking change but much cleaner. The sensor for timestamps will probably continue to confuse users because of the quantization effects.
Is there a solution to this? I'm currently rendering a progress bar on a display, I'm calculating the position of the progress bar using the media_duration, media_position and media_position_updated_at (cast to a float) attributes from a media_player. because the timestamp is only updating every couple of minutes the position of the progress bar only updates comparably and I'd rather it be much more granular.
For anyone else having this issue, if you cast the timestamp to an int or double it will update correctly e.g:
static_cast<double>(id(esptime).timestamp_now())
Yeah probably we should even remove
timestamp
support from sensor completely (it's the only instance i know of where the values get that high) when we have device class support for text sensors (and perhaps integration in the uptime integration as well)
Is anyone already working on the device class implementation for text sensors? I can implement underlying functionality (adding configuration key to schema & validations & publishing the setting to discovery topics) and submit a PR, however - before jumping in - I would just like to check if devs have bigger plans for this or someone is already working on it? Someone would probably still need to go through existing text sensor integrations and classify them accordingly afterwards...
The HASS API changed to allow python object (native objects). Maybe the best solution would be a dedicated sensor_timestamp which returns a python dateteime object to home assistant, taking in an esphome::time::ESPTime?
Snip from https://developers.home-assistant.io/docs/core/entity/sensor/#available-device-classes
timestamp | | Timestamp. Requires native_value to return a Python datetime.datetime object, with time zone information, or None. -- | -- | --I tried the sensor with device_class timestamp route, as I would not care too much about the precision and the log states that it sends the correct value in ms, but HA still displays epoch 0.
Any updates on this? It feels weird for the solution to be switching to text sensors. Are floats hardcoded into the filter code? Could templates be used for this?
Nothing to do with filters. An esphome sensor:
of whatever type holds a float value and the API to HA transfers a float value.
Thanks for clarifying, mind pointing to the code that defines how the sensor values are sent? How hard would it be to make it accept doubles or integral types too?
It's in the protobuf I guess. But the state value that the sensor holds is a float, so you can't get past that. If you wanted to change it to other types, then you would have to modify everything about how sensors work and change the api protocol all the way to the HA integration.
I created a PR to add support for the TIMESTAMP and DATE device classes in the text sensor, reflecting in the API and MQTT: https://github.com/esphome/esphome/pull/6202
After starting the PR review, I will create the PR for the changes to the aioesphomeapi
package and the ESPHome integration in Home Assistant, the changes are ready and tested locally.
The problem
Time component .timestamp not updating. The .second is working fine. The .timestamp is working fine on ESP_LOGI and .timestamp updated. The .timestamp only updates every 128 seconds
Which version of ESPHome has the issue?
2021.10.3
What type of installation are you using?
Home Assistant Add-on
Which version of Home Assistant has the issue?
2021.10.7
What platform are you using?
ESP32
Board
No response
Component causing the issue
time
Example YAML snippet
Anything in the logs that might be useful for us?
Additional information