Condition based on state with variable entity_id

Hi,

I’ve made a script where I want to turn on something when it is off.
The goal is for this to be reuseable. The service switch.turn_on works with a variable for the property target.entity_id, but the condition isn’t working like this.

I’m getting this error:

Message malformed: Entity {{ plug_entity_id }} is neither a valid entity ID nor a valid UUID for dictionary value @ data['sequence'][0]['entity_id']

alias: Test
sequence:
  - condition: state
    entity_id: "{{ plug_entity_id }}"
    state: "off"
  - service: switch.turn_on
    data: {}
    target:
      entity_id: "{{ plug_entity_id }}"
mode: single

The script is called from an automation like this:

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - input_select.heater_mode
condition: []
action:
  - service: script.test
    data:
      plug_entity_id: switch.shellyplug_1

First, why do you need this?
Why do you need to know if the boolean is off before turning it on?
And what is plug? is this some variable?

How are you calling the script?

It was an example. I changed my post to a real life switch.

From an automation. I’ve added this tho my question, thx!

IIRC, State conditions do not accept templates… Assuming you are passing the variable correctly, use a template condition:

alias: Test
sequence:
  - condition: template
    value_template: "{{ is_state(plug_entity_id, 'off') }}"
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: "{{ plug_entity_id }}"
mode: single
4 Likes

Thank you! This is what I was looking for. I still don’t understand why home assistant doesn’t have complete yaml references with an explanation per property.

You mean like this?

https://www.home-assistant.io/docs/scripts/conditions/#state-condition

That’s no complete reference. Only parts that are explained. I mean something like this

If you scroll to the bottom of the page you will see this:

1 Like

I only have two words on that topic Tom… Sunrise/Sunset :laughing:

1 Like