How to check the current on/off state? From an Areas Lights, in A Blueprint; using Selector>Target>Entity>Domain: Lights

So I am a bit of a newbie with home assistant, & in turn A virgin with templates, variable & Blueprints.

This is my first attempt at creating a Blueprint & so I started with A script that I have tried to convert to a Blueprint. But for the sake of simplicity, I can’t work out how to get the On/Off state of the lights in A room(area) to register? (from a Target Selector for lights) So with the great words from Pablo Picasso, i have obtained idea’s, concepts & code from other sources, but I just can’t get it to work.

I think it should be done with a variable & template like:

  input:
    target_lights:
      name: Targeted Lights
      description: The Lights this Script is Targeted at.
      selector:
        target:
          entity:
            domain: light

variables:
  lights: !input "target_lights"

- condition: 
  - "{{ expand(lights) | selectattr('state', '==', 'on') | list | count > 0 }}"

I’m assuming it is how I am referencing the variable or implementing the template,
but I have been banging my head for the last 24hrs on this & i’m stuck…

So Rumpelstiltskin, I offer you my first born blueprint to HAss:

blueprint:
  name: Another Light Blueprint-Script
  description: "My Default Light Blueprint-Script with my take on Profiles;\n\
  \ - Bright.     - (For Day time & Events, I need to be Awake; Bright eyed & bushy tailed).\n\
  \ - Dimmed      - (For after sunset & before sunrise when i'm (still) thinking about bed,\
  \ but I will want to be able to read the Funny pages).\n\
  \ - Night-Light - (For bed/sleep time, & you don't want to wake
  \ everyone else up, but you still want to see in an emergency; how to get to the Fridge)\n\
  \ - Colour-Loop -  (Was to be Aurora-Australis, but color-loop is easier to remember).\n\
  \ - Circadian   -  (For the rest of the house at night, & to get everyone in sync @ night with you).\n\
  \ - Adaptive    -  (This works really well with a bed/sleep input_boolean).\n\
  \ - Random      -  (A Sorely missed replacement to the light effect).\n\
  \ - Custom      -  (Utilise this when using scenes & applying custom colour effects).\n\
  \ - Flash       -  (works great in conjunction with Alarm Panel Automations).\n\n\
  \    - A PREREQUISITE of an input_select { Dropdown } with the desired above Profile/s (words) added is required. \n\
  \    - A PREREQUISITE of an input_boolean { Toggle } is reqired for Adaptive Profile Night-Light to work.\n\
  \    Go to Settings > Devices & Services > Helpers > Create Helper > { Dropdown } or { Toggle }.\n
  \   & I'm assuming you already have some Lights that you wanted to add.\n\n\
  \ Designed to be used with A Automation-Script, & that decides IF & WHEN too turn the Light/s On\
  \ (by calling this script) & Off (by any means you see fit)."
  domain: script
  input:
    target_lights:
      name: Targeted Lights
      description: The Lights this Script is Targeted at.
      selector:
        target:
          entity:
            domain: light
    profile_input_select:
      name: Profile input_select (Dropdown)
      description: The Profile the Targeted Light/s will comform to.
      selector:
        entity:
          domain: input_select
    profile_adaptive_input_switch:
      name: Profile - Adaptive - input_boolean (Toggle)
      description: The Light.Turn_On Adaptive Profile Night-Light Time Switch.
      selector:
        entity:
          domain: input_boolean
    transition_on:
      name: Turn On Light Transition Time
      description: The Light.Turn_On Transition Time.
      default: 0
      selector:
        number:
          min: 0.0
          max: 300.0
          mode: slider
          step: 1.0
    transition_change:
      name: Change Light Transition Time
      description: The Light.Turn_On Transition Time, when one or more light/s are already on.
      default: 9
      selector:
        number:
          min: 0.0
          max: 300.0
          mode: slider
          step: 1.0
    profile_colour_loop_transition_time:
      name: Profile - Colour Loop - Transition Time
      description: The Colour Loop Profile Light.Turn_On Transition Time between colours.
      default: 3
      selector:
        number:
          min: 1.0
          max: 300.0
          mode: slider
          step: 1.0
    profile_circadian_bed_time:
      name: Profile - Circadian - Bed Time
      description: The Circadian Profile Night-Light Time.
      default: '22:30:00'
      selector:
        time:
    profile_circadian_wake_up_time:
      name: Profile - Circadian - Wake Up Time
      description: The Circadian Profile Wake Up Time.
      default: '6:30:00'
      selector:
        time:
    sun_before_offset:
      name: Sunrise - Offset
      description: Offset the sunrise time.
      default: '1:00:00'
      selector:
        time:
    sun_after_offset:
      name: Sunset - Offset
      description: Offset the sunset time.
      default: '-1:00:00'
      selector:
        time:
    profile_bright_color_temp:
      name: Profile - Bright - Colour Tempature
      description: The Bright Profile light Colour-Tempature.
      default: 153
      selector:
        color_temp:
    profile_bright_brightness:
      name: Profile - Bright - brightness
      description: The Bright Profile light brightness.
      default: 255
      selector:
        number:
          min: 0.0
          max: 255.0
          mode: slider
          step: 1.0
    profile_dimmed_color_temp:
      name: Profile - Dimmed - Colour Tempature
      description: The Dimmed Profile light Colour-Tempature.
      default: 500
      selector:
        color_temp:
    profile_dimmed_brightness:
      name: Profile - Dimmed - Brightness
      description: The Dimmed Profile light brightness.
      default: 175
      selector:
        number:
          min: 0.0
          max: 255.0
          mode: slider
          step: 1.0
    profile_night_light_color_temp:
      name: Profile - Night Light - Colour Tempature
      description: The Night Light Profile light Colour-Tempature.
      default: 500
      selector:
        color_temp:
    profile_night_light_brightness:
      name: Profile - Night Light - Brightness
      description: The Night Light Profile light brightness.
      default: 25
      selector:
        number:
          min: 0.0
          max: 255.0
          mode: slider
          step: 1.0
          
