How to set the brightness of a bulb from a variable in a scene

Hi,

I have a variable setup called study_pct (its a variable using the var community addon)

var:
  study_pct:
    friendly_name: "study_pct"
    initial_value: 0

Behind the scenes I have a Philips Hue dimmer that when I press and hold it either increments or decrements the value of study_pct (that works well :slight_smile: )

I’ve setup a scene that I call when I want to apply the value and use the following but cannot get it to work…would someone be able to assist please?

Basically I want to apply the value of study_pct to the light’s brightness value.

- id: '1611073118176'
  name: testDimmer1
  entities:
    light.studylightbulb:
      brightness: >
        value_template: '{{ (states(''var.study_pct'') | int) }}'
      friendly_name: StudyLightBulb
      supported_features: 23
      state: 'on'

Try this

- id: '1611073118176'
  name: testDimmer1
  entities:
    light.studylightbulb:
      brightness: >
        value_template: '{{ (states('var.study_pct') | int) }}'
      friendly_name: StudyLightBulb
      supported_features: 23
      state: 'on'

sheminasalam that is not correct, look where your quotes open and close:

cardblower, you can’t use templates in scenes. Use a script instead.

Many thanks for this, I’ve created a script as below and it works perfectly !

setstudybulb1brightnessfrom_study_pct:
  alias: SetStudyBulb1BrightnessFrom_study_pct
  sequence:
  - service: light.turn_on
    data_template:
      entity_id: light.studylightbulb
      brightness_pct: '{{ (states(''var.study_pct'')) |int }}'
  mode: single
  icon: mdi:bulb
1 Like

Minor point, you no longer need to use data_template:, just data: will work.

Just for completeness, and to help anyone else who is trying to get dimming and brightening working on bulbs, here’s the solution I used:

I created 3 vars in configuration.yaml:

var:
  dimmer_down:
    friendly_name: "dimmer_down"
    initial_value: 0
  dimmer_up:
    friendly_name: "dimmer_UP"
    initial_value: 0
  study_pct:
    friendly_name: "study_pct"
    initial_value: 10

Then I created several automations to handle the up and down buttons being held and released:
They basically set the “dimmer_down” and “dimmer_up” variables to a 1 when the up or down buttons are held

- id: '1611139744189'
  alias: PhilipsDimmerUpHold
  description: ''
  trigger:
  - platform: device
    domain: mqtt
    device_id: b1c7bded8a06fdeb1befc4e5dcd0e87e
    type: action
    subtype: up-hold
    discovery_id: 0x00178801080d9758 action_up-hold
  condition: []
  action:
  - service: var.set
    data:
      entity_id: var.dimmer_up
      value: 1
    entity_id: var.dimmer_up
  mode: single
- id: '1611139985457'
  alias: PhilipsDimmerUpHold-Released
  description: ''
  trigger:
  - platform: device
    domain: mqtt
    device_id: b1c7bded8a06fdeb1befc4e5dcd0e87e
    type: action
    subtype: up-hold-release
    discovery_id: 0x00178801080d9758 action_up-hold-release
  condition: []
  action:
  - service: var.set
    data:
      entity_id: var.dimmer_up
      value: 0
    entity_id: var.dimmer_up
  mode: single

- id: '1610974443418'
  alias: PhilipsDimmerDownHold
  description: ''
  trigger:
  - platform: device
    domain: mqtt
    device_id: b1c7bded8a06fdeb1befc4e5dcd0e87e
    type: action
    subtype: down-hold
    discovery_id: 0x00178801080d9758 action_down-hold
  condition: []
  action:
  - service: var.set
    data:
      entity_id: var.dimmer_down
      value: 1
    entity_id: var.dimmer_down
  mode: single
- id: '1610976772292'
  alias: PhilipsDimmerDownHold-Released
  description: ''
  trigger:
  - platform: device
    domain: mqtt
    device_id: b1c7bded8a06fdeb1befc4e5dcd0e87e
    type: action
    subtype: down-hold-release
    discovery_id: 0x00178801080d9758 action_down-hold-release
  condition: []
  action:
  - service: var.set
    data:
      entity_id: var.dimmer_down
      value: 0
    entity_id: var.dimmer_down
  mode: single

Then I created the automations to react to the “dimmer_down” and “dimmer_up” variables changing to a 1:

- id: '1611139257625'
  alias: StudyLightLoop_BRIGHTEN
  description: ''
  trigger:
  - platform: state
    entity_id: var.dimmer_up
    from: '0'
    to: '1'
  condition: []
  action:
  - service: light.turn_on
    data: {}
    entity_id: light.studylightbulb
  - delay: '1'
  - repeat:
      until:
      - condition: numeric_state
        entity_id: var.dimmer_up
        below: '1'
      sequence:
      - service: script.setstudybulb1brightnessfrom_study_pct
        data: {}
        entity_id: light.studylightbulb
      - delay: '0.5'
      - service: var.set
        data:
          value_template: '{{ (states(''var.study_pct'') | int) + 10 }}'
        entity_id: var.study_pct
  mode: single

- id: '1610977578691'
  alias: StudyLightLoop_DIM
  description: ''
  trigger:
  - platform: state
    entity_id: var.dimmer_down
    from: '0'
    to: '1'
  condition: []
  action:
  - service: light.turn_on
    data: {}
    entity_id: light.studylightbulb
  - delay: '1'
  - repeat:
      until:
      - condition: numeric_state
        entity_id: var.dimmer_down
        below: '1'
      sequence:
      - service: script.setstudybulb1brightnessfrom_study_pct
        data: {}
        entity_id: light.studylightbulb
      - delay: '0.5'
      - service: var.set
        data:
          value_template: '{{ (states(''var.study_pct'') | int) - 10 }}'
        entity_id: var.study_pct
  mode: single

And finally, as I’m using the brightness_pct, I need to keep the study_pct value between 0 and 100, so 2 more automations:

- id: '1611141225253'
  alias: Keep_study_pct below 101
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: var.study_pct
    above: '100'
  condition: []
  action:
  - service: var.set
    data:
      value: 100
    entity_id: var.study_pct
  mode: single
- id: '1611141263301'
  alias: Keep_study_pct above -1
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: var.study_pct
    below: '0'
  condition: []
  action:
  - service: var.set
    data:
      value: 0
    entity_id: var.study_pct
  mode: single

And finally, just to keep this all in one place, here’s the script that is called to apply the study_pct value to the bulb:

setstudybulb1brightnessfrom_study_pct:
  alias: SetStudyBulb1BrightnessFrom_study_pct
  sequence:
  - service: light.turn_on
    data_template:
      entity_id: light.studylightbulb
      brightness_pct: '{{ (states(''var.study_pct'')) |int }}'
  mode: single
  icon: mdi:bulb
1 Like