I read the address of my phone from openstreetmap using the geolocation.
the result is a json string and contains strings and numbers like: Road, housenumber, suburb, town, city, state, postcode, …).
Sometimes the json response doe not contain one or the other and I get the entire json string back (not null as expected).
Anyway, what I do is i check for this issue like this:
if(result.contains("town")) {
town = transform("JSONPATH", "$.address.town", result)
}
else {
town = "-"
}
Now I would like to use a loop to concat these string items if they are NOT “-” like:
road, house_number, suburb, town, state.
I am sure there is a way to do it without checking each single string for beeing “-”, because I need to size down my number of code lines.
(with the same approach I could create a list of open contacts on my windows or active devices in my home (coffee machine, lights, …).
Good idea.
However - if possible - I would like to handle the information in the full address depending if it’s in my home town (just street and suburb) instead of the full address including state and country.
In this case is probably the easiest way to do it. What are your other cases. There are as many solutions to as many problems and the best way to solve your other use cases may be very different.
thank you so much.
It works like a charm - for another use case as well (active devices).
For both rules, I was able to reduce the code from approx. 70 lines to 10
Are your rules parsing times getting any better? I know that is what prompted this lines of code reduction path you are heading down in the first place.
I did not check it in detail, but it should.
I have eliminated most of my sleeps (as you recommend in your code design suggestions (avoid long running rules).
That really opened my eyes.
However, I striggle with some minor issues (these were the reason to use sleep in the first place).
One is this:
I have some rules which are triggered by xx received command.
within this rule I check for triggered by ON or OFF.
This was not working consistently, so I used sleep to wait for the command to settle (??).
Anyway, this worked in all cases with a sleep of 2 seconds.
Now I have started to split these rules mentioned above into “received command ON” and received command OFF (without sleep of course).
This seems to work much better.
Again, instead of expand my code with new stuff, I started to streamline it after I found your design strategy hints.
So thanks a lot for that!
When using the ‘received command’ trigger, the item’s state has not yet been changed. To get around this, there is an implicit variable that you can use to get the command that was received for the item… receivedCommand. You can use this instead of the item’s state, as this is what the item’s state will be changing to. No sleep required!
rule "Test"
when
Item Test_Switch_1 received command
then
if (receivedCommand == ON) {
//do stuff
}
else if (receivedCommand == OFF) {
//do other stuff
}
end
No. It is limited to the scope of the rule instance, just like any other local variable. Meaning you can use it (and the other implicit variables) in multiple rules without issue.