Inconsistent Temperature Units

I’ve been struggling to find a solution for this, what it comes down to is the History Graph is not consistent in upholding the unit of measure for some temperature entities I have (Shelly H&T). Here’s an example:

Using the Horizontal Stack just to get them side-by-side for the screenshot, but here’s the card code for this:

cards:
  - title: 14 Days
    type: history-graph
    entities:
      - entity: sensor.shelly_h_t_f38c7c_temperature
      - entity: sensor.owm_temperature
      - entity: sensor.owm_forecast_temperature
      - entity: sensor.owm_forecast_temperature_low
    refresh_interval: 0
    hours_to_show: 336
  - title: 1 Day
    type: history-graph
    entities:
      - entity: sensor.shelly_h_t_f38c7c_temperature
      - entity: sensor.owm_temperature
      - entity: sensor.owm_forecast_temperature
      - entity: sensor.owm_forecast_temperature_low
    refresh_interval: 0
    hours_to_show: 24
type: horizontal-stack

Here’s the entity attribute for the H&T sensor from Developer Tools:

unit_of_measurement: °F
friendly_name: H&T - Garage Temperature
device_class: temperature

Things I’ve tried:

  1. Similar to link 2, set backend configuration to:

    homeassistant:
        customize: !include customize.yaml
        unit_system: imperial
    
  2. Add frontend entity parameter of unit_of_measurement: '°F' (or variants as described in link 3)

  3. Verify H&T’s sensor units

  4. Send data to InfluxDB and check values - remains normal with correct value (in F units)

I’m stumped, help me force the output shown in a history graph to a single unit.

Your database has a ºC in it and it won’t merge the graphs until that piece of data is purged or beyond your 14 days. So do nothing and it will fix itself. That’s why the 1 day works and the 14 day doesn’t. The 1 day data doesn’t contain the ºC in the database, but the 14 day one does.

By the looks of it, you have about 2 days left.

Are the units linked to missing data? I added another sensor that has more complete data that doesn’t show inconsistent units.

Why does the one sensor have the correct value (but incorrect units)?

Units are pulled from the first datapoint in the series. So if your first data point has the wrong units, you have to wait for that datapoint to be outside your dataset.

During this current 2 week period though, there hasn’t been any changes to units with the sensor firmware or HA configs. The first data point in the dataset has the same units as the current datapoints, otherwise, wouldn’t there be an invalid conversion occurring across the whole history graph? In my examples above (14/12 day graphs), if the HA frontend is using the “incorrect” Celsius unit, my HA config should convert it to imperial, but the numeric value remains the same (when comparing 12day vs 10/8/6/1day). I don’t believe this is intended behavior and may be a bug in HA when showing >~10days of data in a History Graph card.

I’ve been collecting data on the sensor in question and storing it in InfluxDB since July. Here’s some history to where I tried forcing a unit change in firmware of the sensor, thus causing a conversion of value.

It is my assumption that data passed from the HA Recorder to InfluxDB is stored after HA unit conversions. If this is a valid assumption, there’s something with unit interpretation that HA is bugging out on.

Help!

The sensor graphs don’t use influx db, they use the home assistant database. Just do a SQL look up of that item and you’ll see that it has ºC in the database, not ºF. You’re welcome to continue to argue, but you just need to purge your HA database or wait.

I know that HA doesn’t use the separate InfluxDB for its data, my point with bringing that into discussion is that I’ve consistently recorded the the same numeric data and yet this inconsistency occurs.

Your advice to purge the database of the data or wait until the recorder autopurges it is basically what I’ve done for nearly a year and hearing that is the only option is disappointing since it will only continue on. I don’t believe I’ve argued anything yet and don’t see how adding more context around my thoughts is, I am merely trying to ask for help for my problem: Why is this inconsistency in units occurring and how do I fix it.

Don’t change your unit of measurement. As soon as you change it, it adds a new unit of measurement in the HA database. That will cause your graphs to use a different unit of measurement if that’s the first unit of measurement found in the database.

I’m trying to explain how it works and you keep basically saying “I don’t like the explanation, help!!!”. There’s nothing you can do with the basic database. You’d have to move to something else, then you’d have to learn how to edit it. You can edit the current database, but it’s a pain. This is ultimately the issue with databases in general.

Thanks, I always wondered how that happened. I guess I didn’t realize that the unit of measure is stored in a text field, attributes, in the states table. And, within that, it appears to be coded in, what, JSON? Almost a database within a database. At any rate, interesting approach to keep the database structure very simple and flexible.

My point is, the OP can confirm or disprove that there’s nothing else going on (beyond the expected behavior) by checking the offending values in the states table. It’s not really difficult to copy the home-assistant_v2.db file to another machine and open it up in DB Browser or whatever.

If you could imagine, it’s been optimized a few times over the years…

There’s really no need, that’s just how it works. Takes the first unit_of_measurement and filters the graphs separately based off that.

I haven’t changed my units of measurements within the current duration of Recorder’s purge_keep_days (my config is set to 14 days) and I’m telling you that all the units within the environment (sensor firmware is reporting data in imperial, HA unit_system’s is set to imperial, History Graph doesn’t have anything set) are consistent.

But here we are and the sensor’s is showing a metric unit of measurement.

So I figured I double check and do this per your recommendation, queried the states table with a entity_id filter for the specific entity name, and received 815 rows. Looking at the attributes field, example here:

{"unit_of_measurement": "\u00b0F", "friendly_name": "H&T - Garage Temperature", "device_class": "temperature"}

…for due diligence and to double check, split out the json and ran a value count on all of the unit_of_measurement unique values, just to see what the breakdown was, and sure enough, all 815 report “\u00b0F”. 32000 character limit here, but tab separated data if anyone’s interested: ha_db_dump_shelly_htgarage.tsv - Pastebin.com

It does appear as if the data is correct, then the last option would be browser cache. Have you cleared the browser cache and refreshed the page?

Went another route and searched for everything that would produce a unit_of_measurement that might lead to Celsius:

Dead end, unless these sensors which are used in the History Graphs somehow affect the unrelated sensor.

I have, tried using incognito, different browsers, different computer, all show the same behavior.

I would submit your findings on github by creating a new issue against home assistant core. Post all the data and screenshots you provided here. I was able validate your data.