How can I have a limit when I dim down Fibaro Dimmer 2 with my automation?

This is my automation for dimming down the Fibaro Dimmer 2 that controls the spots in the living room (Stue):

- id: '16046462722814'
  alias: Stuelys svakere
  description: Gjøre lyset i stua svakere
  trigger:
  - platform: mqtt
    topic: eg/LysStuaNed
  condition:
  - condition: state
    entity_id: light.stuedimmer
    state: 'on'
  action:
  - data_template:
      brightness: '{{states.light.stuedimmer.attributes.brightness - 40}}'
    entity_id: light.stuedimmer
    service: light.turn_on

This is controlled by a Nexa switch, and I have one problem. It won’t stop at my set minimum dim level in the Dimmer 2. I’m assuming that’s because this only works for the switches connected directly to the dimmer. But how can I make sure that it won’t go below a set number? I am guessing that using a second condition (probably with condition : and as the second line in a condition template) that has the

value_template: "{{ states('states.light.stuedimmer.attributes.brightness < 40 }}"

would stop it from from dimming more if it’s 39 or less, but if the current brightness is 42 I think it will dim down to 2, so the spots in actuality are off. Is there any way to do this?

states.light.stuedimmer.attributes.brightness is a string, you won’t get very far with that. (ie most HA values are stored as strings)

Try

“{{ states.light.stuedimmer.attributes.brightness | int - 40 }}”

This converts the current brightness to an integer and then subtracts 40, is that what you were after ?

If you want (say) a floor of 36 you could use : -
“{{ max((states.light.stuedimmer.attributes.brightness | int - 40), 36) }}” see below

Edit: Okay I’m back at a workstation (so not working from memory)

The more resilient (and recommended) method of getting an attribute is : -
state_attr(‘light.stuedimmer’, ‘brightness’) | int
This can avoid some errors at start up

The max syntax is : -
{{ [(states.light.stuedimmer.attributes.brightness | int - 40), 36)] | max }}

Which will deduct 40 or if it goes below 36, set it to 36
you should do the same the other way with a max of 255 (for which you’d use a min)

Thanks for helping out! But from the last code I get an error message when validating:

Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected ‘)’, expected ‘]’) for dictionary value @ data[‘action’][0][‘data_template’][‘brightness’]. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 19). Please check the docs at Automation - Home Assistant

This is with this automation:

- id: '16046462722814'
  alias: Stuelys svakere
  description: Gjøre lyset i stua svakere
  trigger:
  - platform: mqtt
    topic: eg/LysStuaNed
  condition:
  - condition: state
    entity_id: light.stuedimmer
    state: 'on'
  action:
  - data_template:
      brightness: "{{ [(states.light.stuedimmer.attributes.brightness | int - 40), 36)] | max }}"
    entity_id: light.stuedimmer
    service: light.turn_on

I’m sure the error is mine, it usually is. :wink: Btw is it possible to use something similar so that instead of dimming one bit for each press it works like this:

Press on button when off: Turn on (which will go to last brightness)
Press on button when on, any brightness above 36: Gradually (preferably slowly) dimming to 36 and stopping there, but another press will stop it at the current level.
Press on button when on and brightness is 36: Gradually (preferably slowly) brightening to full and stopping there, but another press will stop it at the current level.

Or is that a bit to andvanced to do?

Come to think of it, maybe the simplest way would be to set the dimmer to work slowly (like 10 seconds from 0-100 and from 100-0) and then just use the button to start brigthening/dimming and second press to stopp it at the current level. But I can’t find any way to stop dimming with the service. @Mutt I saw the thread from April where you and a few others were discussing something very similar, but that was with another system (Zigbee, I believe).

Put the light on adjust the brightness then go to the template editor.
Se what the following tells you : -

states.light.stuedimmer.attributes.brightness
state_attr(‘light.stuedimmer’, ‘brightness’)
state_attr(‘light.stuedimmer’, ‘brightness’) | int
state_attr(‘light.stuedimmer’, ‘brightness’) | int - 40
[(state_attr(‘light.stuedimmer’, ‘brightness’) | int - 40), 36)] | max
(state_attr(‘light.stuedimmer’, ‘brightness’) | int + 40), 255)] | min

Report back

