-2

I have a list and an object and I would like to use a list value to get one of the keys' values.

let devices = ['001A2208B97D','001A2208C9FA','001A2214ADC8','001A2214A73A','001A2214B86E','001A2214A6DF','001A2214ADBF','001A2208CFD3']
let entities = ['Temperature', 'Valve', 'Battery', 'Offset']
let temperature = { device_class: 'temperature', icon: "hass:thermometer-bluetooth", unit: "°C"}
let valve = { device_class: '', icon: "hass:valve", unit: "%"}
let battery = { device_class: 'battery', icon: "hass:battery-bluetooth", unit: ""}
let offset = { device_class: '', icon: "hass:offset", unit: "°C" }

for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {
        msg.payload = {
            "icon": temperature['icon'],
            "unit_of_measurement": temperature['unit'],
            "state_class": "measurement",
        }
    }
}

As you can see, the entities is lower case, so before getting the value from temperature, I need to convert to lower case.

I tried entities[0].toLowerCase()['unit'] and entities[0.toLowerCase()]['unit'] and (entities[0].toLowerCase())['unit'] and I have run out of ideas.

Does anybody know how to do this correctly? Ideally in a one liner, i.e. without first creating a new list or dict with all the lower case value. On the fly if possible :)

6
  • 1
    You can't/won't create an object with temperature in it instead of having a variable? like const data = {"temperature" : { icon: "mdi:thermometer-bluetooth", unit: "°C" }}? If you can create that, it will be really easy for you to work with. Otherwise you have to use something like the unsafe eval to evaluate your string to the variable. Commented Sep 20, 2023 at 20:08
  • temperature is the name of a variable, you can access the unit like temperature.unit. Its not clear from your question what you are trying to access Commented Sep 20, 2023 at 20:08
  • You may mean window[entities[0].toLowerCase()].unit to get the unit from the window variable called temperature. Commented Sep 20, 2023 at 20:23
  • 2
    Also you do not have a JSON - you have an object. This is JSON Commented Sep 20, 2023 at 20:24
  • Are you really saying that if you were to Google "JavaScript I need to convert to lower case", that you didn't find anything?! We expect that you'll do your research before posting here. Stack Overflow isn't supposed to be a tutorial site. Commented Sep 20, 2023 at 20:59

1 Answer 1

0

If this code exists in the global scope, the variable "temperature" will exist as a property of the "window" object, so you could request it like this:

window.temperature

Or this:

window["temperature"]

Or this:

var entity = "Temperature"
window[entity.toLowerCase()]

So your code might look like this:

let entities = ['Temperature', 'Valve', 'Battery', 'Offset']
let temperature = { icon: "mdi:thermometer-bluetooth", unit: "°C" }

msg.topic = window[entities[0].toLowerCase()]['unit']

But if this code is not in the global scope, such as in a function, it would work if you put the "temperature" variable inside another object, like this:

let entities = ['Temperature', 'Valve', 'Battery', 'Offset']
let stuff = {
  temperature: { icon: "mdi:thermometer-bluetooth", unit: "°C" }
}

msg.topic = stuff[entities[0].toLowerCase()]['unit']

(disclaimer: this code is completely untested)

Sign up to request clarification or add additional context in comments.

6 Comments

I updated my initial post with more code. I hope this makes it clearer. I did not want to spam too much with too much code and make it difficult to read. I will test the with both of Magmatic's approaches.
This is exactly what I said in my comment
EDIT: I created a new object per your recommendations and it worked perfectly! Thank you all for your fast support :)
Or, just temperature.
@Scott Marcus: I am iterating through the entities, so they won't all be temperature ;-). And as I said, I did search and I did try different approaches. And I have used this successfully before, but if you looked at my code and my problem you would see that this is a special case not covered in any examples I could find.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.