variables:
  lights: !input "target_lights"

sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: !input profile_input_select
            state: Bright
        sequence:
          - if:
              - condition: 
                - "{{ expand(lights) | selectattr('state', '==', 'on') | list | count > 0 }}"
            then:
              - service: light.turn_on
                data:
                  color_temp: !input profile_bright_color_temp
                  brightness: !input profile_bright_brightness
                  transition: !input transition_change
                target:
                  !input target_lights
            else:
              - service: light.turn_on
                data:
                  color_temp: !input profile_bright_color_temp
                  brightness: !input profile_bright_brightness
                  transition: !input transition_on
                target:
                  !input target_lights
      - conditions:
          - condition: state
            entity_id: !input profile_input_select
            state: Dimmed
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ expand(lights.entity_id) | selectattr('state', '==', 'on') | list | count > 0 }}
            then:
              - service: light.turn_on
                data:
                  color_temp: !input profile_dimmed_color_temp
                  brightness: !input profile_dimmed_brightness
                  transition: !input transition_change
                target:
                  !input target_lights
            else:
              - service: light.turn_on
                data:
                  color_temp: !input profile_dimmed_color_temp
                  brightness: !input profile_dimmed_brightness
                  transition: !input transition_on
                target:
                  !input target_lights
      - conditions:
          - condition: state
            entity_id: !input profile_input_select
            state: Night-Light
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ is_state( 'lights' , 'on') }}
            then:
              - service: light.turn_on
                data:
                  color_temp: !input profile_night_light_color_temp
                  brightness: !input profile_night_light_brightness
                  transition: !input transition_change
                target:
                  !input target_lights
            else:
              - service: light.turn_on
                data:
                  color_temp: !input profile_night_light_color_temp
                  brightness: !input profile_night_light_brightness
                  transition: !input transition_on
                target:
                  !input target_lights
      - conditions:
          - condition: state
            entity_id: !input profile_input_select
            state: Random
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ expand(lights.entity_id) | selectattr('state', '==', 'on') | list | count > 0 }}
            then:
              - service: light.turn_on
                data:
                  hs_color:
                    - "{{ range(360)|random }}"
                    - 100
                  transition: !input transition_change
                target:
                  !input target_lights
            else:
              - service: light.turn_on
                data:
                  hs_color:
                    - "{{ range(360)|random }}"
                    - 100
                  transition: !input transition_on
                target:
                  !input target_lights
      - conditions:
          - condition: state
            entity_id: !input profile_input_select
            state: Colour-Loop
        sequence:
          - service: light.turn_on
            data: 
              hs_color:
                - "{{ range(360)|random }}"
                - 100
              brightness_pct: 100
              transition: !input profile_colour_loop_transition_time
            target:
              !input target_lights
          - repeat:
              while:
                - condition: template
                  value_template: "{{ expand(lights.entity_id) | selectattr('state', '==', 'on') | list | count > 0 }}"
                - condition: state
                  entity_id: !input profile_input_select
                  state: Colour-Loop
              sequence:
                - service: light.turn_on
                  data:
                    hs_color:
                      - "{{ range(360)|random }}"
                      - 100
                    brightness_pct: 100
                    transition: !input profile_colour_loop_transition_time
                  target:
                    !input target_lights
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: !input profile_colour_loop_transition_time
                    milliseconds: 0
      - conditions:
          - condition: state
            entity_id: !input profile_input_select
            state: Flash
        sequence:
          - service: light.turn_on
            data: 
              transition: !input transition_on
            target:
              !input target_lights
          - repeat:
              while:
                - condition: template
                  value_template: "{{ expand(lights.entity_id) | selectattr('state', '==', 'on') | list | count > 0 }}"
                - condition: state
                  entity_id: !input profile_input_select
                  state: Flash
              sequence:
                - service: light.turn_on
                  data:
                    flash: short
                  target:
                    !input target_lights
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 3
                    milliseconds: 0
      - conditions:
          - condition: state
            entity_id: !input profile_input_select
            state: Circadian
        sequence:
          - choose:
              - conditions:
                  - condition: time
                    after: !input profile_circadian_bed_time
                    before: !input profile_circadian_wake_up_time
                sequence:
                  - if:
                      - condition: template
                        value_template: >-
                          {{ expand(lights.entity_id) | selectattr('state', '==', 'on') | list | count > 0 }}
                    then:
                      - service: light.turn_on
                        data: 
                          color_temp: !input profile_night_light_color_temp
                          brightness: !input profile_night_light_brightness
                          transition: !input transition_change
                        target:
                          !input target_lights
                    else:
                      - service: light.turn_on
                        data: 
                          color_temp: !input profile_night_light_color_temp
                          brightness: !input profile_night_light_brightness
                          transition: !input transition_on
                        target:
                          !input target_lights
              - conditions:
                  - condition: sun
                    before: sunset
                    after: sunrise
                    before_offset: !input sun_before_offset
                    after_offset: !input sun_after_offset
                sequence:
                  - if:
                      - condition: template
                        value_template: >-
                          {{ expand(lights.entity_id) | selectattr('state', '==', 'on') | list | count > 0 }}
                    then:
                      - service: light.turn_on
                        data: 
                          color_temp: !input profile_bright_color_temp
                          brightness: !input profile_bright_brightness
                          transition: !input transition_change
                        target:
                          !input target_lights
                    else:
                      - service: light.turn_on
                        data: 
                          color_temp: !input profile_bright_color_temp
                          brightness: !input profile_bright_brightness
                          transition: !input transition_on
                        target:
                          !input target_lights
            default:
              - if:
                  - condition: template
                    value_template: >-
                      {{ expand(lights.entity_id) | selectattr('state', '==', 'on') | list | count > 0 }}
                then:
                  - service: light.turn_on
                    data:
                      color_temp: !input profile_dimmed_color_temp
                      brightness: !input profile_dimmed_brightness
                      transition: !input transition_change
                    target:
                      !input target_lights
                else:
                  - service: light.turn_on
                    data:
                      color_temp: !input profile_dimmed_color_temp
                      brightness: !input profile_dimmed_brightness
                      transition: !input transition_on
                    target:
                      !input target_lights
      - conditions:
          - condition: state
            entity_id: !input profile_input_select
            state: Adaptive
        sequence:
          - choose:
              - conditions:
                  - condition: state
                    entity_id: !input profile_adaptive_input_switch
                    state: "on"
                sequence:
                  - if:
                      - condition: template
                        value_template: >-
                          {{ expand(lights.entity_id) | selectattr('state', '==', 'on') | list | count > 0 }}
                    then:
                      - service: light.turn_on
                        data: 
                          color_temp: !input profile_night_light_color_temp
                          brightness: !input profile_night_light_brightness
                          transition: !input transition_change
                        target:
                          !input target_lights
                    else:
                      - service: light.turn_on
                        data: 
                          color_temp: !input profile_night_light_color_temp
                          brightness: !input profile_night_light_brightness
                          transition: !input transition_on
                        target:
                          !input target_lights
              - conditions:
                  - condition: sun
                    before: sunset
                    after: sunrise
                    before_offset: !input sun_before_offset
                    after_offset: !input sun_after_offset
                sequence:
                  - if:
                      - condition: template
                        value_template: >-
                          {{ expand(lights.entity_id) | selectattr('state', '==', 'on') | list | count > 0 }}
                    then:
                      - service: light.turn_on
                        data: 
                          color_temp: !input profile_bright_color_temp
                          brightness: !input profile_bright_brightness
                          transition: !input transition_change
                        target:
                          !input target_lights
                    else:
                      - service: light.turn_on
                        data: 
                          color_temp: !input profile_bright_color_temp
                          brightness: !input profile_bright_brightness
                          transition: !input transition_on
                        target:
                          !input target_lights
            default:
              - if:
                  - condition: template
                    value_template: >-
                      {{ expand(lights.entity_id) | selectattr('state', '==', 'on') | list | count > 0 }}
                then:
                  - service: light.turn_on
                    data:
                      color_temp: !input profile_dimmed_color_temp
                      brightness: !input profile_dimmed_brightness
                      transition: !input transition_change
                    target:
                      !input target_lights
                else:
                  - service: light.turn_on
                    data:
                      color_temp: !input profile_dimmed_color_temp
                      brightness: !input profile_dimmed_brightness
                      transition: !input transition_on
                    target:
                      !input target_lights
    default:
      - if:
          - condition: 
            -  "{{ expand(lights.entity_id) | selectattr('state', '==', 'on') | list | count > 0 }}"
        then:
          - service: light.turn_on
            data: 
              transition: !input transition_change
            target:
              !input target_lights
        else:
          - service: light.turn_on
            data: 
              transition: !input transition_change
            target:
              !input target_lights
        

