Dynamic map transformation

Hello Comunity!
Is there any way to map the state of an item (e.g. a Switch) with another item (e.g. a String)?

To avoid the XY Problem, why? It’s almost trivial to do what you are asking in a Rule but there might be other approaches that are more suited to your end goal.

Hi Rich
My goal would be to display as state of a switch the time elapsed since its activation. I thought of writing a simple python script and to display the output instead of ON or OFF using a map transformation, hence my question.

That’s a completely different question.

What you would do is create another Item to store the time elapsed. When the switch turns on start counting up the time using Design Pattern: Looping Timers or a cron triggered Rule where this time elapsed Item is updated. Continue updating the time elapsed Item until the Switch turns OFF. You can use a JS transform to convert the count to something easier to understand (e.g. 2h 5m 25s) or use the Time unit of measurement.

Thanks for the info. Would this method allow for a continuous update of the state of the switch or is it about the creation of a self-standing string that displays the time?

I don’t understand the question. The Switch turns on. A separate Number Item counts how long the Switch has been on. You apply a transformation to the Number Item’s label to convert the number of seconds or what ever you count by to something a little more human friendly if desired. When the Switch turns off, stop counting.

Imagine a sitemap with a switch that has the following label: “MySwitch [%s]”. This will display “MySwitch” on the left side and a ON or a OFF besides the switch, on the right side. What I’d like to show is a timer instead of ON/OFF on the right side.

And still control the Switch on that same line? Cannot be done.

You can hide the Switch and show the Number Item.

You can show the Switch on spot and the time in another spot.

You can not combine the states of two different Items in one Item’s label and still be able to interact with just one of them as a Switch.

Crystal clear! This answers my original question, as to whether two different items can somehow interact on the same line. Maybe an idea for a future improvement :stuck_out_tongue_winking_eye:

Actually, I got quite close to my goal, by using the EXEC transformation, but it only works with a transformation program that outputs one single value, not a continuously updating string.

There is one sneaky kludge you might try.

Your “counter” Number Item must be updated using only postUpdate by the looping timers.
That frees up commands to this Item for any purpose. Set autoupdate false, so commands do not interfere with state.

Now you can display the Number using a Switch widget, with mapped buttons.

Switch item=myCounter label="My Switch [%d]" mappings=[1="ON",2="OFF]

Now just add a rule that listens for commands to the counter Item and converts them into commands for your Switch Item.

EDIT
you might prefer a single button display, using visibility

Switch item=myCounter label="My Switch [%d ON]" mappings=[2="Toggle"] visibility=[mySwitch=="ON"]
Switch item=myCounter label="My Switch [OFF]" mappings=[1="Toggle"] visibility=[mySwitch!="ON"]
1 Like

Very interesting! I’ll give it a try!

Clever approach! I often forget about autoupdate when approaching problems like this.