Get the state / brightness off a ligh

Hi,

Still having difficulties to play around with HA templating …

What I want to do :
get the state of a lamp and/or read it’s brigthness
and send it to rhasspy’s tts (tts works perfectly …)

What I have in automation.yaml :slight_smile:

 - alias: 'rhasspy GetState'
  trigger:
    - platform: event
      event_type: rhasspy_GetState
    - platform: state
      entity_id: light.bureau
  action:
    - service: rest_command.tts
      data_template: 
        payload: >
          {% set myState = light.bureau.attributes %}
         State is {{myState}}

Log file : Error rendering data template: UndefinedError: ‘light’ is undefined

There is something i’m missing …

Thanks for your help
:slight_smile:

Instead of light.bureau.attributes try

states('light.bureau')

You also don’t need to define a variable, you could just do

State is {{ states('light.bureau') }}

I would also add the name of the light, because if your light changes state, there will be a message with it’s state but you don’t know what device HA is talking about (assuming you have more than one device you want to know the state of it).

I suggest something like this:

payload: >
  {% if states('light.bureau') == 'on' %}
     Light in bureau is on and brightness is {{ (state_attr('light.bureau', 'brightness') / 255 * 100) | int }} percent.
  {% else %}
     Light in bureau is {{ states('light.bureau') }}
  {% endif%}

@Tediore, @Burningstone

Thanks, I’ll check your advices asap …

:slight_smile:

p