Turn light up 50% on button press, then full, then off (HomeAssistant)

We've a wall light in our hall whose main lightswitch is poorly placed. So, I put a smart bulb in and put a Zigbee button in a more convenient place.

However, the light is brighter than a thousand suns, so I wanted a it to come on at just under half brightness by default, with an option to turn it up to full (in case someone loses a contact, or is bored of having retinas)

The following script in HomeAssistant/Hass.io achieves that.

One of the things that an trip you up, is checking the current brightness state - it's not reported as a percentage, but as a (I assume) byte value.

Details

  • Language: HomeAssistant

Snippet

hall_light_on:
  alias: Hall Light
  sequence:
  - choose:
    - conditions:
      - condition: device
        type: is_off
        device_id: c6c8daf58fdfdfc8630a5935739b931b
        entity_id: light.lk_zbt_dimlight_d0120_level_on_off
        domain: light
      sequence:
      - type: turn_on
        device_id: c6c8daf58fdfdfc8630a5935739b931b
        entity_id: light.lk_zbt_dimlight_d0120_level_on_off
        domain: light
        brightness_pct: 40
    - conditions:
      - condition: numeric_state
        entity_id: light.lk_zbt_dimlight_d0120_level_on_off
        attribute: brightness
        below: '254'
      sequence:
      - type: turn_on
        device_id: c6c8daf58fdfdfc8630a5935739b931b
        entity_id: light.lk_zbt_dimlight_d0120_level_on_off
        domain: light
        brightness_pct: 100
    default:
    - type: turn_off
      device_id: c6c8daf58fdfdfc8630a5935739b931b
      entity_id: light.lk_zbt_dimlight_d0120_level_on_off
      domain: light
  mode: single

Usage Example

# Automation to act on the button press
- id: '1610729827744'
  alias: Hall Button Press
  description: ''
  trigger:
  - platform: event
    event_type: zha_event
  condition:
  - condition: template
    value_template: '{{ trigger.event.data.device_ieee == "00:14:4a:01:23:19:31:a4"}}'
  action:
  - service: script.hall_light_on
    data: {}
  mode: single