resoai / TileBoard

A simple yet highly configurable Dashboard for HomeAssistant
MIT License
1.63k stars 278 forks source link

Rounding numbers in Text_List #815

Closed jsbrich closed 2 years ago

jsbrich commented 2 years ago

I am trying to round some temperature and humidity values to the nearest integer and I can't get it to work right.

  width: 1.1,
  height: 1.25,
  type: TYPES.TEXT_LIST,
  id: {}, // using empty object for an unknown id
  state: false, // disable state element
  list: [
  {
    title: 'Outside',
    icon: 'mdi-thermometer',
    value: '&sensor.outside_temperature.state',
    unit: '°F',
    filter: function(value) {
        var num = parseFloat(value);
        return num && !isNaN(num) ? num.toFixed(0) : value;
     }
 },

I would expect it to print something like Outside 35°F, but instead it is printing Outside 35.2°F

What am I missing?

jsbrich commented 2 years ago

Finally figured it out, so will post here in case anyone else is struggling.

I swear I tried this before posting, but this worked for me

                            position: [0, 0],
                            width: 1.1,
                            height: 1.25,
                            type: TYPES.TEXT_LIST,
                            id: {}, // using empty object for an unknown id
                            state: false, // disable state element
                            list: [
                                {
                                    title: 'Outside',
                                    icon: 'mdi-thermometer',
                                    unit: '°F',
                                    value: function() {
                                        var num = parseFloat(this.parseFieldValue('&sensor.outside_temperature.state'));
                                        return !isNaN(num) ? num.toFixed(0) : 'b';
                                    },
                                },

All the examples I saw, left the parseFloat call out and the toFixed was stumbling over parseFieldValue returning a string.