The trouble with setting a transition is say the lamp starts at 0 and transitions to 255 over say 60 seconds, is that the lamp gradually goes from off to full brightness over 60 BUT …
If you ask the bulb - at 0 seconds it says its at 0 (correct) but 1 second later (because its transitioning) it says its at 100% or 255. So you can’t stop it and expect meaningful results.
HA state changes occur at 1 second intervals
So if you want 0 to 100% over 10 seconds then you will need a repeating loop with 10% increments
(ie sometimes 25, sometimes 26) and then interrupt the loop with your second press.
That’s fine going up, but what about down, how will the system know if you want it brighter or dimmer ?
One method is always continue to the other extreme, then you can set it back (big bright messy bit in the middle) or always assume you start from zero (dark bit in the middle).
Also 10% per second over 10 discrete seconds is going to look a bit lumpy, even if you set transitions to be over the 1 second gap (I don’t know why it just does).
You have fibaro dimmers, so do I.
I have brightness profiles through the day for each type of bulb and use case (ie 8W led are in bathrooms, not every individual bulb (I could but I’m lazy) )
So I turn a bulb on and it’s set to come on at minimum brightness (stops flaring) then HA detects the bulb on and transitions it the the profile value over (say) 6 seconds (it’s adjustable), when ‘day’ goes to ‘evening’ the transition is say 120 seconds (again adjustable).
But the fibaro dimmers can also be configured for double click = maximum brightness (for emergencies or ‘this damn print is just too damn small to see otherwise’. :rofl: )
Second, as the bulb comes on at minimum, then if I ‘slowly’ double click (not fast enough to trigger max brightness, but faster than the 4 second limit I set. Then this interrupts (ie switch off was less than 4 seconds ago) the ‘adjust brightness to profile’ and leave the bulb at ‘dim’ ). So I have access to 3 brightnesses with a normal manual switch.
I have found that brightness and brightness_pct should work the same (but for the units) but they are not ‘just’ interchangeable.
I have a working brightness adjustment automation, give me a couple of hours and I’ll post it for you to pick apart

AND Stop using the " states. " syntax, it will cause you problems eventually AND this is not a case where you NEED to use it.

Here you go. : -

  - alias: au_light_lamp0102_lnk_on
    trigger:
      - platform: state
        entity_id: input_boolean.ib_light_lamp01and02_link
        to: 'on'
    condition:
      - condition: template
        value_template: "{{ states('light.fibdim2_lamp01_level') != states('light.fibdim2_lamp02_level') }}"
    action:
      - service: input_boolean.turn_on
        entity_id: input_boolean.ib_light_lamp0102_timer
      - service: input_boolean.turn_on
        entity_id: input_boolean.ib_aalight_stop_adjust
      - service_template: input_boolean.turn_{{ states('light.fibdim2_lamp01_level') }}
        entity_id: input_boolean.ib_light_lamp01_on
      - delay: '00:00:01'
      - service: light.turn_on
        data_template:
          entity_id: >
            {% if is_state('input_boolean.ib_light_lamp01_on', 'on') %}
              light.fibdim2_lamp02_level
            {% else %}
              light.fibdim2_lamp01_level
            {% endif %}
          brightness_pct: >
            {% if is_state('input_boolean.ib_light_lamp01_on', 'on') %}
              {{ (state_attr('light.fibdim2_lamp01_level', 'brightness') | float / 255 * 100) | int }}
            {% else %}
              {{ (state_attr('light.fibdim2_lamp02_level', 'brightness') | float / 255 * 100) | int }}
            {% endif %}
          transition: "{{ states('input_number.in_aalight_normal_transition') | int }}"
      - delay: '00:00:09'
      - service_template: input_boolean.turn_off
        entity_id: input_boolean.ib_aalight_stop_adjust
      - service: script.sc_light_lamp0102_reset

Note That this is part of a working automation on “fibaro dimmer 2” and that it uses brightness_pct
If you still have no joy using just ‘brightness’ you may have to switch over.
You will have to pick the bones out of this yourself (shouldn’t be too hard) I’m just giving you the tools to undertake the task.
Note also how I ‘read’ brightness and ‘write’ brightness_pct in this case (I had issues otherwise)
I got it to work and haven’t had ANY time to refine it so “it is what it is”

Basically this was part of a system to tie two lamps together (on/off/brightness) “if” a boolean was on, else allow them to act independantly

Thank you very much! I will mess with this in the weekend and let you know if I need more help. :+1: (Oh, I have that “too fine print” problem too, but I usually just use the phone’s camera as a magnifying glass…)

I got a bit of time today, and I’m afraid the main code is a bit above my head… :blush: But I got the other code in your first post working, with a few changes (replacing some paranthesis that weren’t working in my templates, maybe a typo on your part?). This code worked:

{{states.light.stuedimmer.attributes.brightness}}
{{state_attr('light.stuedimmer', 'brightness')}}
{{state_attr('light.stuedimmer', 'brightness') | int}}
{{state_attr('light.stuedimmer', 'brightness') | int - 40}}
{{[(state_attr('light.stuedimmer', 'brightness') | int - 40), 36] | max}}
{{[(state_attr('light.stuedimmer', 'brightness') | int + 40), 255] | min}}

And this is the result:

54
54
54
14
36
94

So they added and subracted as they should, and at least stopped in the bottom end, which is the most important part. It’s a bit late to actually try it here, but I think this is the code to dim the light, but stop at 36.

  action:
  - data_template:
      brightness: '{{[(state_attr('light.stuedimmer', 'brightness') | int - 40), 36] | max}}'
    entity_id: light.stuedimmer
    service: light.turn_on

And this will brighten the light, but stop at the max brightness:

  action:
  - data_template:
      brightness: '{{[(state_attr('light.stuedimmer', 'brightness') | int + 40), 255] | min}}'
    entity_id: light.stuedimmer
    service: light.turn_on

I’ll try that tomorrow. :slight_smile: