How to obtain the stored brightness value of a light in a scene, and compare it to the current brightness of the light

I’m trying to run a script that will really slowly decrease the brightness of different bulbs in a room down to the brightness they’ve been set in a scene. I.e. The automation has set the scene Bright in the daytime due to motion and while I’m still stood there the sun sets and I want the scene Dim to be now applied, but the transition option doesn’t work with my light bulbs and they all of a sudden just change to dim. If i run a script I can make it slowly loop -1%/15secs until a defined value is reached, but what I really want to have is that manually defined value be the value that is defined in the Dim scene. This will allow me to change the presets in the scene and I not have to also go into the script and redefine these new values here too. I can see the brightness value in scenes.yaml but I don’t know how to compare this value to the current value of the bulb and loop the dimming slowly until it’s reached.

This obviously doesn’t work but it’s where I’m at at the moment. A repeat while value template:
{{ state_attr(‘light.bulbasaur’,‘brightness’)|int > state_attr(‘scene.dining_room_lights_dim’,‘brightness’)|int }}

This value template below works, but I’ll have to manually change the value if the scene is changed at any point in the future:
“{{ (state_attr(‘light.bulbasaur’,‘brightness’)|int > 26 ) }}”

Any ideas on how you can compare the state of the bulb to it’s set brightness in a scene? Thanks in advance.

Hello CompactCow,

Research Circadian lighting.

Not sure why you say that it “obviously” doesn’t work. Can you paste the output of Deverloper Tools → States when you pull up scene.dining_room_lights_dim?

When pasting code, always use the “Preformatted text” option in the composer, so that people can more easily read your code.

This is the output of Developer Tools → States when I pull up scene.dining_room_lights_dim:

entity_id:
  - light.bulbasaur
  - light.shellybulbduo_e8db84a9f16c
  - light.tall_skinny_lamp
  - light.dining_room_ceiling_lights
  - switch.piano_light
  - switch.art_deco_light
id: "2731358132781"
friendly_name: Dining Room Lights Dim

and I am just trying to get this script working before adding the other lights to run in parallel to decrease to their brightness to match the scene. If I can get it to work I might just run it in the automation rather than call a script, as the master setting will be the scene values. It’s a script at the moment to help testing.

sequence:
  - repeat:
      sequence:
        - action: light.turn_on
          metadata: {}
          data:
            brightness_step_pct: -1
          target:
            entity_id: light.bulbasaur
        - delay:
            hours: 0
            minutes: 0
            seconds: 15
            milliseconds: 0
      while:
        #- condition: template
        #  value_template: "{{ (state_attr('light.bulbasaur','brightness')|int > 26 ) }}"
        #  enabled: false
        - condition: template
          value_template: >-
            {{ state_attr('light.bulbasaur','brightness')|int >
            state_attr('scene.dining_room_lights_dim','brightness')|int }}
alias: Test Brightness Down
description: ""
mode: restart

Sir_Goodenough: The Adaptive or Circadian lighting idea is probably great (haven’t tried them but know of them), but I don’t think they are what I’m looking for. Just a simple command to reduce the lights down to the Dim scene setting as the transition option wont annoyingly apply to the type of lights I have (bulbasaur is a shelly vintage, shelly bulb duo is the single recessed ceiling light in the corridor leading upto the dining room, tall skinny lamp is an old zigbee bulb (ikea maybe??), ceiling lights are running through a shelly dimmer 2, and the switches are unfortunately at the moment no dimming smart switches). The transition, if it worked, would be ok I guess with 300secs, but if we can fully control the transition to the scene I might as well make it so nobody notices! 15secs is just an idea, I might make it double that or more if I can get this working, especially in the living room.

At the end of the day I’m just saving myself from having to manually change the target value in the script, seen as the value is sitting there somewhere in HA and I seem to have hit a brick wall with extracting it!

Are you certain you need the brightness to be stored in a scene? For example, you could instead create an input_number helper, where you can store the desired brightness. You can edit it from the UI and access it in automations and scripts—and also, if wanted, trigger automations when the number changes (for example to instantly update the current brightness match the target brightness and current time of day).

That approach is the crux of how my lights’ adaptive temperature is kept and updated throughout the day.

Well, I don’t want to store the brightness in a scene, I just want to extract and compare the brightness already in a scene. I can see the stored brightnesses in the scenes.yaml, so if I can get the code to reference this value and check it against the current value, I can loop a decreasing brightness until this is reached. It means no helpers required or raw integers, just a reference to the value of brightness I can see in the .yaml file. I just can’t work out the line of code to reference this scenes.yaml brightness.

“extract and compare the brightness already in a scene” is the same as “store the brightness in a scene.” A scene is defined as a set of entities and the set of associated state and attributes for each entity. So if you want to control the brightness using a scene, you need to store the brightness with the scenes.yaml definition.

The reason I asked is, there does not appear to be a way to get at the brightness of an entity that is set using a Home Assistant scene. Scenes provided by other integrations might have that attribute–for example, scenes from the Hue integration have it. But not HA scenes created with scenes.yaml.

If what you’re after is just having one centralized place to put a brightness definition that can be changed, and have automations access that brightness in order to do computations with it, scenes are not the way to do it. Hence as one possible workaround I suggested an input_number. There are other ways to do it, too.

