Help with Config Error (invalid entity ID)

Hi all,

I’m running into an issue getting a script to pass the validation, for some reason it keeps throwing an error in the logs and I can’t figure out why, especially since there is another variable entity in the script it seems to have no issue with.

Automation (no errors):

- alias: Lights auto on
  id: '9e82b1c4-0f3c-42ed-a8f4-5487d2ad90da'
  mode: parallel
  max: 9
  max_exceeded: silent
  trigger: 
    - platform: state
      entity_id: binary_sensor.back_door
      to: 'on'
    - platform: state
      entity_id: binary_sensor.front_door
      to: 'on'
    - platform: state
      entity_id: binary_sensor.ranch_slider
      to: 'on'
  condition:
    - condition: or
      conditions:
      - condition: state
        entity_id: input_boolean.night_mode
        state: 'on'
      - condition: state
        entity_id: input_boolean.sunset_mode
        state: 'on'
  action:
    - service: script.autodoorlight
      data_template:
        door: "{{ trigger.entity_id }}"
        light: "{{ trigger.entity_id.split('.')[1] }}"

Script:

autodoorlight:
  alias: Door Light Auto on
  variables:
    on_light: 'light.{{ light }}'
  sequence:
    - choose:
      - conditions:
        - condition: state
          entity_id: "{{ on_light }}"
          state: "unavailable"
        sequence:
          - service: script.notification
            data:
              notify_service: notify.user_mobile_devices
              message: "{{ door.attributes.friendly_name }} light is turned off, please turn me on \uD83D\uDCA1"
      default:
        - service: light.turn_on
          entity_id: "{{ on_light }}"
        - wait_for_trigger:
            platform: state
            entity_id: "{{ door }}"
            to: "off"
            for: 
              seconds: 30
          timeout:
            minutes: 5
          continue_on_timeout: true
        - service: light.turn_off
          entity_id: "{{ on_light }}"

Cannot for the life of me figure out why it is ok with the door variable and not the on_light variable. I’ve even done it so the automations sends the whole entity, i.e. light.front_door. Script still throws an error in the logs.

Any help here would be really appreciated.

Thanks

I’m not sure, but is it possible, to use two “templated” entries in a row? First you use a template/variable to get to the on_light. And later on you use this templated entry again. Something rings a bell (but a very small one) that you can’t use it that way. But there is a good chance, that I confuse this with something else, sorry. :slight_smile:

Maybe you try it “directly” and see if it works:

autodoorlight:
  alias: Door Light Auto on
  sequence:
    - choose:
      - conditions:
        - condition: state
          entity_id: "light.{{ light }}"
          state: "unavailable"
[...]

Just tried this:

Automation:

action:
    - service: script.autodoorlight
      data_template:
        open: "{{ trigger.entity_id.split('.')[1] }}"

Script:

- choose:
   - conditions:
     - condition: state
       entity_id: "light.{{ open }}"
       state: "unavailable"

Exact same issue:

Invalid config for [script]: Entity ID light.{{ open }} is an invalid entity ID for dictionary value @ data[‘sequence’][0][‘choose’][0][‘conditions’][0][‘entity_id’]. Got None not a valid value for dictionary value @ data[‘sequence’][0][‘default’][0][‘entity_id’]. Got None. (See /config/configuration.yaml, line 219).

I don’t think you can use a template for entity_id of a state condition.
Just use a template condition.

It is throwing the same error for the above portion too though.

Yet it seems to have no issue with the this portion:

platform: state
entity_id: "{{ door }}"
to: "off"

You can’t template a trigger entity_id

Just make a template trigger instead

wait_for_trigger:
  platform: template
  value_template: >
    {{ is_state(door, 'off') }}

And for your service, if you want to template entity_id, it needs to go in target or data. It can’t be outside

Reduced version of your automation:

- alias: Lights auto on
  id: '9e82b1c4-0f3c-42ed-a8f4-5487d2ad90da'
  mode: parallel
  max: 9
  max_exceeded: silent
  trigger: 
    - platform: state
      entity_id:
        - binary_sensor.back_door
        - binary_sensor.front_door
        - binary_sensor.ranch_slider
      to: 'on'
  condition: "{{ is_state('input_boolean.night_mode', 'on') or is_state('input_boolean.sunset_mode', 'on') }}"
  action:
    - service: script.autodoorlight
      data:
        door: "{{ trigger.entity_id }}"
        light: "{{ trigger.to_state.object_id }}"

Revised version of your script:

autodoorlight:
  alias: Door Light Auto on
  variables:
    on_light: 'light.{{ light }}'
  sequence:
    - choose:
        - conditions: "{{ is_state(on_light, 'unavailable') }}"
          sequence:
            - service: script.notification
              data:
                notify_service: notify.user_mobile_devices
                message: "{{ state_attr(door, 'friendly_name') }} light is turned off, please turn me on \uD83D\uDCA1"
      default:
        - service: light.turn_on
          target:
            entity_id: "{{ on_light }}"
        - wait_for_trigger:
            - platform: template
              value_template: "{{ is_state(door, 'off') }}"
              for: 
                seconds: 30
          timeout:
            minutes: 5
          continue_on_timeout: true
        - service: light.turn_off
          target:
            entity_id: "{{ on_light }}"

EDIT

Corrections. See petro’s post below.

1 Like

Thank you very much, that is definately in the right direction. Getting the error for the condition in the script though:

Invalid config for [script]: Expected a dictionary @ data[‘sequence’][0][‘choose’][0][‘conditions’][0]. Got None.


Post above has been updated to include the corrections.

There’s more errors in your script. You have to use state_attr to get information from an entity_id.

this is wrong, it should be

state_attr(door, 'friendly_name')

Also, you have syntax errors in your condition.

That should be

        - conditions: "{{ is_state(on_light, 'unavailable') }}"

notice the missing ) ?

Thanks for the corrections! I didn’t pay close attention when I revised the script.

1 Like

I was on mobile when i first replied… missed them all too

1 Like

I should have noticed the missing bracket too. :man_facepalming:
I wasn’t sure about the friendly name either, but was going to sort that once the light variable issue was sorted.

All tested and working brilliantly, thank you very much :slight_smile:
If you want to post the full piece up I will mark it as a solution.

You can do it, just paste what you got and mark it as a solution.

1 Like

FWIW, the version I posted above contains the two corrections petro supplied.

1 Like