I hope someone out there can help me with this PLEASE???
:pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray:

Isn’t this working? Which error do you have?
Anything in the log?

I’m not solving the problem, but you can include the count test as a variable, then just test for that variable in the conditions, so you have a single point for editing this.

Like this:

variables:
  lights: !input "target_lights"
  lights_on: >-
    {{ expand(lights) 
    | selectattr('state', 'eq', 'on') 
    | list 
    | count > 0 }}

sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: !input profile_input_select
            state: Bright
        sequence:
          - if:
              - condition: 
                - "{{ lights_on }}"
            then:
              - servicee
...

You can also create variables for transition_change and transition_on and then reduce this whole block to:

          - service: light.turn_on
            data:
              color_temp: !input profile_bright_color_temp
              brightness: !input profile_bright_brightness
              transition: "{{ iif(lights_on, transition_change, transition on) }}"
            target:
              !input target_lights

Also:

This will allow users to select devices, which will probably break the expand you have later (I’m not sure if expand works for devices).

Maybe you could do like this to force selecting entities (which in other hands don’t have the same nice user experience):

    target_lights:
      name: Targeted Lights
      description: The Lights this Script is Targeted at.
      selector:
        entity:
          multiple: true
          domain: light
1 Like

Sweet, thanks for that. That has heaps of useful stuff in it that helps heaps.
So once again thanks heaps for that EdwardTFNEdward Firmo But that doesn’t really answer my original Question.

