CSS template : access msg.payload value

Hi everyone

I'm trying to recreate what used to work in dashboard 1, in dashboard 2

As my gate move, the background of the button fill-up the button according to it's position

screen

I use to do it by passing the position to a function node to create a msg.payload with a gradian string that move with the gate position (the position is given by my Logo PLC)

I send this message to a template and put it to the root section of de CSS code.

Si the gradian string is available in the root section.

Then I create a class that use the gradian and apply it to the button.
In dashboard 2, the msg.payload in the root section stay as "msg.payload" and not the content (gradian string).

Here is the code for dashboard 1 and2, for a light button which use the same trick

It's maybe not the right way to do it ?!

Cheers
Pierre

[
    {
        "id": "c98d320c349fd2c8",
        "type": "s7 in",
        "z": "da5ea0e3.80dff",
        "g": "261a948a9a2b2a7c",
        "endpoint": "5ee1b8d9.11d978",
        "mode": "single",
        "variable": "B028-current time",
        "diff": true,
        "name": "",
        "x": 450,
        "y": 2920,
        "wires": [
            [
                "b9b8062a28465fc7"
            ]
        ]
    },
    {
        "id": "b9b8062a28465fc7",
        "type": "function",
        "z": "da5ea0e3.80dff",
        "g": "261a948a9a2b2a7c",
        "name": "gradian",
        "func": "var count;\nlet maxi=global.get(\"lumiere.tempolength\")*60;\nlet tempo=global.get(\"lumiere.tempo\");\nlet etat=flow.get(\"jrd.lumiere.etat\",\"memoryOnly\");\n\nif (etat == false) {\n    count = 0 + \"%\";\n} else {\n    if (tempo == true) {\n        count = (100 - msg.payload / maxi * 100) + \"%\";\n        } else {\n            count = 100 + \"%\";\n        }\n\n    }\n\n\n//msg.payload = \"linear-gradient(90deg, rgba(43, 131, 232, 1) 0%, rgba(43, 131, 232, 1) \" + count + \", rgba(0, 0, 0, 0) \" + count + \")\"\nmsg.payload = \"linear-gradient(90deg, var(--clrON1) 0%, var(--clrON1) \" + count + \", rgba(0, 0, 0, 0) \" + count + \")\"\n\n\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 680,
        "y": 2880,
        "wires": [
            [
                "bdad615566720824",
                "0bf2d07e34c5de28",
                "83240363ebd7ef1c"
            ]
        ]
    },
    {
        "id": "0bf2d07e34c5de28",
        "type": "ui-template",
        "z": "da5ea0e3.80dff",
        "g": "261a948a9a2b2a7c",
        "group": "",
        "page": "",
        "ui": "da8557b3525c7464",
        "name": "",
        "order": 1,
        "width": 0,
        "height": 0,
        "head": "",
        "format": "\n    :root {\n        --loadingC: {{ msg.payload }};\n    }\n    \n    [id=\"nrdb-ui-widget-49a338e9edb27076\"] > button {\n        background-image: var(--loadingC);\n        /*background-image: linear-gradient(90deg, rgba(47,117,250,1) 0%, rgba(0,255,219,1) 37%, rgba(124,255,166,1) 100%);*/\n        background-color: var(--clrOff);\n    }\n",
        "storeOutMessages": true,
        "passthru": true,
        "resendOnRefresh": true,
        "templateScope": "site:style",
        "className": "",
        "x": 860,
        "y": 2920,
        "wires": [
            [
                "94202bdb663d8e3f"
            ]
        ]
    },
    {
        "id": "bdad615566720824",
        "type": "ui_template",
        "z": "da5ea0e3.80dff",
        "g": "261a948a9a2b2a7c",
        "group": "aedadc2eea351ad5",
        "name": "loading",
        "order": 4,
        "width": 0,
        "height": 0,
        "format": "<style>\n    :root {\n        --loadingC: {{msg.payload}};\n    }\n    [node-id=\"999f5afe.16306\"] > button {\n        background-image: var(--loadingC);\n        /*background-image: linear-gradient(90deg, rgba(47,117,250,1) 0%, rgba(0,255,219,1) 37%, rgba(124,255,166,1) 100%);*/\n        background-color: #606060;\n    }\n</style>\n",
        "storeOutMessages": true,
        "fwdInMessages": true,
        "resendOnRefresh": true,
        "templateScope": "local",
        "className": "",
        "x": 860,
        "y": 2880,
        "wires": [
            []
        ]
    },
    {
        "id": "5ee1b8d9.11d978",
        "type": "s7 endpoint",
        "transport": "iso-on-tcp",
        "address": "192.168.1.155",
        "port": "102",
        "rack": "2",
        "slot": "3",
        "localtsaphi": "02",
        "localtsaplo": "00",
        "remotetsaphi": "03",
        "remotetsaplo": "00",
        "connmode": "rack-slot",
        "adapter": "",
        "busaddr": "2",
        "cycletime": "1000",
        "timeout": "5000",
        "name": "LOGO",
        "vartable": [
            {
                "addr": "DB1,X1024.0",
                "name": "I1.1"
            },
            {
                "addr": "DB1,X1024.1",
                "name": "I1.2"
            },
            {
                "addr": "DB1,X1024.2",
                "name": "I1.3"
            },
            {
                "addr": "DB1,X1024.3",
                "name": "I1.4"
            },
            {
                "addr": "DB1,X1024.4",
                "name": "I1.5"
            },
            {
                "addr": "DB1,X1024.5",
                "name": "I1.6"
            },
            {
                "addr": "DB1,X1024.6",
                "name": "I1.7"
            },
            {
                "addr": "DB1,X1024.7",
                "name": "I1.8"
            },
            {
                "addr": "DB1,X1025.0",
                "name": "I2.1"
            },
            {
                "addr": "DB1,X1025.1",
                "name": "I2.2"
            },
            {
                "addr": "DB1,X1025.2",
                "name": "I2.3"
            },
            {
                "addr": "DB1,X1025.3",
                "name": "I2.4"
            },
            {
                "addr": "DB1,X1025.4",
                "name": "I2.5"
            },
            {
                "addr": "DB1,X1025.5",
                "name": "I2.6"
            },
            {
                "addr": "DB1,X1025.6",
                "name": "I2.7"
            },
            {
                "addr": "DB1,X1025.7",
                "name": "I2.8"
            },
            {
                "addr": "DB1,X1064.0",
                "name": "Q1.1"
            },
            {
                "addr": "DB1,X1064.1",
                "name": "Q1.2"
            },
            {
                "addr": "DB1,X1064.2",
                "name": "Q1.3"
            },
            {
                "addr": "DB1,X1064.3",
                "name": "Q1.4"
            },
            {
                "addr": "DB1,X1064.4",
                "name": "Q2.1"
            },
            {
                "addr": "DB1,X1064.5",
                "name": "Q2.2"
            },
            {
                "addr": "DB1,X1064.6",
                "name": "Q2.3"
            },
            {
                "addr": "DB1,X1064.7",
                "name": "Q2.4"
            },
            {
                "addr": "DB1,X1065.0",
                "name": "Q2.5"
            },
            {
                "addr": "DB1,X1065.1",
                "name": "Q2.6"
            },
            {
                "addr": "DB1,X1065.2",
                "name": "Q2.7"
            },
            {
                "addr": "DB1,X1065.3",
                "name": "Q2.8"
            },
            {
                "addr": "DB1,X4.0",
                "name": "Ni4.0"
            },
            {
                "addr": "DB1,X4.1",
                "name": "Ni4.1"
            },
            {
                "addr": "DB1,X4.2",
                "name": "Ni4.2"
            },
            {
                "addr": "DB1,X4.3",
                "name": "Ni4.3"
            },
            {
                "addr": "DB1,X4.4",
                "name": "Ni4.4"
            },
            {
                "addr": "DB1,X4.5",
                "name": "Ni4.5"
            },
            {
                "addr": "DB1,X4.6",
                "name": "Ni4.6"
            },
            {
                "addr": "DB1,X4.7",
                "name": "Ni4.7"
            },
            {
                "addr": "DB1,X5.0",
                "name": "Nq1"
            },
            {
                "addr": "DB1,X5.1",
                "name": "Nq2"
            },
            {
                "addr": "DB1,X5.2",
                "name": "Nq3"
            },
            {
                "addr": "DB1,X5.3",
                "name": "Nq4"
            },
            {
                "addr": "DB1,X5.4",
                "name": "Nq5"
            },
            {
                "addr": "DB1,X5.5",
                "name": "Nq6"
            },
            {
                "addr": "DB1,X5.6",
                "name": "Nq7"
            },
            {
                "addr": "DB1,X5.7",
                "name": "Nq8"
            },
            {
                "addr": "DB1,WORD600",
                "name": "NAI1"
            },
            {
                "addr": "DB1,WORD700",
                "name": "NAQ1"
            },
            {
                "addr": "DB1,WORD602",
                "name": "NAI2bis"
            },
            {
                "addr": "DB1,WORD300",
                "name": "B028-delay"
            },
            {
                "addr": "DB1,WORD302",
                "name": "B028-remainning time"
            },
            {
                "addr": "DB1,BYTE304",
                "name": "B028-unit"
            },
            {
                "addr": "DB1,WORD305",
                "name": "B028-current time"
            },
            {
                "addr": "DB1,X6.0",
                "name": "Ni6.0"
            },
            {
                "addr": "DB1,X6.1",
                "name": "Ni6.1"
            },
            {
                "addr": "DB1,WORD307",
                "name": "B043-OffDelay"
            },
            {
                "addr": "DB1,WORD309",
                "name": "B043-RemainingTime"
            },
            {
                "addr": "DB1,WORD311",
                "name": "B043-Unit"
            },
            {
                "addr": "DB1,WORD312",
                "name": "B043-CurrentTime"
            },
            {
                "addr": "DB1,WORD314",
                "name": "B025-CurentTime"
            },
            {
                "addr": "DB1,WORD316",
                "name": "B025-OffDelay"
            },
            {
                "addr": "DB1,WORD318",
                "name": "B025-RemainingTime"
            },
            {
                "addr": "DB1,WORD312",
                "name": "B025-Unit"
            },
            {
                "addr": "DB1,WORD321",
                "name": "B030-CurentTime"
            },
            {
                "addr": "DB1,WORD323",
                "name": "B0305-OffDelay"
            },
            {
                "addr": "DB1,WORD325",
                "name": "B030-RemainingTime"
            },
            {
                "addr": "DB1,WORD327",
                "name": "B030-Unit"
            },
            {
                "addr": "DB1,WORD328",
                "name": "B006-CurrentTime"
            },
            {
                "addr": "DB1,WORD330",
                "name": "B006-OnDelay"
            },
            {
                "addr": "DB1,WORD332",
                "name": "B006-RemainingTime"
            },
            {
                "addr": "DB1,WORD334",
                "name": "B006-DelayTimeBase"
            },
            {
                "addr": "DB1,X6.2",
                "name": "NI6.2"
            },
            {
                "addr": "DB1,DWORD335",
                "name": "B055-Counter"
            },
            {
                "addr": "DB1,X6.3",
                "name": "NI6.3"
            },
            {
                "addr": "DB1,X6.4",
                "name": "NI6.4"
            },
            {
                "addr": "DB1,X6.5",
                "name": "NI6.5"
            },
            {
                "addr": "DB1,X6.6",
                "name": "NI6.6"
            },
            {
                "addr": "DB1,X6.7",
                "name": "NI6.7"
            },
            {
                "addr": "DB1,DWORD347",
                "name": "B055-ONthreshold"
            },
            {
                "addr": "DB1,DWORD351",
                "name": "B055-OFFthreshold"
            },
            {
                "addr": "DB1,WORD339",
                "name": "B063-Freq"
            },
            {
                "addr": "DB1,WORD341",
                "name": "B063-ONthreshold"
            },
            {
                "addr": "DB1,WORD343",
                "name": "B063-OFFthreshold"
            },
            {
                "addr": "DB1,WORD355",
                "name": "B052-OffDelay"
            },
            {
                "addr": "DB1,WORD357",
                "name": "B056-OffDelay"
            },
            {
                "addr": "DB1,X1065.4",
                "name": "Q3.1"
            },
            {
                "addr": "DB1,X1065.5",
                "name": "Q3.2"
            },
            {
                "addr": "DB1,X1065.6",
                "name": "Q3.3"
            },
            {
                "addr": "DB1,X1065.7",
                "name": "Q3.4"
            },
            {
                "addr": "DB1,X1066.0",
                "name": "Q3.5"
            },
            {
                "addr": "DB1,X1066.1",
                "name": "Q3.6"
            },
            {
                "addr": "DB1,X1066.2",
                "name": "Q3.7"
            },
            {
                "addr": "DB1,X1066.3",
                "name": "Q3.8"
            },
            {
                "addr": "DB1,X1026.0",
                "name": "I3.1"
            },
            {
                "addr": "DB1,X1026.1",
                "name": "I3.2"
            },
            {
                "addr": "DB1,X1026.2",
                "name": "I3.3"
            },
            {
                "addr": "DB1,X1026.3",
                "name": "I3.4"
            },
            {
                "addr": "DB1,X1026.4",
                "name": "I3.5"
            },
            {
                "addr": "DB1,X1026.5",
                "name": "I3.6"
            },
            {
                "addr": "DB1,X1026.6",
                "name": "I3.7"
            },
            {
                "addr": "DB1,X1026.7",
                "name": "I3.8"
            },
            {
                "addr": "DB1,WORD359",
                "name": "B098-CurrentTime"
            },
            {
                "addr": "DB1,WORD361",
                "name": "B098-OffDelay"
            },
            {
                "addr": "DB1,WORD363",
                "name": "B098-RemainingTime"
            },
            {
                "addr": "DB1,WORD365",
                "name": "B111-Counter"
            }
        ]
    },
    {
        "id": "da8557b3525c7464",
        "type": "ui-base",
        "name": "Bullion",
        "path": "/dashboard",
        "includeClientData": true,
        "acceptsClientConfig": [
            "ui-notification",
            "ui-control"
        ],
        "showPathInSidebar": false,
        "showPageTitle": true,
        "navigationStyle": "default",
        "titleBarStyle": "default"
    },
    {
        "id": "aedadc2eea351ad5",
        "type": "ui_group",
        "name": "Arrosage",
        "tab": "b30fa2cf.b0f918",
        "order": 14,
        "disp": false,
        "width": "4",
        "collapse": false,
        "className": ""
    },
    {
        "id": "b30fa2cf.b0f918",
        "type": "ui_tab",
        "name": "Maison",
        "icon": "fa-home",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

Example how to change any :root variable and how to add progress indicator to the button.

[{"id":"25e06468f1ffe1e0","type":"ui-button","z":"9ab81d258b54a577","group":"b438a90c1f4de560","name":"","label":"progress","order":2,"width":0,"height":0,"emulateClick":false,"tooltip":"","color":"","bgcolor":"","className":"button-with-progress","icon":"account","iconPosition":"left","payload":"","payloadType":"str","topic":"topic","topicType":"msg","buttonColor":"lightblue","textColor":"","iconColor":"","enableClick":true,"enablePointerdown":false,"pointerdownPayload":"","pointerdownPayloadType":"str","enablePointerup":false,"pointerupPayload":"","pointerupPayloadType":"str","x":660,"y":2820,"wires":[[]]},{"id":"f6ea8d1791916ab7","type":"ui-template","z":"9ab81d258b54a577","group":"","page":"","ui":"29792df7d7b05e2e","name":"change CSS root variables","order":0,"width":0,"height":0,"head":"","format":"<script>\n    export default {\n        data() {\n            return {\n                mounted: false\n            }\n        },\n        watch: {\n            msg: function () {\n                if(this.mounted == false){\n                    return\n                }\n                if(this.msg?.payload != undefined && this.msg?.topic != undefined){\n                    document.documentElement.style.setProperty(this.msg.topic, this.msg.payload);\n                }                \n            }\n        },        \n        mounted() {\n            this.mounted = true\n        }\n       \n    }\n</script>\n<style>   \n    .invisible-template {\n        display:none;\n    }\n</style>","storeOutMessages":true,"passthru":true,"resendOnRefresh":true,"templateScope":"widget:ui","className":"invisible-template","x":720,"y":2780,"wires":[[]]},{"id":"211012346c64d07a","type":"ui-template","z":"9ab81d258b54a577","group":"","page":"","ui":"29792df7d7b05e2e","name":"CSS","order":0,"width":0,"height":0,"head":"","format":".button-with-progress button{\n    contain: layout;\n}\n.button-with-progress .v-btn__overlay{\n    opacity: 1;\n    color: rebeccapurple;\n    z-index: -1;\n    width: calc(var(--progress, 0) * 1%);\n}\n.button-with-progress .v-btn:hover>.v-btn__overlay{\n    opacity: .8;\n}","storeOutMessages":true,"passthru":true,"resendOnRefresh":true,"templateScope":"site:style","className":"","x":650,"y":2740,"wires":[[]]},{"id":"baeffe41c4ef195e","type":"ui-slider","z":"9ab81d258b54a577","group":"b438a90c1f4de560","name":"","label":"Change progress","tooltip":"","order":1,"width":0,"height":0,"passthru":false,"outs":"all","topic":"topic","topicType":"msg","thumbLabel":"true","showTicks":"false","min":0,"max":"100","step":1,"className":"","iconPrepend":"","iconAppend":"","color":"","colorTrack":"","colorThumb":"","showTextField":false,"x":250,"y":2780,"wires":[["c14de657091bfeee"]]},{"id":"c14de657091bfeee","type":"change","z":"9ab81d258b54a577","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"--progress","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":2780,"wires":[["f6ea8d1791916ab7"]]},{"id":"27724edc45dca1d3","type":"comment","z":"9ab81d258b54a577","name":"Payload is CSS variable value, topic is variable name","info":"","x":360,"y":2740,"wires":[]},{"id":"b438a90c1f4de560","type":"ui-group","name":"B & S","page":"7294756f31e17b81","width":"6","height":"1","order":1,"showTitle":true,"className":"","visible":"true","disabled":"false","groupType":"default"},{"id":"29792df7d7b05e2e","type":"ui-base","name":"My Dashboard","path":"/dashboard","appIcon":"","includeClientData":false,"acceptsClientConfig":[],"showPathInSidebar":false,"showPageTitle":true,"navigationStyle":"default","titleBarStyle":"default"},{"id":"7294756f31e17b81","type":"ui-page","name":"Home","ui":"29792df7d7b05e2e","path":"/home","icon":"home","layout":"grid","theme":"52ba8a01d6eda628","breakpoints":[{"name":"Default","px":"0","cols":"3"},{"name":"Tablet","px":"576","cols":"6"},{"name":"Small Desktop","px":"768","cols":"9"},{"name":"Desktop","px":"1024","cols":"12"}],"order":7,"className":"","visible":"true","disabled":"false"},{"id":"52ba8a01d6eda628","type":"ui-theme","name":"Mobile","colors":{"surface":"#ffffff","primary":"#0094ce","bgPage":"#eeeeee","groupBg":"#ffffff","groupOutline":"#cccccc"},"sizes":{"density":"default","pagePadding":"5px","groupGap":"5px","groupBorderRadius":"10px","widgetGap":"5px"}}]

1 Like

Looks great, thank you very much

I was hopping to keep the code simpler. I'll try to understand how yours works, cause... it works ! :smiley:

It is simpler than you find it at first look. Just ask specific questions if something is not understandable.

Yeah it's probably, I'm just not familiar with the script part in the template node. But I'll learn