Hi, All
when making complex widgets where you need to calculate a color or icon depending on conditions, you have to describe the same thing many times
and
in continuation of the topic Using vars in custom widgets: when are vars updated?
I implemented this via substitutions (with nested substitutions)
Ok, my suggestion is:
in config block of root component I added new block “subst” like defineVars
component: <component>
config:
subst:
sub1: <text or expression>
sub2: <text or expression>
...
subN: <text or expression>
sub1, sub2, subN - names can be anything except reserved words
- see examples
if the substitution contains only text, then you should use $subst.subN without “=” at the beginning
if the substitution contains expression then you should use =$subst.subN with “=” at the beginning
Examples
component: oh-list-item
config:
subst:
sub1: props.prop1?props.prop1 + " and something text":"nothing"
sub2: '(props.item) ? "State of " + props.prop1 : "Set props to test!"'
icon: if:mdi:door
color: props.prop2?(Number.parseFloat(props.prop)2>10?"blue":"green"):"red"
isClosed: '@@(props.item)==="CLOSED"?"true":"false"'
bagde: $subst.isClosed
As you can see there are nested substitutions “badge”
How it use:
uid: test222
tags: []
props:
parameters:
- description: A text prop
label: Prop 1
name: prop1
required: false
type: TEXT
- context: item
description: An item to control
label: Item
name: item
required: false
type: TEXT
- description: A test number
label: Test number
name: prop2
required: false
type: TEXT
parameterGroups: []
timestamp: Oct 12, 2023, 11:07:56 AM
component: oh-list-item
config:
subst:
sub1: props.prop1?props.prop1 + " and something text":"nothing"
sub2: '(props.item) ? "State of " + props.prop1 : "Set props to test!"'
icon: if:mdi:door
color: props.prop2?(Number.parseFloat(props.prop)2>10?"blue":"green"):"red"
isClosed: '@@(props.item)==="CLOSED"?"true":"false"'
bagde: $subst.isClosed
footer: =$subst.sub1
title: =$subst.sub2
icon: $subst.icon
iconColor: =$subst.color
badgeColor: =$subst.color
badge: =$subst.bagde
subtitle: =props.prop2
if props.prop2 = 10, color is green
if props.prop2 = 101, color is blue
Another example
config:
subst:
sub1: props.prop1?props.prop1 + " and something text":"nothing"
sub2: '(props.item) ? "State of " + props.prop1 : "Set props to test!"'
icon: ($subst.isClosed)?"if:mdi:door":"if:mdi:door-open"
color: props.item?(@@(props.item)==="OPEN"?"orange":"blue"):"red"
isClosed: '@@(props.item)==="CLOSED"'
bagde: ($subst.isClosed)?"closed":"open"
footer: =$subst.sub1
title: =$subst.sub2
icon: =$subst.icon
iconColor: =$subst.color
badgeColor: =$subst.color
badge: =$subst.bagde
subtitle: =($subst.isClosed)?"--CLOSED--":"--OPEN--"
It works dinamicaly, color, icon, subtitle changes when window closed or open
The implemetation is non optimal as I think, but it works. Maybe this could be done differently through ‘jse-eval’ and its methods (expr.addUnaryOp or others), but I could not do it correctly.
I tried expr.addUnaryOp - $ - but it just replace my substition with text, no expression
Maybe someone has the desire and can improve my solution because i am newbie in javascript/vue
The source code is here GitHub - d51x/openhab-webui at widget_substitutions
@JustinG What do you think about it?