For anyone else who finds this post. The problem with the above example is that the target selector; result (which can be found in the [Edit in yaml] of the saved script parameters). E.g.

  input:
    target_lights:
      area_id: office

But the template calculation is expecting A [entity_id] not A [area_id]. So the answer to the problem is in either how we handle the input data with the [Variables] or by using another {Selector}.

So if we format the variable input data according to if the user selected {Area, Device or Entity} in the blueprint parameter {target_lights>selector>target>entity>domain: lights}.

  input:
    target_lights:
      name: Targeted Lights
      description: The Lights this Script is Targeted at.
      selector:
        target:
          entity:
            domain: light

variables:
  lights: !input "target_lights"
  lights.area_id: "{{ expand (  area_entities ( lights.area_id ) ) | selectattr ( 'domain' , 'eq' , 'light' ) | selectattr ( 'state' , 'eq' , 'on' ) | list | count > 0 }}"
# for entities:
  lights.entity_id: "{{ is_state ( lights.entity_id , 'on' ) }}"
# for device:
  lights.device_id: "{{ is_state ( device_entities ( lights.device_id ) | first , 'on' ) }}"
# combining them all together so that it can handle missing data:
  lights_on: "{% if lights.device_id is defined %} {{ is_state ( device_entities ( lights.device_id ) | first , 'on' ) }} {% elif lights.entity_id is defined %} {{ is_state ( lights.entity_id , 'on' ) }} {% else %} {{ expand (  area_entities ( lights.area_id ) ) | selectattr ( 'domain' , 'eq' , 'light' ) | selectattr ( 'state' , 'eq' , 'on' ) | list | count > 0 }} {% endif %}"

- condition: 
  - "{{ lights_on }}"

Otherwise we choose another [Selector>Target] like [area] rather than [entity] & rewrite half of the script.
Please feel free to correct me if I’m wrong or if you have a better way of doing this?
Edit: :sob: There is a much better way of doing this: WTH! triumph How do you reference A Device’s state (On/Off/unavailable) for A Target Selector?

1 Like