Using .rules-variable as item label?

Hi Dibbler42,

just tried for my usecase what you suggested here, but I get an error.

xxx [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘xxx’: org.eclipse.xtext.util.PolymorphicDispatcher$NoSuchMethodException: Couldn’t find method ''assignValue’’ for objects [JvmVoid: (eProxyURI: xxx.rules#xtextLink::0.2.4.2.0.0::0::/2), Label , xxx, org.eclipse.xtext.xbase.interpreter.impl.DefaultEvaluationContext@22d20afb, org.eclipse.xtext.util.CancelIndicator$1@d574100]

So “.Label” is not working for my Item.

That is my Code (I set the String Item Gast_Name_UG_Gaestezimmer in a sitemap with a selection)

rule "Gast_Name_UG_Gaestezimmer_changed"
when
		Item Gast_Name_UG_Gaestezimmer received update
then
    	g_UG_Gaestezimmer_Rollladen.Label = Gast_Name_UG_Gaestezimmer.state.toString()
end

Can you help me?

Thanks in advance

maybe help is very simple, just use .label instead of .Label. I check it with my rule and found that i use .label

Did you use the Designer to develop your rules? With ctrl + Space you should be able to browse trough all the options.

Thomas

Thanks for quick reply.

unfortunately not, tired it, similar error (shame I did not tried it before :slight_smile: . I use the Designer and was wondering why the Content Assist did not show Label nor label. Therefore I asked for help.
Even tried to set a fixed name in rule with no success either.
Also tried a single item instead of a group item, no success, “.label” coud not be resolved. Do I need another import?

Very funny. here are my lines of code and it works like a charme

		for (MapEntry : StationPrices.entrySet.sortBy[value]) {
			// Set the item label to station name
			openHABItems.get(i).label = StationNames.get(MapEntry.getKey())
			// Set price
			if (MapEntry.getValue() != Double.MAX_VALUE) {
				openHABItems.get(i).postUpdate(String::format("%.3f €", MapEntry.getValue()))
			} else {
				openHABItems.get(i).postUpdate("geschlossen")
			}
			i++
		}

Which type of item do you use? A Text item? I want to change the label of a Rollershutter, perhaps that is the issue?

Tested it with different single item types (number, switch, string), no success, could not change the label dynamically

I change the labels of string items. Could you post your code again? Maybe we find something

For sure, tried to reduce it:

Items:

String	Gast_Name_UG_Gaestezimmer		"Gastname im UG Gästezimmer"	(g_Namen)
Group:Rollershutter:OR(UP,DOWN)	g_UG_Gaestezimmer_Rollladen	"Rollladen Gästezimmer[%d %%]"	<rollershutter>	(g_UG_Rollladen, g_UG_Gaestezimmer)

sitemap to change the guest name:

Selection item=Gast_Name_UG_Gaestezimmer label="Gastname im UG Gästezimmer" icon="info" mappings=["Gast A"="Gast A", "Gast B"="Gast B", "Gästezimmer"="Gästezimmer"]

rule

rule "Gaeste_initialisieren"
when
	System started
then
{
	postUpdate(Gast_Name_UG_Gaestezimmer, "Gästezimmer")
}
end

rule "Gast_Name_UG_Gaestezimmer_changed"
when
		Item Gast_Name_UG_Gaestezimmer received update
then
    	g_UG_Gaestezimmer_Rollladen.label=Gast_Name_UG_Gaestezimmer.state.toString()
end

sitemap which should show dynamic label:

Switch item=g_UG_Gaestezimmer_Rollladen	mappings=[UP="Hoch", STOP="X", DOWN="Runter"]

I assume you build the items in your rules file, don’t you?
I want to change the label of an existing item

No, the items are not Build in the rule. The items are placeholders for different fuelstation values and i sort the prices and then i need to change the label according to che cheapest station.

Maybe it is a problem with groups. you could try it with a string item. if it work we are one step ahead.

Thomas

Tried a single item as well, with no success (see post #6 and post 9)

Even found your code on knx-user-forum.de and tried your imports (just to elimimate this even if I didn’t think so)

Hi @Dibbler42,

A very interesting post. I tried applying this logic to have the “lastUpdate” in my item’s label. This is to avoid duplicating all my items (_DateTime update items) and showing the lastUpdate as a separate line on my sitemap.

I tried it like this:

rule "Update label"
when
    Item Tex_Window_W10_GV_Bureau_Zijkant received update
then
	// logInfo("Window", Tex_Window_W10_GV_Bureau_Zijkant.lastUpdate)
    // Tex_Window_W10_GV_Bureau_Zijkant.Label = String::format("ABC Raam Bureau zijkant %1$td.%1$tm.%1$tY %1$tT ", Tex_Window_W10_GV_Bureau_Zijkant.lastUpdate)
    Tex_Window_W10_GV_Bureau_Zijkant.Label = String::format("ABC Raam Bureau zijkant %s ", Tex_Window_W10_GV_Bureau_Zijkant.lastUpdate)
end

However, when the rule is triggered, I get:

2017-05-29 18:03:23.624 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Update label': An error occured during the script execution: Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: test.rules#|::0.2.2.2.0.1::0::/2)

I’m not sure what I am doing wrong. Or maybe it is just not possible?

Sorry no idea, maybe you should try first to assign a non dynamic string.

Thomas

Maybe that needs a .toString

Hi,

I have made some progress.

This seems to do the trick:

rule "Update label"
when
	Item Tex_Window_W10_GV_Bureau_Zijkant received update
then
	Tex_Window_W10_GV_Bureau_Zijkant.label = String::format("Last update on %s ", Tex_Window_W10_GV_Bureau_Zijkant.lastUpdate.toString)
end

The “.toString” was missing and it should be “.label” instead of “.Label”.

The only problem is that I am not getting a nice date format. It is shown as “Last update on 2017-05-30T09:39:44.310+02:00

I tried changing it to:

Tex_Window_W10_GV_Bureau_Zijkant.label = String::format("Last update on %1$td.%1$tm.%1$tY %1$tT ", Tex_Window_W10_GV_Bureau_Zijkant.lastUpdate.toString)

But then I get a “d != java.lang.String”. When I remove the “toString”, I get a “d != org.joda.time.DateTime”.

So how do I get it displayed nicely (e.g. 30-05-2017 09:46)?

Thanks,
Dries

I’ve found it! :slight_smile:

Tex_Window_W10_GV_Bureau_Zijkant.label = "Last update on: " + Tex_Window_W10_GV_Bureau_Zijkant.lastUpdate.toDateTime.toString("dd-MM-yy hh:mm")

I want to sort a group of items from largest to smallest and then show this order in a sitemap - perhaps by changing some placeholder items labels and values - your code here looks similar but wondering if you could show an example from start to finish including items/groups, sitemaps and rules?

Sure will take a little bit of time

Thomas

OK, here is the full example using the tankerkoenig binding. Idea is to get fuel prices and display them in a sorted order or display “closed” if the station is closed.

To achieve that i defined item to hold the actual price, item with the information if station is closed and display items. the rule iterates trough the prices copy them to a map that will be sorted and then transferred to the display items (with korrekt label).

Here are items, rules, transformations and sitemaps.
Items

//------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//    Virtual
//
//------------------------------------------------------------------------------------------------------------------------------------------------------------

    //--------------------------------------------------------------------------------------------------------------------------------------------------------
    //
    //    Fuel stations
    //
    //--------------------------------------------------------------------------------------------------------------------------------------------------------
    Group VT_FuelStation "Tankstellen" <scene_gas_station>

    DateTime VT_FuelStation_LastUpdate "Letztes Update [%1$tH:%1$tM]" <time_clock> (VT_FuelStation)

    Group VT_FuelStation_Source "Tankerkönig Price Items" <scene_gas_station> (VT_FuelStation)
    Number VT_FuelStation_NordoelUetersen_Diesel "Nordoel (Uetersen) [%.3f €]" <scene_gas_station> (VT_FuelStation_Source)
    Number VT_FuelStation_NordoelElmshorn_Diesel "Nordoel (Elmshorn) [%.3f €]" <scene_gas_station> (VT_FuelStation_Source)
    Number VT_FuelStation_HEMElmshorn_Diesel "HEM (Elmshorn) [%.3f €]" <scene_gas_station> (VT_FuelStation_Source)
    Number VT_FuelStation_StarElmshorn_Diesel "Star (Elmshorn) [%.3f €]" <scene_gas_station> (VT_FuelStation_Source)
    Number VT_FuelStation_HEMWedel_Diesel "HEM (Wedel) [%.3f €]" <scene_gas_station> (VT_FuelStation_Source)
    Number VT_FuelStation_SBWedel_Diesel "SB (Wedel) [%.3f €]" <scene_gas_station> (VT_FuelStation_Source)
    
    Group VT_FuelStation_Open "Tankerkönig Open Flag Items" <scene_gas_station> (VT_FuelStation)
    Contact VT_FuelStation_NordoelUetersen_Open "Nordoel (Uetersen) [MAP(contact.map):%s]" <scene_gas_station> (VT_FuelStation_Open)
    Contact VT_FuelStation_NordoelElmshorn_Open "Nordoel (Elmshorn) [MAP(contact.map):%s]" <scene_gas_station> (VT_FuelStation_Open)
    Contact VT_FuelStation_HEMElmshorn_Open "HEM (Elmshorn) [MAP(contact.map):%s]" <scene_gas_station> (VT_FuelStation_Open)
    Contact VT_FuelStation_StarElmshorn_Open "Star (Elmshorn) [MAP(contact.map):%s]" <scene_gas_station> (VT_FuelStation_Open)
    Contact VT_FuelStation_HEMWedel_Open "HEM (Wedel) [MAP(contact.map):%s]" <scene_gas_station> (VT_FuelStation_Open)
    Contact VT_FuelStation_SBWedel_Open "SB (Wedel) [MAP(contact.map):%s]" <scene_gas_station> (VT_FuelStation_Open)

    Group VT_Display_FuelStation "Tankerkönig Display Items" <scene_gas_station> (VT_FuelStation)
    String VT_Display_Fuelstation_1 "Tankstelle 1 [%s]" <scene_gas_station> (VT_Display_FuelStation)
    String VT_Display_Fuelstation_2 "Tankstelle 2 [%s]" <scene_gas_station> (VT_Display_FuelStation)
    String VT_Display_Fuelstation_3 "Tankstelle 3 [%s]" <scene_gas_station> (VT_Display_FuelStation)
    String VT_Display_Fuelstation_4 "Tankstelle 4 [%s]" <scene_gas_station> (VT_Display_FuelStation)
    String VT_Display_Fuelstation_5 "Tankstelle 5 [%s]" <scene_gas_station> (VT_Display_FuelStation)
    String VT_Display_Fuelstation_6 "Tankstelle 6 [%s]" <scene_gas_station> (VT_Display_FuelStation)

Rules

import java.util.ArrayList
import java.util.Map
import java.util.HashMap

//------------------------------------------------------------------------------------------------------------------------------------------------------------
//
// Rule: Retrieve and process fuel station information
//
//------------------------------------------------------------------------------------------------------------------------------------------------------------
rule "Retrieve and process fuel station information"
when

    System started or
    Time cron "0 0/15 * * * ?" or // Every 15 min.
    Item VT_FuelStation_Source received update
    
then

    val Map <String, Double> PriceMap = new HashMap<String, Double>() // Price map = Station name -> gas price
    
    var ArrayList<StringItem> DisplayItems =  new ArrayList<StringItem>()
    var Integer i
    
    
    VT_FuelStation_LastUpdate.postUpdate(new DateTimeType())
    
    // Build map with prices
    VT_FuelStation_Source?.members.forEach[ PriceItem | 
        
        if (PriceItem.state != NULL) {
            
            PriceMap.put(PriceItem.label, (PriceItem.state as DecimalType).doubleValue)
            
        } else {
            
            PriceMap.put(PriceItem.label, 0.0)
            
        }

    ]
    
    // Build array with display items
    DisplayItems.add(0, VT_Display_Fuelstation_1)
    DisplayItems.add(1, VT_Display_Fuelstation_2)
    DisplayItems.add(2, VT_Display_Fuelstation_3)
    DisplayItems.add(3, VT_Display_Fuelstation_4)
    DisplayItems.add(4, VT_Display_Fuelstation_5)
    DisplayItems.add(5, VT_Display_Fuelstation_6)
    
    // Combine prices with display items
    i = 0
    for (PriceEntry : PriceMap.entrySet.sortBy[value]) {
        
        // Set the label of the display items
        DisplayItems.get(i).label = PriceEntry.getKey()
        
        // Set price according to label of display items
        if (PriceEntry.getValue() != 0.0)
        {
            DisplayItems.get(i).postUpdate(String::format("%.3f €", PriceEntry.getValue()))
        } else {
            DisplayItems.get(i).postUpdate("Geschlossen")
        }
        
        // Switch to next display items
        i = i+1
        
    }

end

Sitemap

        Text item=VT_Display_Fuelstation_1 icon="scene_gas_station" {
        Text item=VT_FuelStation_LastUpdate label="Letztes Update [%1$tH:%1$tM]" icon="time_clock"
        Text item=VT_Display_Fuelstation_1 icon="scene_gas_station"
        Text item=VT_Display_Fuelstation_2 icon="scene_gas_station"
        Text item=VT_Display_Fuelstation_3 icon="scene_gas_station"
        Text item=VT_Display_Fuelstation_4 icon="scene_gas_station"
        Text item=VT_Display_Fuelstation_5 icon="scene_gas_station"
        Text item=VT_Display_Fuelstation_6 icon="scene_gas_station"
    }

And thats it. Any optimizations and changes are welcome.

Thomas

Well that is very cool - I will try to translate that to my power use case and post my items, rules and sitemap