Display the value of a input_text in a log message

I am just getting started in HA so sorry if this has been asked before but I could not find an answer. I am trying to set the value of a input_text from a group of lights and it appears to be working but when I try to display it in the log file I get an error
Error: Error rendering data template: UndefinedError: ‘input_text’ is undefined.

Also when i try to toggle the device after the log file message I get the identity_id as unknown.

I think it is my syntax that is the problem and my limited understanding of yaml

The code I am using is as below

       data_template:
         entity_id: input_text.light_to_switch
         value: '{{ states.group.simulation_lights.attributes.entity_id | random }}'
     - service: logbook.log
        data:
        name: Notice..
          message: Random Away Lights Step 2  '{{input_text.light_to_switch}}'
     - service: homeassistant.toggle
       data_template:
         entity_id: '{{states(''input_text.light_to_switch'')}}' ```

This is not how you access the state of an entity:

'{{input_text.light_to_switch}}'

This is:

"{{ states('input_text.light_to_switch') }}"

I suggest you have a read of this:

There are lots of examples.

Also your indentation is not correct. Should be:

     - service: logbook.log
       data:
         name: Notice..
         message: Random Away Lights Step 2  '{{states('input_text.light_to_switch')}}'
1 Like

Thank you for pointing me in the correct direction. I will read it and let you know how I get on. Once again thank you

Once again Thank you but I still seem to be having a problem. The value set for input_text.light_to_switch is for example light.foyer which I can see in the debug step details. however when i call it in the log as in your example I get the value of unknown. I was looking at the examples and tried .value on the end but still get unknown.

Nowhere does it say to do that.

Please show what you have now.

This is what I have at the moment and the results I am getting.

  - service: input_text.set_value
    data_template:
      entity_id: input_text.light_to_switch
      value: '{{ states.group.simulation_lights.attributes.entity_id | random }}'
  - service: logbook.log
    data_template:
      name: Notice..
      message: Random Away Lights Step 2  '{{states('input_text.light_to_switch')}}'
  - service: homeassistant.toggle
    data_template:
      entity_id: '{{states(''input_text.light_to_switch'')}}'

The first service shows the following result

Executed: November 25, 2021, 8:15:00 PM
Result:
params:
domain: input_text
service: set_value
service_data:
entity_id: input_text.light_to_switch
value: light.kitchen_light
target: {}
running_script: false
limit: 10

However the log shows the following unknown not light.kitchen_light the value set on service 1

Notice… Random Away Lights Step 2 ‘unknown’

Hence the third service fails to toggle as it too has unknown not light.kitchen_light

Executed: November 25, 2021, 8:30:00 PM
Result:
params:
domain: homeassistant
service: toggle
service_data:
entity_id: unknown
target: {}

What does this show as the result in the Developer tools Template editor:

{{states('input_text.light_to_switch')}}

Also, try this:

  - service: homeassistant.toggle
    target:
      entity_id: '{{states(''input_text.light_to_switch'')}}'

And you dont need to use data_template: any more. Just data: will do.

1 Like

{{states(‘input_text.light_to_switch’)}} in developer tools shows unknown

That’s your problem then.

Either you have not got the correct entity id (check in Developer Tools / States) or it has no state set.

EDIT: I’ve just noticed how you are setting the input_text. Try this instead:

  - service: input_text.set_value
    target:
      entity_id: input_text.light_to_switch
    data:
      value: '{{ states.group.simulation_lights.attributes.entity_id | random }}'
1 Like

I could not find the entity_id input_text.light_to_switch in the developer tools. I re added it in helpers and now it is working perfectly. Thank you so much for all your help.

1 Like