JOSNata to dim lights - could you please point whats wrong with code?

hi, I am trying to DIM lights using JSONata snippet:

{
   "brightness": $min(
       [
           $entities("light.lz2m_taras_group_gu10_5x").attributes.brightness - 60,
           2   
       ]
   )
}

but instead of dimming lights by -60 steps, it goes straight two value of 2.

could you please point me whats wrong with this?

increasing brightess goes without any issues, does it with steps of 60.

thank you kindly!

Perhaps you require $max( )?

Currently 250
-60 equals 190
max [190, 2] is 190 (whereas min [190, 2] is 2)

tried $max - same results unfortunately (jumps straights to 2).

Well you must have a problem with your flow somewhere else

If $entities("light.lz2m_taras_group_gu10_5x").attributes.brightness correctly returns, for example, 250

then your array will be [190, 2]
and $max of that will return 190 (which I assume is what you want)

So presumably

       $entities("light.lz2m_taras_group_gu10_5x").attributes.brightness

is not returning a value above 62

I hope I understand correctly what you are saying.

Bitghtest setting is indeed at 250.

My flow is very simple as for testing I just start it with timestamp inject node (that doesn’t fire every repeatedly).

So when my light is at brightness 250, when I hit that inject node - light dims to that 2 value - if instead of 2 I’ll use ie 100 - it dims straight to 100 - as if it would not take - 60 into consideration at all.

This of course is not the thing when it comes to increasing brightness.

I use similar setup for color_change - that works the way intended - it changes colour until it reaches either 252 or 500 (mireds).

It’s brightness this struggles with.

So my assumption was that you are using this JSONata successfully to increase the brightness.

This requires the ability to read brightness using $entities(‘light.name’).attribtues.brightness, as well as the ability to set brightness using a service call.

So you are using the code, and it is working, just setting to the ‘2’ value or ‘100’ value. This is what I expect, since your code is using $min

Therefore, I changed the code to use $max, to pick up the greater of the current brightness (less 60) or 2.

I don’t have your entities, so I have tested this using try-jsonata

The bit at the top is just creating a dummy function $entities to return the “current brightness” of 230.
The code works, and as I am using 50, returns 180.

The second time this would return 130, then 80, then 30, then 2.

Is this a light group defined in home assistant? Recently there have been problems addressing groups of lights together. The solution is to expand the light group so that you address each entity individually.

thank you kindly for going extra mile and test the actual code @Biscuit - now for the strangest part, for what @Mikefila suggested - I went and checked single entities of the group (just one) and this code indeed works on single entity BUT whats the most bizarre - I went back and changed but to group entities - and it started working for the group! I mean – i just don’t know what happened that it suddenly started working with this zigbee2mqtt created group.

in the end - happy days - it works.

thank you both for your help!

1 Like