Run a script based on emulated Hue bulb brightness change?

Let’s say I have light.virtual_bulb_1 set up as an emulated Hue bulb. I have turn_on set up to run a script, turn_off set up to run another script… but I’m trying to figure out how to handle brightness up/down.

If light.virtual_bulb_1 brightness increases, I want to execute script.virtual_dimmer_1_up

If light.virtual_bulb_1 brightness decreases, I want to execute script.virtual_dimmer_1_down

How do I accomplish this?

You’ll want to create a template sensor that grabs the brightness attribute of that light, then use that template sensor in the trigger of an automation like this:

- alias: Dat automation
  trigger:
  - platform: state
    entity_id: sensor.virtual_light_1_brightness
  condition:
  - condition: template
    value_template: "{{ trigger.from_state.state != None }}"
  action:
  - service_template: "script.virtual_dimmer_1_{{ 'up' if trigger.to_state.state | float > trigger.from_state.state | float else 'down' }}"

The condition is there to prevent the automation from firing at home assistant startup in case the light isn’t available right away, or when the light is turned on (which likely means that the template sensor will go from None to a number).

I also don’t totally understand the purpose of what you’re trying to accomplish here but hopefully it gets you there.

Does this all look correct? I got emulated Hue working, but I’m struggling a bit with the dimmer - perhaps because I made some changes, but emulated Hue configuration seems slow to refresh.

Here’s what I’ve got. Is this right?

In configuration.yaml:

emulated_hue:
  expose_by_default: false
  entities:
    light.emulated_bulb_1:
      name: "Emulated Bulb 1"
      hidden: false

light:
  - platform: template
    lights:
      emulated_bulb_1:
        friendly_name: "Emulated Bulb 1"
        turn_on:
          service: script.emulated_bulb_1_on
        turn_off:
          service: script.emulated_bulb_1_off
        set_level:

sensor:
  - platform: template
    sensors:
      emulated_bulb_1_brightness:
        friendly_name: "Emulated Bulb 1 Brightness"
        value_template: "{{ states.emulated_bulb_1.attributes.brightness }}"
        entity_id: light.emulated_bulb_1

In automations.yaml:

- id: emulated_bulb_dimmer
  alias: Emulated Bulb Dimmer
  trigger:
    platform: state
    entity_id: sensor.emulated_bulb_1_brightness
  condition:
    condition: template
    value_template: "{{ trigger.from_state.state != None }}"
  action:
    service_template: "script.hue_dimmer_1_{{ 'down' if trigger.to_state.state | float < trigger.from_state.state | float else 'up' }}"

And then, in scripts.yaml, I have a script for hue_dimmer_1_down and hue_dimmer_1_up, both of which I know are working correctly.