Help: Fun with maps!

So I'm just messing around trying to understand maps better. I have two sets of code here one works, one doesn't... what am I missing? :grin:

Working example found on internet:

    Map exampleMap = ["person1": ["firstName": "John", "lastName": "Doe"]]
    log.debug "exampleMap: $exampleMap"
    // From Log - [person1:[firstName:John, lastName:Doe]]
    exampleMap.each{ key, item ->
        log.debug "key: $key - FirstName: ${item['firstName']}"
    }

Output:
exampleMap: [person1:[firstName:John, lastName:Doe]]
key: person1 - FirstName: John

Now mine:

    def deviceMap = new HashMap()
    deviceMap.put("dName", "${deviceName}")
    deviceMap.put("bType", "${batteryType}")
    deviceMap.put("bDate", "${batteryDate}")
    deviceMap.put("bWarnLvl", "${batteryWarnLvl}")
                
    if(state.batteryMap == null) state.batteryMap = [:]
    state.batteryMap.put("${deviceId}", "${deviceMap}")
    log.debug "batteryMap: $state.batteryMap"
    // From Log - [9053:[bType:USB, bDate:2024-11-23, dName:Back Door Keypad, bWarnLvl:30]]
    state.batteryMap.each{ tKey, tItem ->
        log.debug "tKey: $tKey - bType: ${tItem['bType']}"
    }

Output:
batteryMap: [9053:[bType:USB, bDate:2024-11-23, dName:Back Door Keypad, bWarnLvl:30]]
ERROR: groovy.lang.MissingPropertyException: No such property: bType for class: java.lang.String Possible solutions: bytes on line 211 (method pageConfig)


Thanks for looking!

When you save the device's map into state, "${deviceMap}" converts the device map into a long string. So the state is saved as Map<String,String> instead of Map<String,Map<String,String>>. That means upon read, tItem is just a String. Using put("${deviceId}", deviceMap) allows it to work.

2 Likes

I was sooo close! lol

Thanks you!

2 Likes

:rofl: Don't know if this was intentional but I like your title, reminds me of Sheldon's "Fun with Flags" in the Big Bang Theory :wink:

4 Likes

Sure was, still love that show. Glad someone got the reference!

4 Likes

Will this become something you're likely to share (sounds interesting) or family use only? :slight_smile:

1 Like

Depends if I ever finish it. :wink:

2 Likes

Ooo...

Just gimme your wife's number and I'll text her a statement of work so we can get your schedule all sorted out. :wink: :smiley:

1 Like

Old habits are hard to break... Watching college football and coding. :upside_down_face:

2 Likes

Seems like many of us are tackling batteries :slight_smile:

2 Likes

OMG...we have an embarrassment of (battery monitoring) riches. :smiley:

Why are you hogging this to yourself? :wink:

I plan to release this ... hopefully soon. Working on the final feature: allowing users to choose the Device Data field names and then migrate current battery data from previous field names, while translating the formats (e.g. dates).

2 Likes

Cool & thanks! :slight_smile:

I'm like 1,500 lines away from fininshing my battery tracking app, but since 1) I don't know how to code at all, and 2) You are almost done; I'll set that project aside. :wink:

3 Likes

Ha! The release will support the reporting (as I pasted earlier) and also various kinds of tracking and notifications: low battery (%), new battery (%), old battery (based on average life expectancy), and dead battery (last activity). The device data will hold the battery type, battery change date, and battery age history (list of previous battery ages). If you have other suggestions, please share. My brain is all battery all the time right now :slight_smile:

3 Likes

For what it's worth, mine will be MUCH simpler. lol Which reminds me I need to go put the goats and chickens away for the night (seriously!). :goat: :chicken: :goat: :chicken:

2 Likes

I wish I had goats...my wife has a friend who has them and the pictures and descriptions of the younger one's shenanigans are hilarious. I hope yours are equally entertaining.

My father had five to seven llamas for years...def not as fun as goats, and man did they poop a lot...

Thanks for asking, don't mind if I do. :slight_smile:

I track historical change dates to ID if I have devices w/worsening battery life over time. So some way to keep track of that data could be helpful. I do it this way now, listing multiple dates in the entry in Data:

  • BatteryChangeDate: 02-28-24 -- 03-15-23

Over time I can see if the change dates start getting closer together or not. I've had some older devices start to show their age by having shorter and shorter battery life w/same usage, leading me to replace them after a while so I'm not wasting batteries.

Could be accomplished by having a way to display a battery change date history. Seeing the actual dates is more helpful for me than having a showing calculated average number of days between changes, as I would likely not see a trend in that case. Make sense?

The app tracks this information, but in a slightly different way. Instead of maintaining a history of each battery change date, it keeps a history of each previous battery age at death. For instance, batteryLifeWks (or whatever you choose to name it) is a comma-separated field that holds the battery lives (in weeks): 50,52,48,etc. ... as many as you wish to keep. The app supports automatic (app-detected) or manual updates of this field upon battery changes.

Perfect, that is just what I need (to feed my battery OCD). :wink:

My app's formatting will look very familiar to you, as I stole/modified it from your apps :smile: (Credit given in the code to you and Stephack.)

2 Likes