I don’t know if we’re on the same wavelength or if it’s my poor explanation. You do state in that last comment however, that “there does not appear to be a way to get at the brightness of an entity that is set using a Home Assistant scene”. This therefore may be the unfortunate answer.

However, I’ll try and explain in another way. I have a scene called scene.dining_room_lights_dim that I use after sunset. Before sunset I would have a scene called scene.dining_room_lights_medium. My last automation would not activate this dim scene until after sunset and if we were still in the room, as occupancy was detected, it would wait until next time we were detected after sunset. This however ment that the lights could stay on medium brightness for hours after sunset and it didn’t quite work. We would reach for the switch and negate the first rule of a perfect automatic house: no manually intervention! If the lights gradually dimmed it would be perfect, but in other scenarios such as we left the room and then returned, I want it to set Dim straight away and not bother with fading, so I don’t want Circadian lighting. So this time around rewriting the automation, I wanted to try the transition option, set at 300secs, but it doesn’t work on most lights (a bit of a flaw somewhere with either the lights or HA??), and so the lights take about 1-2secs to reach dim, rather than 300secs. Now there are loads of ways to overcomplicate the process of moving from medium to dim over a set period of time, and I can create helpers for all the light entities and different scenes. I can even just set the target value to achieve ( value_template: “{{ (state_attr(‘light.bulbasaur’,‘brightness’)|int > 26 ) }}” ). But the scenes.yaml file have the values right there, visible in the yaml!!!

For example, below you can see that the fat shelly vintage called bulbasaur is already defined as brightness 26 in the scenes.yalm, without needing a helper, it’s just there for all to see, and I just want to use this stored information and see if bulbasaur is above, in this case, the value “26”:

- id: '2731358132781'
  name: Dining Room Lights Dim
  entities:
    light.bulbasaur:
      supported_color_modes:
      - brightness
      color_mode: brightness
      brightness: 26 # IT'S RIGHT HERE!!!!!!!!
      friendly_name: Bulbasaur
      supported_features: 32
      state: 'on'
    light.shellybulbduo_e8db84a9f16c: #I know, It's on the to-do list to rename!!
      min_color_temp_kelvin: 2700
      max_color_temp_kelvin: 6500
      min_mireds: 153
      max_mireds: 370
      supported_color_modes:
      - color_temp
      color_mode: color_temp
      brightness: 64 # AGAIN RIGHT HERE!!!!!!

If at some point I change the scene, that value will change in the scenes.yaml file and the automation will reference this new value, without me needing to change anything else (helpers). This is why referencing this value is perfect!

All I want to do is compare this value stored in the scenes.yaml with the current light brightness, to save having to set up Circadian lighting or helpers or raw integers. This gradual decrease (or increase) will be used in many different scenarios for all the lighting throughout the house, and therefore the amount of helpers needed will be very, very large, so seems so illogical when the value I want is sat right here in scenes.yaml.

Can we not extract the value. I.e. ‘scene.dining_room_lights_dim.light.bulbasaur.attributes.brightness’ to compare to state_attr(‘light.bulbasaur’,‘brightness’), and if so, what is the correct code to replace ‘scene.dining_room_lights_dim.light.bulbasaur.attributes.brightness’ to get this value??

If you’re right that “there does not appear to be a way to get at the brightness of an entity that is set using a Home Assistant scene”, and HA doesn’t have a fully working transition option within the automation action ‘activate scene with 300sec transition’ for most smart lights, I’m down to a ridiculous amount of helpers to solve this. It just seems so much easier to access this value set within the scenes.yaml, I just can’t get my code to compare the value I can see within this bl**dy file!

If I had to guess, I’d say the scene values must be stored in .storage as JSON. If that’s the case, and you can figure out where, you could use a JQ query to retrieve the value.

Your lengthier explanation confirms that we have been on the same exact wavelength since I originally responded to your post.

The problem is that you’re resisting accepting that scenes are not the way to accomplish what you want–and they really aren’t meant to be a mechanism for this kind of thing.

Can we not extract the value. I.e. ‘scene.dining_room_lights_dim.light.bulbasaur.attributes.brightness’ to compare to state_attr(‘light.bulbasaur’,‘brightness’) , and if so, what is the correct code to replace ‘scene.dining_room_lights_dim.light.bulbasaur.attributes.brightness’ to get this value??

The answer for a regular HA scene, as far as I can tell, and as I already answered, is no. It makes sense, because HA scenes are designed for an “execute this scene” use case, rather than a “here’s a profile of a light state that I want to be able to reference from scripts and automations” use case. HA’s colloquial way to tackle that use case is via helpers, like input_numbers, even if many good arguments exist for there being a better way, or for scenes having the capability you’re asking for.

It just seems so much easier to access this value set within the scenes.yaml, I just can’t get my code to compare the value I can see within this bl**dy file!

HA is a free and open-source software project. When it does not do what you want it to do, your typical recourse is either to accept what it actually does and find a solution, or to contribute code to the project to add the support you would like to see. In HA, you can also submit feature requests or just continue discussion on “WTH” posts.

I suggest you reframe thinking about this as “I just can’t get my code to compare the value.” It’s not about comparing the value; it’s about accessing it. If you don’t get past that, you aren’t going to accomplish the improvement to your automation that you’re seeking, even though there are plenty of ways to do it.