Hi,
I am using OH3 and have installed the BOM addon so I can get the weather for Australia.
It is working well but I have an issue if the forecasticon variable the addon uses gets set to light-shower then the icon I get is the standard light bulb icon and not the light-shower icon I have loaded into /etc/openhab/icons/classic
The icon files do exist in the /etc/openhab/icons/classic directory and have the correct permissions set.
All the other icons work. If the forecasticon variable is set to sunny then I get the sunny icon, if set to cloudy I get the cloudy icon etc.
I am trying to get the light-showers icon to work on the overview page. The code I am using is: ="oh:"+(items.BOM_Day1_ForecastIcon.state)
I tried to use the metatdata State Description to maybe put a pattern that would remove the dash from the name light-shower and turn it into lightshower and then I could have that as a file name in the icons directory.
Is there a way I can get the correct icon to show up as the icons change all the time as they are the weather icons for the day.
As I said before all the other icons work but I think because the icon I want to use starts with the word light then I get the lightbulb icon instead of rain clouds.
I have come a bit further.
I had to install the map transformation addon.
I then created a bom.map file and added:
light-shower=lightshower
Then I added metadata State Description in the pattern field:
MAP(bom.map):%s
Now the display shows lightshower (without the dash, which is what I need I think) so I can create a lighshower.png icon and see what icon shows up.
BUT now I have another problem. I cannot use an expression to get the name without the dash in it.
If I use this: =“oh:”+(items.BOM_Day5_ForecastIcon.state)
I get oh:light-shower
If I use this:
=“oh:”+(items.BOM_Day5_ForecastIcon.displayState)
I get oh:undefined
My question now is how do I get an expression that can get the mapped variable (without the dash)
Hyphens are prohibited in icon filenames, except as part of a dynamic icon set. The icon picker uses it as a special character. You’ll have to rename to light_shower or something, and change whatever does the selection.
Note that icon selection is based on raw Item states. Transforming a state for display has no effect. You’ll need to do the transform before it gets put into the Item.
I have progressed a bit further.
I have created a group and added the 7 items I want to change the names of in that group.
I created a rule that the state of a member has been updated.
Then I refresh the load of the BOM addon and this gets the data from the weather site.
I have logging enabled and I can see I am getting the event.item name and state etc of all 7 items. That part works.
The part I cannot get to work is how to find a dash in the state so I can post an update and replace the dash with an underscore. I want to use Javascript. I have done this before in DSL but I want to see if I can do it in Javascript
Where’s this string coming from? Can you apply a transform at the binding channel? You certainly can add a transform profile to the channel-Item link, no rule required.
Yes I can transform the name BUT I cannot get the displayState to work.
I have cracked how to do it and it is failry simple and I don’t get a data loop.
Here is how I did it.
Created a group. Put all the items in the group that I want to be converted if they have dashes in the state.
Created a rule.
Trigger: when a member of the group changed
Action in javascript and it is just one line of code:
This really seems like a very round about way to achieve this. Even the JavaScript is overwrought.
You use the name to pull the Item from the registry in order to get the name. Just use the name. And you don’t need to pull an Item from the registry to get its state. Use the items dict.
Depending on how the rule is triggered, this will result in an infinite loop. If you use a changed trigger the rule will trigger one extra unnecessary time. If the rule is triggered using received command than you are OK. But if you use received update it will loop and loop forever.
But a rule is really not the appropriate way to implement this. The hierarchy for solving a problem like this is:
solve it at the source
transformation at the Thing/Channel
transformation using a Profile on the link between the Channel and the Item
write a rule
Only move down the list if a given step is impossible. In this case 3 is very much possible. You’d create a JavaScript transformation along the lines of:
inputs: {}
id: “2”
label: Strip the dashes out and replace with underscores.
configuration:
type: application/javascript
script: >
//This contains things that I have found that work in javascript
//More information here: https://openhab-scripters.github.io/openhab-helper-libraries/Guides/But%20How%20Do%20I.html
//need below to log to openhab.log file
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
//below is if you are going to use the ececute command
var Exec = Java.type("org.openhab.core.model.script.actions.Exec");
//below is also neede for the execute command
var Duration = Java.type("java.time.Duration");
events.postUpdate(itemRegistry.getItem(event.itemName).getName(), itemRegistry.getItem(event.itemName).getState().toString().replace(/-/g, '_'));
logger.info(itemRegistry.getItem(event.itemName).getName() + " " + itemRegistry.getItem(event.itemName).getState().toString().replace(/-/g, '_'));
Strange I am using the back tick top left hand of keyboard.
I couldn’t use the transform beacuse I want to show the icon on the overview page.
In the icon field I have this:
=“oh:”+(items.BOM_Day1_ForecastIcon.state)
Which works unless there is a dash in the name. This was the original issue. It showed a lightbulb if the icon state was light-shower
I did put map to change the display of the item but that didn’t work either as I couldn’t use:
=“oh:”+(items.BOM_Day1_ForecastIcon.displayState)
SO I figured the easiest way is to just change the data coming in as that is out of my control because it is coming from the weather site.
It seems to work.No loops. This only runs every hour.
But that what a transformation profile does. It sits between the Channel and the Item and transforms the value before it ever gets to your Item so by the time it gets to the Item it has the ‘_’ instead of ‘-’.
This has nothing to do with State Description metadata or transformation for display.
If you use the transform Profile with the above JS, your ="oh:"+(items.BOM_Day1_ForecastIcon.state) expression will work because the Item will never get a state that includes ‘-’.
I think I am missing something here.
Can you show me in a screenshot how I would do the profile bit.
I am not using any files I am doing all of this via the GUI.
I would feel better if I didn’t need to use a rule .
Thanks
Not at the moment but I can tell you where to look.
Navigate to the Item. Click on the Link. That will open a new page showing the link between the Item and the Channel. At the bottom you can select from among several Profiles. One of them will be transform where you can define a transformation which will apply to the data as it passed from the Channel to the Item.
You’ll need to put the JS above into a .js file in the /etc/openhab/transform folder though. As with .persist files, openHAB doesn’t yet support defining certain transforms through the UI.
Also, the JavaScript transformation is a separate addon you’ll need to install.